Python/Pygame character flashing rapidly

davkue

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)
user890167

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] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Message guarantees on rapidly updated entities in Firebase

분류에서Dev

How to make flashing text in Css?

분류에서Dev

Multiple flashing text items using 1 id?

분류에서Dev

Terminal flashing between current and previous screen

분류에서Dev

Partition for embedded linux before flashing the SD card

분류에서Dev

Flashing U-boot on beaglebone black

분류에서Dev

Dell laptop: No bios screen, no sounds or flashing lights. Power light is on

분류에서Dev

Prevent flashing of BIOS from Windows with full administrator capabilities

분류에서Dev

Unknown Character

분류에서Dev

What is this character: '*'?

분류에서Dev

character and array

분류에서Dev

Firefox ~ 84에서 "Paint Flashing Tool"은 어디에 있습니까?

분류에서Dev

selectize.js - how can I disable the flashing cursor after the selected item?

분류에서Dev

How can I avoid ListView flashing the view set by @android:id/empty before loading data.

분류에서Dev

If character in string put escape character "\" in front of the found character

분류에서Dev

How to grep and treat meta character as normal character?

분류에서Dev

What is the default character encoding?

분류에서Dev

Character declaration forms

분류에서Dev

Regex with multiple character separator

분류에서Dev

Duplicate Character Counter for an Alphanumeric

분류에서Dev

jquery character countdown in textarea

분류에서Dev

Escape UNIX character

분류에서Dev

Write a character at the end of an array

분류에서Dev

Specific character in sql

분류에서Dev

Python: encode special character

분류에서Dev

Replace special character in htaccess

분류에서Dev

Replace special character in htaccess

분류에서Dev

Deserialize json character as enumeration

분류에서Dev

limit data character post

Related 관련 기사

뜨겁다태그

보관