我正在使用PyGame制作一款小游戏,当我运行游戏时,我的角色会一直闪烁,我无法弄清楚原因。有人可以帮帮我吗?这是我的代码:
import pygame, sys, time, math
from pygame.locals import *
pygame.init()
fpsClock = pygame.time.Clock()
screen = pygame.display.set_mode((640,480))
pygame.display.set_caption("Hunter vs. Hunter")
keys = [False, False, False, False]
shirtpos = [330,270]
pantspos = [342,322]
helmpos = [349,230]
bowpos = [395,248]
invcounter = 0
arrows=[]
bkdrop = pygame.image.load("Resources\grassybackdrop.gif")
helm = pygame.image.load("Resources\mainhelm.gif")
shirt = pygame.image.load("Resources\mainshirt.gif")
pants = pygame.image.load("Resources\mainpants.gif")
inv = pygame.image.load("Resources\invdisplay.gif")
bow = pygame.image.load("1bow.gif")
arrow = pygame.image.load("arrow.gif")
screen.blit(bkdrop, (0,0))
while 1:
screen.blit(helm, helmpos)
screen.blit(shirt, shirtpos)
screen.blit(pants, pantspos)
screen.blit(bow, bowpos)
pygame.display.flip()
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type==pygame.MOUSEBUTTONDOWN:
position=pygame.mouse.get_pos()
arrows.append([math.atan2(position[1]-(shirtpos[1]+32),position[0]-(shirtpos[0]+26)),shirtpos[0]+32,shirtpos[1]+32])
for bullet in arrows:
index=0
velx=math.cos(bullet[0])*10
vely=math.sin(bullet[0])*10
bullet[1]+=velx
bullet[2]+=vely
if bullet[1]<-64 or bullet[1]>640 or bullet[2]<-64 or bullet[2]>480:
arrows.pop(index)
index+=1
for projectile in arrows:
arrow1 = pygame.transform.rotate(arrow, 360-projectile[0]*57.29)
screen.blit(arrow1, (projectile[1], projectile[2]))
if event.type == pygame.KEYDOWN:
if event.key == K_w:
keys[0]=True
elif event.key == K_a:
keys[1]=True
elif event.key == K_s:
keys[2]=True
elif event.key == K_d:
keys[3]=True
elif event.key == K_TAB:
screen.blit(inv, (147,96))
invcounter+=1
if event.type == pygame.KEYUP:
if event.key == K_w:
keys[0]=False
elif event.key == K_a:
keys[1]=False
elif event.key == K_s:
keys[2]=False
elif event.key == K_d:
keys[3]=False
if invcounter % 2 == 0:
if keys[0] == True:
shirtpos[1]-=2.5
pantspos[1]-=2.5
helmpos[1]-=2.5
bowpos[1]-=2.5
if keys[1] == True:
shirtpos[0]-=2.5
pantspos[0]-=2.5
helmpos[0]-=2.5
bowpos[0]-=2.5
if keys[2] == True:
shirtpos[1]+=2.5
pantspos[1]+=2.5
helmpos[1]+=2.5
bowpos[1]+=2.5
if keys[3] == True:
shirtpos[0]+=2.5
pantspos[0]+=2.5
helmpos[0]+=2.5
bowpos[0]+=2.5
if pantspos[1] > 386:
pantspos[1] = 386
shirtpos[1] = 336
helmpos[1] = 296
bowpos[1] = 310
if invcounter % 2 == 0:
screen.blit(bkdrop, (0,0))
pygame.display.flip()
fpsClock.tick(60)
答案 0 :(得分:2)
你正在翻转屏幕两次。一旦你绘制了对象,再一次在循环的底部附近。
此外,您似乎有blits对应于event.keydown类型的东西。不确定这些是你的实际玩家精灵对象还是其他辅助物品,但理想情况下我认为你不想处理任何blitting - 你只是想改变对象的状态(我并不一定意味着'make一个状态机'如果你不写一个,我只是说 - 在这里处理事件......稍后画出来)
理想情况下,PyGame的'虚拟循环'就像这样:
while True:
for event in pygame.event.get():
# handle events
MyScreenObject.fill((0, 0, 0)) # or whatever background you're using
VisibleObjectGroup.draw() # draw all objects that can be seen in a sprite.Group -- use these and love these, they're loaded with good things
pygame.display.flip() #call this ONE time per loop
MyFPSClock.tick(FPS)
我的意思是那里没有任何碰撞检查,但这是最终的想法 - 在事件处理之前不要更新屏幕。