python - Pygame key press event -
hey guys i'm trying add movement sprite have , i'm having trouble, heres code. i'm using wasd keys move player around place, can't seem figure out why won't move. appreciated.
import pygame import math sys import exit pygame.locals import * pygame.mixer.init class player(pygame.sprite.sprite): def __init__(self, screen): pygame.sprite.sprite.__init__(self) self.image = pygame.image.load("ryu.png") transcolor = self.image.get_at((1, 1)) self.image.set_colorkey(transcolor) self.rect = self.image.get_rect() self.dx = screen.get_width()/2 self.dy = screen.get_height()/2 self.rect.center = (self.dx, self.dy) self.screen = screen self.speed = 4 def update(self): self.rect.center = (self.dx, self.dy) def returnposition(self): return self.rect.center def moveleft(self): if self.rect.left < 0: self.dx+=0 else: self.dx-=self.speed def moveright(self): if self.rect.right >self.screen.get_width(): self.dx+=0 else: self.dx+=self.speed def moveup(self): if self.rect.top <0: self.dy+=0 else: self.dy-=self.speed def movedown(self): if self.rect.bottom > self.screen.get_height(): self.dy+=0 else: self.dy+=self.speed def checkkeys(mydata): (event, player) = mydata keys = pygame.key.get_pressed() if keys [k_a]: print 'he turned left!!!' player.moveleft() if keys [k_s]: print "he's going down!!!" if keys [k_d]: print "he turned right!!!" if keys [k_w]: print "he's going up!!!" def main(): screen = pygame.display.set_mode((640, 480)) clock = pygame.time.clock() keepgoing = true pygame.display.set_caption("creating sprite") player = player(screen) background = pygame.surface(screen.get_size()) background.fill((0, 0, 0)) allsprites = pygame.sprite.group(player) while keepgoing: clock.tick(30) event in pygame.event.get(): if event.type == pygame.quit: keepgoing = false allsprites.clear(screen, background) allsprites.update() allsprites.draw(screen) pygame.display.flip() if __name__ == "__main__": main()
Comments
Post a Comment