2012-03-28 71 views
0

因此,与pygame的,你有一个while循环不断循环,那么你的事件处理程序在for循环中,那么你想要的代码继续执行。通过while while循环检查事件中途?

我需要后,我执行while循环,虽然他们怎么一回事,因为冲击对方内线一行代码来处理事件,但我也需要前行来处理不同的事件。

怎么样,我的主while循环中,处理一组事件,执行一些代码,然后再处理另一组事件?

+0

我有一个退出事件,用户事件,4个的keydown事件和2个KEYUP事件。我也有一行代码,它会遮住背景,并且会遮住一个图像(应该将它放在背景上,但不会因为顺序)。我用我的用户事件处理程序调用该函数。我尝试了以下顺序:while循环,for循环(退出事件,4个keydown事件,2个keyup事件),blit,for循环(用户事件)。我会编辑帖子并告诉你.. – maxhud 2012-03-28 00:30:05

回答

0

解决。

我在用户事件处理程序中设置了一个等于1的变量,然后在需要执行代码的位置,我检查变量是否等于1,如果是,我执行我的代码并设置变回0

好东西。

这里是我的代码,如果有人想帮我找一个更好的解决办法;)(有点长):

uif = "images/userGray.png" 
bif = "images/bg.jpg" 
cif = "images/chair3.png" 
i = 0 
playerX = 1 
playerY = 1 
pX = 1 
pY = 1 
bX = 0 
bY = 0 
moveX = 0 
moveY = 0 
windowX = 640 
windowY = 480 
lowerY = 1024 
lowerX = 1024 
bullets = [] 
x = 0 
y = 0 
rotate = False 

objects = [] 

objects.append([256,260,410,511]) 

import pygame, sys 
from pygame.locals import * 

pygame.init() 

screen = pygame.display.set_mode((640,480),0,32) 

background = pygame.image.load(bif).convert() 
user = pygame.image.load(uif).convert_alpha() 
chair = pygame.image.load(cif).convert_alpha() 
chair1 = pygame.image.load(cif).convert_alpha() 

pygame.time.set_timer(USEREVENT + 1, 100) 

def shoot(inLoc, clLoc, weapon): 
    bulletId = len(bullets) 
    bullets[bulletId] = [inLoc, clLoc, 200, 3] 

moveSpeed = .1 

while True: 



    for event in pygame.event.get(): 
     if event.type == QUIT: 
      pygame.quit() 
      sys.exit() 
     if event.type == USEREVENT + 1: 
      rotate = True; 
     if event.type == KEYDOWN: 
      if event.key == K_LEFT or event.key == K_a: 
       moveX = -1*moveSpeed 
      elif event.key == K_RIGHT or event.key == K_d: 
       moveX = moveSpeed 
      if event.key == K_DOWN or event.key == K_s: 
       moveY = moveSpeed 
      elif event.key == K_UP or event.key == K_w: 
       moveY = -1*moveSpeed 
     if event.type == KEYUP: 
      if event.key == K_LEFT or event.key == K_a or event.key == K_RIGHT or event.key == K_d: 
       moveX = 0 
      if event.key == K_DOWN or event.key == K_s or event.key == K_UP or event.key == K_w: 
       moveY = 0 

    dontMoveX = 0 
    dontMoveY = 0 

    for obj in objects: 

     if playerX + moveX > obj[0]-user.get_width() and playerX + moveX < obj[1] and playerY + moveY > obj[2]-user.get_height() and playerY + moveY < obj[3]: 
      if playerY + moveY == obj[2]-user.get_height()-1 or playerY + moveY == obj[3]+1: 
       dontMoveX = 0 
      else: 
       dontMoveX = 1 

     if playerX + moveX > obj[0]-user.get_width() and playerX + moveX < obj[1] and playerY + moveY > obj[2]-user.get_height() and playerY + moveY < obj[3]: 
      if playerX + moveX == obj[0]-user.get_width()-1 or playerX + moveX == obj[1]+1: 
       dontMoveY = 0 
      else: 
       dontMoveY = 1 

    if dontMoveX == 0: 
     playerX += moveX 

     if (playerX >= 0 and playerX <= windowX/2) or (playerX >= lowerX-(windowX/2) and playerX <= lowerX-user.get_width()): 
      pX+=moveX 
     if playerX > windowX/2 and playerX < lowerX-(windowX/2): 
      bX+=-1*moveX 

    if dontMoveY == 0: 
     playerY += moveY 

     if (playerY >= 0 and playerY <= windowY/2) or (playerY >= lowerY-(windowY/2) and playerY <= lowerY-user.get_width()): 
      pY+=moveY 
     if playerY > windowY/2 and playerY < lowerY-(windowY/2): 
      bY+=-1*moveY 

    screen.blit(background,(bX,bY)) 



    screen.blit(user,(pX,pY)) 


    pygame.mouse.set_visible(False); 

    if rotate == True:  

     if i < 360: 
      i = i + 18 
     else: 
      i = 0 

     orig_chair_rect = chair.get_rect() 
     chair1 = pygame.transform.rotate(chair, i); 
     rot_chair_rect = orig_chair_rect.copy() 
     rot_chair_rect.center = chair1.get_rect().center 
     chair1 = chair1.subsurface(rot_chair_rect).copy() 

     rotate = False 

    x,y = pygame.mouse.get_pos() 
    x -= chair.get_width()/2 
    y -= chair.get_height()/2 

    screen.blit(chair1,(x,y)) 

    pygame.display.update() 
+0

这听起来有点迂回。你为什么不公布你所做的事,看看有人能指出一个更好的解决方案?用完整代码编辑的 – 2012-03-28 02:20:59

+0

。我是新来的,所以我可能也有其他一些问题。 – maxhud 2012-03-28 02:35:09