[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] Re:



I think the solution would be to utilize not only the KEYDOWN event
but also the KEYUP event. when, for example, a right-arrow KEYDOWN
event is generated, increase an attribute (call it speed) of the
object to something like (5, 0). Then, every frame, add this speed to
the objects position. When the KEYUP event comes, set the speed back
down to (0, 0).

On Sun, Aug 24, 2008 at 11:24 AM, yanom @linuxmail.org
<yanom@xxxxxxxxxxxxx> wrote:
> I have a problem with my pygame project:
> i used pygame.key.set_repeat(2,2) to make it so that continually holding down the left or right arrow keys moves a character. i have a problem though:
> whenever i press a key other than left or right, even if i am still holding an arrow key, my character stops moving. here is my code:
>
>
>
>
> import pygame, os, sys
> from pygame.locals import *
> pygame.init()
> clock = pygame.time.Clock()#start the clock
> screen = pygame.display.set_mode((640, 680), 0, 32)#create the screen, conveniently called screen
> enemy = pygame.image.load("enemy.sub.png").convert_alpha()#load sub images
> player = pygame.image.load("player.sub.png").convert_alpha()
> enemyrect = enemy.get_rect() #create rects
> playerrect = player.get_rect()
> playerrect = playerrect.move(500,500)#move the player sub to the bottom of the screen
> bullet = pygame.image.load("bullet.png").convert_alpha()#load the bullet image
> bulletrect = bullet.get_rect()#create bullet rect
> #bulletrect = bulletrect.move(0,780)#move it to the bottom
> speed = [2,0] #top speed of the enemysub
> pygame.key.set_repeat(2,2) #enable key repeat
> while 1:#main game loop
>        clock.tick(60)#60 fps max
>        enemyrect=enemyrect.move(speed)     #bounce  |
>        if enemyrect.right > 640: speed = [-2, 0]#   |
>        if enemyrect.left <0: speed = [2, 0]#        |
>        if bulletrect.top > 600:
>                bulletrect.left = enemyrect.left
>                bulletrect.top = enemyrect.top
>        else:
>                bulletrect=bulletrect.move(0,7)
>        for event in pygame.event.get(): #event query    |
>                if event.type == QUIT:#                      |
>                        exit()#                                  |
>                if event.type == KEYDOWN:#                   |
>                        if event.key == K_RIGHT:#                |
>                                playerrect = playerrect.move(2,0)#   |
>                        if event.key == K_LEFT:#                 |
>                                playerrect = playerrect.move(-2,0)#  |
>        screen.fill((0,255,255)) #fill screen
>        screen.blit(enemy, enemyrect)#blit enemy
>        screen.blit(player, playerrect)#blit player
>        screen.blit(bullet,bulletrect)#blit bullet
>        pygame.display.update() #update screen
>
>
> how do i solve this problem?
>
> =
> Tecpel - Propel Quality Measurement
> Wide range of quality sources from Taiwan.
> http://a8-asy.a8ww.net/a8-ads/adftrclick?redirectid=d1e2a9b5471fc3d9949f564a2f7d0acf
>
>
> --
> Powered by Outblaze
>