I'm making a small game using PyGame, and my character keeps flashing rapidly when I run the game, and I can't figure out why. Can someone please help me out? Here's my code:
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)
You're flipping the screen twice. Once right after you draw the objects, and again near the bottom of the loop.
Additionally you appear to have blits which correspond to event.keydown type things. Not sure if these are your actual player sprite objects or other ancillary things, but ideally I don't think you want to handle any blitting there - you just want to change the state of your objects (and I don't necessarily mean 'make a state machine' if you're not writing one, I just mean - handle the event here... draw it later)
Ideally a 'dummy loop' for PyGame would resemble something like this:
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)
I mean there's no collision checks there or anything, but this is the idea, ultimately -- don't update the screen until after events are processed.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다