2015-12-06 130 views
1

我一直在Python上编写一个程序,一个生存游戏。我似乎有添加图像/精​​灵的问题。我注意到图像闪烁。有任何解决这个问题的方法吗?Python Sprites /图像闪烁

import os 
import pygame 
import time 
import random 
from pygame.locals import * 


launchLog = pygame.init() 
print(launchLog) 

white = (255,255,255) 
black = (0,0,0) 
red=(255,0,0) 
blue=(0,0,255) 
green=(0,255,0) 
skin = (236,227,100) 
size = 10 
dependant = (green) 
rate = 0.0018 
weight = 100 
bound_x = 600 
bound_x2 = bound_x-size 
bound_y = 600 
bound_y2 = bound_y-size 

screen = pygame.display.set_mode((bound_x,bound_y)) 
pygame.display.set_caption("Survival: EfEs Edition") 



clock = pygame.time.Clock() 

font = pygame.font.SysFont(None,25) 
x = 300 
y = 300 
roundX = x 
roundY = y 
img=pygame.image.load("pewds.png") 





def hmn(x,y,size): 
    clothes = pygame.draw.rect(screen,skin,[x,y,size,size-size*2]) 
    snake = pygame.draw.rect(screen, red, [x,y,size,size]) 

def mesg(msg,color): 
    txt = font.render(msg, True, color) 
    screen.blit(txt, [x,y-30]) 
    display.flip(img) 

def gameLoop(): 
    global x 
    global y 
    jump = 1 
    quitted = False 
    starved = 100 
    eaten = False 
restart = False 

lead_change = 0 
lead_y = 0 

randF_x = random.randrange(0,bound_x) 
randF_y = random.randrange(0,bound_y) 
didX2=round(randF_x/10.0)*10.0 
didY2=round(randF_y/10.0)*10.0 
while not quitted: 
    screen.blit(img,(x,y -15)) 
    screen.blit(img,(x,y -15)) 
    pygame.display.update() 
    hmn(x,y,size) 
    starved=starved-rate 
    #print(starved) 
    if starved<0: 
     screen.fill(white) 
     mesg("You died of hunger! Press R to restart.",red) 
     pygame.display.flip() 
     time.sleep(0.3) 
     for event in pygame.event.get(): 
      if event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_r: 
        starved = 100 
        pygame.display.update() 
        rate = 0.0018 
        x=300 
        y=300 


    for event in pygame.event.get(): 
     #print(event) 
     if event.type == pygame.QUIT: 
      pygame.display.update() 
      quitted = True 
     if event.type == pygame.KEYDOWN: 
      if event.key == pygame.K_a: 
       lead_change = -.2 
      elif event.key == pygame.K_d: 
       lead_change = .2 
      elif event.key == pygame.K_w: 
       lead_y = .2 
      elif event.key == pygame.K_s: 
       lead_y = -0.2 

     if event.type == pygame.KEYUP: 
      if event.key == pygame.K_d or event.key == pygame.K_a: 
       lead_change = 0 
       #print(x) 
       #print(y) 
      elif event.key == pygame.K_ESCAPE: 
       #print("Quitting") 
       quitted = True 
      elif event.key == pygame.K_w or event.key == pygame.K_s: 
       #print(y) 
       lead_y=0 


    x += lead_change 
    y -= lead_y 
    roundX = x 
    roundY = y 
    global roundX 
    global roundY 
    didX=round(roundX/10)*10 
    didY=round(roundY/10)*10 
    screen.fill(white) 
    s = pygame.draw.rect(screen, red, [bound_x2/15,bound_y2/20, starved * 2, 50]) 
    pygame.draw.rect(screen, dependant, [didX2,didY2,10,10]) 
    pygame.display.update() 

    #boundary script 
    if x>bound_x2: 
     x=bound_x2 
    elif x<-1: 
     x=-1 
    elif y<2: 
     y=2 
    elif y>590: 
     y=590 
    hmn(x,y,size) 
    pygame.display.flip() 
    if didX == didX2 and didY==didY2: 

     didX2 = round(random.randrange(0,bound_x)/10)*10 
     didY2 = round(random.randrange(0,bound_y)/10)*10 
     global dependant 
     dependant = green 
     global starved 
     starved = starved +0.01 
     global weight 
     weight = weight + 3 

    elif weight>150: 
     mesg("Weight increased! You now get hungry faster.",black) 
     pygame.display.update() 
     global rate 
     rate = rate+0.0008 




pygame.display.update()  
clock.tick(55) 

mesg("Leaving game", black) 
screen.blit(img,(x,y-15)) 
pygame.display.update() 
time.sleep(2) 
pygame.quit() 
quit() 

gameLoop()` 

请原谅坏编码,游戏只处于它的最早状态。

回答

0

正确的顺序是:

  • 吸引你要显示的帧(所有的blit()S,画()S)上
  • 只做显示器之一的一切,等等。更新()与所有更改区域的列表或display.flip()更新整个屏幕,一旦你绘制了所有内容

tl; dr-不要将draw()与update秒。