简体   繁体   English

这里的菜鸟 - 当我在屏幕上移动鼠标时,我的精灵更新的 pygame animation

[英]Noob here - pygame animation of my sprite updates when I move the mouse on screen

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.这段代码阻止计数器移动,除非我将鼠标移到 window 上,它会忽略 frame_count = 0 并再次开始计数,下面的代码我创建了一个名为 timer 的新变量,所以它实际上改变了这个数字,这可以解决我的问题问题,但我更困惑为什么上面的代码不能正常工作,是小故障/错误还是我的编码不好哈哈,在底部更像是一项黑客工作,我更希望它只是 frames_count = 0 而不是添加一个名为 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.您的代码未正确处理pygame.KEYUP事件。

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 .如果释放的键是r ,上面的示例代码只会重置framestimer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM