noob here, I am experiencing a weird glitch or bug maybe, I have this code
elif event.type == pygame.KEYUP:
frames = 0
frames_count = 0
This code stops the counter from moving except when I move my mouse onto the window it ignores the frame_count = 0 and starts the counting again, the code below I have made a new veriable called timer so it actually changes the number this some what fixes my issue but I am more confused as to why the code above alone doesnt work just fine, is it a glitch/bug or just my bad coding haha, on the bottom is more of a hack job, I would prefer it to just be frames_count = 0 instead of adding a completely new variable called timer.
import pygame
import sys
from pygame.locals import *
pygame.init()
resWidth = 800
resheight = 600
# Setup the game window + Name and Icon
pygame.display.set_caption("Game")
icon = pygame.image.load("gameicon.png")
pygame.display.set_icon(icon)
screen = pygame.display.set_mode((resWidth, resheight), RESIZABLE)
clock = pygame.time.Clock()
# PLayer
x = 350
y = 300
width = 40
height = 80
vel = 0.19 # The speed of the character
spritesheet = pygame.image.load("SpriteSheet.png")
frames_left = []
frames_left.append(spritesheet.subsurface(pygame.Rect(0, 50, 20, 47)))
frames_left.append(spritesheet.subsurface(pygame.Rect(50, 50, 25, 47)))
frames_left.append(spritesheet.subsurface(pygame.Rect(100, 50, 22, 47)))
frames_left.append(spritesheet.subsurface(pygame.Rect(150, 50, 22, 47)))
frames_left.append(spritesheet.subsurface(pygame.Rect(200, 50, 24, 47)))
frames_left.append(spritesheet.subsurface(pygame.Rect(250, 50, 28, 47)))
frames_left.append(spritesheet.subsurface(pygame.Rect(300, 50, 23, 47)))
frames_left.append(spritesheet.subsurface(pygame.Rect(350, 50, 22, 47)))
frames_left.append(spritesheet.subsurface(pygame.Rect(400, 50, 21, 47)))
frames_right = []
frames_right.append(spritesheet.subsurface(pygame.Rect(0, 150, 20, 47)))
frames_right.append(spritesheet.subsurface(pygame.Rect(50, 150, 25, 47)))
frames_right.append(spritesheet.subsurface(pygame.Rect(100, 150, 22, 47)))
frames_right.append(spritesheet.subsurface(pygame.Rect(150, 150, 22, 47)))
frames_right.append(spritesheet.subsurface(pygame.Rect(200, 150, 24, 47)))
frames_right.append(spritesheet.subsurface(pygame.Rect(250, 150, 28, 47)))
frames_right.append(spritesheet.subsurface(pygame.Rect(300, 150, 23, 47)))
frames_right.append(spritesheet.subsurface(pygame.Rect(350, 150, 22, 47)))
frames_right.append(spritesheet.subsurface(pygame.Rect(400, 150, 21, 47)))
frames_up = []
frames_up.append(spritesheet.subsurface(pygame.Rect(0, 0, 50, 50)))
frames_up.append(spritesheet.subsurface(pygame.Rect(50, 0, 50, 50)))
frames_up.append(spritesheet.subsurface(pygame.Rect(100, 0, 50, 50)))
frames_up.append(spritesheet.subsurface(pygame.Rect(150, 0, 50, 50)))
frames_up.append(spritesheet.subsurface(pygame.Rect(200, 0, 50, 50)))
frames_up.append(spritesheet.subsurface(pygame.Rect(250, 0, 50, 50)))
frames_up.append(spritesheet.subsurface(pygame.Rect(300, 0, 50, 50)))
frames_up.append(spritesheet.subsurface(pygame.Rect(350, 0, 50, 50)))
frames_up.append(spritesheet.subsurface(pygame.Rect(400, 0, 50, 50)))
frames_down = []
frames_down.append(spritesheet.subsurface(pygame.Rect(0, 100, 50, 50)))
frames_down.append(spritesheet.subsurface(pygame.Rect(50, 100, 50, 50)))
frames_down.append(spritesheet.subsurface(pygame.Rect(100, 100, 50, 50)))
frames_down.append(spritesheet.subsurface(pygame.Rect(150, 100, 50, 50)))
frames_down.append(spritesheet.subsurface(pygame.Rect(200, 100, 50, 50)))
frames_down.append(spritesheet.subsurface(pygame.Rect(250, 100, 50, 50)))
frames_down.append(spritesheet.subsurface(pygame.Rect(300, 100, 50, 50)))
frames_down.append(spritesheet.subsurface(pygame.Rect(350, 100, 50, 50)))
frames_down.append(spritesheet.subsurface(pygame.Rect(400, 100, 50, 50)))
animations = {
'left': frames_left,
'right': frames_right,
'up': frames_up,
'down': frames_down
}
player_direction = 'down'
frames = 0
frame_count = 0
timer = 1
# Colours
red = (255, 0, 0)
black = (0, 0, 0)
white = (255, 255, 255)
blue = (0, 0, 255)
# Game Loop
while True:
screen.fill(white) # Makes things visible
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit() # Make the quit button work
if event.type == VIDEORESIZE: # Makes the window resize not glitch
screen = pygame.display.set_mode((event.w, event.h), RESIZABLE)
frame_count += timer # timer continues moving
if frame_count >= 100: # if "number" frames have passed
frame_count = 0 # reset the counter
frames = (frames + 1) % len(animations[player_direction])
keys = pygame.key.get_pressed() # Movement for the player
if keys[pygame.K_LEFT]:
x -= vel
player_direction = 'left'
timer = 1
if keys[pygame.K_RIGHT]:
x += vel
player_direction = 'right'
timer = 1
if keys[pygame.K_UP]:
y -= vel
player_direction = 'up'
timer = 1
if keys[pygame.K_DOWN]:
y += vel
player_direction = 'down'
timer = 1
elif event.type == pygame.KEYUP:
frames = 0
timer = 0
screen.blit(animations[player_direction][frames], (x, y))
# pygame.draw.rect(screen, blue, (x, y, width, height))
# Draws a rectangle (resolution, colour, location x, location y, shape
pygame.display.flip() # Updates everything!
Your code is not processing the pygame.KEYUP
event properly.
Since it's an event case, it needs to be handled in the event processing loop, not in the part that checks for key-presses:
if keys[pygame.K_DOWN]:
y += vel
player_direction = 'down'
timer = 1
elif event.type == pygame.KEYUP: # <-- SHOULD NOT BE HERE
frames = 0
timer = 0
But in with the other events:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit() # Make the quit button work
elif event.type == pygame.KEYUP: # If *any* key released
if event.key == pygame.K_r # 'r' for Reset
frames = 0
timer = 0
elif event.type == VIDEORESIZE: # Makes the window resize not glitch
screen = pygame.display.set_mode((event.w, event.h), RESIZABLE)
Note that your existing code resets the timer on any key release. This sounds wrong to me since it will fire on any of your movement keys being released - maybe this is what you intended. The example code above only resets frames
and timer
if the key released is r
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.