Python/Pygame字符快速闪烁
Python/Pygame character flashing rapidly
我正在使用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)
您正在翻转屏幕两次。一次在您绘制对象之后,一次在循环底部附近。
此外,您似乎有对应于 event.keydown 类型事物的 blit。不确定这些是否是您实际的玩家精灵对象或其他辅助对象,但理想情况下我认为您不想在那里处理任何 blitting - 您只想更改对象的状态(我不一定是说 'make a state machine' 如果你不写一个,我的意思是 - 在这里处理事件......稍后再画)
理想情况下,PyGame 的 'dummy loop' 类似于这样:
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)
我的意思是那里没有碰撞检查或其他任何东西,但这就是最终的想法——在处理完事件之前不要更新屏幕。
我正在使用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)
您正在翻转屏幕两次。一次在您绘制对象之后,一次在循环底部附近。
此外,您似乎有对应于 event.keydown 类型事物的 blit。不确定这些是否是您实际的玩家精灵对象或其他辅助对象,但理想情况下我认为您不想在那里处理任何 blitting - 您只想更改对象的状态(我不一定是说 'make a state machine' 如果你不写一个,我的意思是 - 在这里处理事件......稍后再画)
理想情况下,PyGame 的 'dummy loop' 类似于这样:
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)
我的意思是那里没有碰撞检查或其他任何东西,但这就是最终的想法——在处理完事件之前不要更新屏幕。