在pygame中存在其他图像的同时移动图像时闪烁

时间:2017-05-22 06:09:44

标签: image python-2.7 pygame

import pygame

pygame.init()

screen=pygame.display.set_mode([800,600])

gameon = True

bgcolor=pygame.color.Color('#ffe39b')

gen=pygame.image.load("N:\python\installed files\images\gen.png")

gen_small= pygame.transform.scale(gen,(90,90))

black= ([0,0,0])

white= ([255,255,255])

bus1=pygame.rect.Rect(225,100,15,100)

bus2=pygame.rect.Rect(350,450,100,15)

bus3=pygame.rect.Rect(500,100,15,100)

dx=-200

dy=-200

dx2=-300

dy2=-300

#make a button

genbut=pygame.image.load("N:\python\installed files\images\genbut.png")

busbut=pygame.image.load("N:\python\installed files\images\elecbusbut.png")

linebut=pygame.image.load("N:\python\installed files\images\linebut.png")

motorbut=pygame.image.load("N:\python\installed files\images\motorbut.png")

loadbut=pygame.image.load("N:\python\installed files\images\loadbut.png")

imagepass=False

i=0

while gameon:


genbutzone=genbut.get_rect()
genbutzone.x=400
genbutzone.y=0
screen.blit(gen_small,(600,100))
screen.blit(genbut,(400,0))

screen.blit(busbut,(100,0))
screen.blit(motorbut,(600,0))
screen.blit(loadbut,(300,0))
screen.blit(linebut,(200,0))
screen.blit(gen_small,(350,500))

pygame.draw.rect(screen,black,bus1)

pygame.draw.rect(screen,black,bus2)

pygame.draw.rect(screen,black,bus3)
pygame.display.flip()

event = pygame.event.poll()
mx,my=pygame.mouse.get_pos()
if genbutzone.collidepoint(mx,my) and event.type== pygame.MOUSEBUTTONDOWN:
    imagepass=True
    print("i  hit")
if event.type==pygame.MOUSEBUTTONDOWN and not genbutzone.collidepoint(mx,my):

    print( "hi")
    imagepass=False
    i=i+1

if imagepass==True and event.type==pygame.MOUSEMOTION and i==0:
    dx,dy= pygame.mouse.get_pos()
if imagepass== True and event.type==pygame.MOUSEMOTION and i==1:
    dx2,dy2= pygame.mouse.get_pos()

screen.blit(gen_small,(dx,dy))
screen.blit(gen_small,(dx2,dy2))    
pygame.display.flip()
screen.fill(bgcolor)      

pygame.quit()

quit()

1 个答案:

答案 0 :(得分:0)

请花点时间制定一个真实的问题,并格式化代码。这并不困难。

话虽如此,就我所见,您的问题是多次拨打pygame.display.flip()

确保在游戏循环中首先清理屏幕(screen.fill(bgcolor)),然后完成所有绘图,然后拨打pygame.display.flip()pygame.display.update()一次。