[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[pygame] Re:
- To: pygame-users@xxxxxxxx
- Subject: [pygame] Re:
- From: "Hugo Arts" <hugo.yoshi@xxxxxxxxx>
- Date: Sun, 24 Aug 2008 15:02:24 -0500
- Delivered-to: archiver@xxxxxxxx
- Delivered-to: pygame-users-outgoing@xxxxxxxx
- Delivered-to: pygame-users@xxxxxxxx
- Delivery-date: Sun, 24 Aug 2008 16:02:29 -0400
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to :subject:in-reply-to:mime-version:content-type :content-transfer-encoding:content-disposition:references; bh=8r9rCnZ2WrFcCkwEkdfCbqTW40Eo4xPCucOSWD5aMpI=; b=rAY74HdTfXw+06PFLz6Cb6gldaKsS48doC20Bl7W2bmIiH9pE1uY2QBKyoqrv0uPbi 2Nz5VRSa7ZJr4GYwP0SglM114kEOSCR9/RBglUWrQKKs7wRpmp/ecb1RAIdL8vJQHkmW gm6sIi5YYYpKICjyerd0CD1JucNbQvueiwJ90=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:in-reply-to:mime-version :content-type:content-transfer-encoding:content-disposition :references; b=lUGhbq1OwhnypoB3ig5Cek9C9r9Glzm5Jb9yemGsTg8ZzPcJtG8baPFzs+gaKjpNGu 8pfurJX/4WpoRFedW+0ulmQDDDYYA4h+oZiWF6XPnsCbu+TlPSmWAH2DtK/DMKyVy2yc FpNMiHlCErNk23x1Te4QtCsSYRxn6eRZS1dOs=
- In-reply-to: <20080824162431.8F2ACCB6CB@xxxxxxxxxxxxxxxxxxxxxxx>
- References: <20080824162431.8F2ACCB6CB@xxxxxxxxxxxxxxxxxxxxxxx>
- Reply-to: pygame-users@xxxxxxxx
- Sender: owner-pygame-users@xxxxxxxx
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
>