The rect (instance) of the sprite is now able to move across the screen - this was done in the main loop - just like checking whether we had closed the window (discussed in post 1) in a for loop - we did the same thing, but instead looked for the keydown event.
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.gameloop = False
if (event.type == pygame.KEYDOWN):
if (event.key==pygame.K_RIGHT):
self.movex= 5
self.player.x += self.movex
elif (event.key==pygame.K_LEFT):
self.movex = -5
self.player.x += self.movex
if (event.type == pygame.KEYUP):
if (event.key==pygame.K_RIGHT):
self.movex= 0
self.player.x += self.movex
elif (event.key==pygame.K_LEFT):
self.movex= 0
self.player.x += self.movex
Though - this code could be more efficient - that being the fact that instead of being in the mainloop - it should be in a method in the player class - hence enforcing data independence. The next thing that must be worked upon is the gravity, at the moment, the 'sprite' is just stuck up the top of the screen, with the user able to move him back and forwards.

No comments:
Post a Comment