因此,对于pygame,你有一个while循环连续循环,然后你的事件处理程序在for循环中,然后是你想要连续执行的代码。
我需要在while循环中执行一行代码之后处理一个事件,尽管它们相互影响,但我还需要在行之前处理不同的事件。
如何在我的主while循环中处理一组事件,执行一些代码,然后处理另一组事件?
答案 0 :(得分: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()