So I've been exparimenting with pygame, I can draw my sprite, but when I press WASD I have to press them all many times, and all the sprite does is move down and side to side, with no real control
I update the self.rect
in the sprite class, and when you press a key it clears the screen and should redraw the sprite with a different xy position.
Here's the code:
import pygame
pygame.init()
screen = pygame.display.set_mode((750, 750))
BLACK = (0, 0, 0)
WHITE = (255,255,255)
RED = (255,0,0)
class Sprite(pygame.sprite.Sprite):
def __init__(self, pos):
super(Sprite, self).__init__()
self.image = demon
self.rect = self.image.get_rect(center = pos)
def update(self, moveX, moveY):
self.rect.x += moveX
self.rect.y += moveY
all_sprites_list = pygame.sprite.Group()
demon = pygame.image.load("C:/programming/doomman/cacodemon.png").convert_alpha()
x = 300
y = 300
my_sprite = Sprite((x, y))
all_sprites_list.add(my_sprite)
clock = pygame.time.Clock()
pygame.display.set_caption("Demon Dance")
carryOn = True
while carryOn == True:
keys = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type==pygame.QUIT:
carryOn=False
elif event.type == pygame.KEYDOWN:
screen.fill(BLACK)
if keys[pygame.K_w]:
my_sprite.update(50, 0)
if keys[pygame.K_s]:
my_sprite.update(-50, 0)
if keys[pygame.K_d]:
my_sprite.update(0, 50)
if keys[pygame.K_a]:
my_sprite.update(0, 50)
pygame.display.flip()
clock.tick(60)
all_sprites_list.draw(screen)
pygame.key.get_pressed()
returns a list with the state of all keyboard buttons. This is not intended to get the key of a keyboard event. The key that was pressed can be obtained from the key
attribute of the pygame.event.Event
object:
while carryOn == True:
# [...]
for event in pygame.event.get():
if event.type==pygame.QUIT:
carryOn=False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
my_sprite.update(50, 0)
if event.key == pygame.K_s:
my_sprite.update(-50, 0)
if event.key == pygame.K_d:
my_sprite.update(0, 50)
if event.key == pygame.K_a:
my_sprite.update(0, 50)
# [...]
The keyboard events (see pygame.event module) occur only once when the state of a key changes. The KEYDOWN
event occurs once every time a key is pressed. KEYUP
occurs once every time a key is released. Use the keyboard events for a single action or a step-by-step movement.
pygame.key.get_pressed()
returns a list with the state of all keyboard buttons. This is not intended to get the key of a keyboard event. The key that was pressed can be obtained from the key
attribute of the pygame.event.Event
object.
However, you must evaluate the keys in the application loop rather than the event loop:
while carryOn == True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
carryOn=False
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
my_sprite.update(50, 0)
if keys[pygame.K_s]:
my_sprite.update(-50, 0)
if keys[pygame.K_d]:
my_sprite.update(0, 50)
if keys[pygame.K_a]:
my_sprite.update(0, 50)
# [...]
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.