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 x y 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)