【发布时间】:2015-01-28 22:42:00
【问题描述】:
我正在使用 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)
【问题讨论】:
-
如果您的图像在绘制时闪烁,则说明您可能需要进行双缓冲。
标签: python python-2.7 pygame