2014-02-12 22 views
0

蟒蛇我在Python有一個程序:形象不好,在pygame的

import sys, random, pygame 
from pygame.locals import * 
pygame.init() 

# Preparing Pygame 
size = width, height = 718, 502 
screen = pygame.display.set_mode(size) 
framerate = pygame.time.Clock() 
pygame.display.set_caption('Flappy Cube') 

#Peparing background 
background_x = 0 
background_y = 0 
background = pygame.image.load("background.jpg") 


#Preparing Cube 
cube_x = 100 
cube_y = 200 
cube_unscaled = pygame.image.load("cube.png") 
cube = pygame.transform.smoothscale(cube_unscaled, (64, 64)) 

#Preparing Tubes 
tube_x = 750 
tube_y= 300 
tube_unscaled = pygame.image.load("tube.png") 
tube = pygame.transform.smoothscale(tube_unscaled, (125, 500)) 



# The main game loop 
def exit_game(): 
     sys.exit() 

while True: 
     #Background 
     screen.blit(background, (background_x,background_y)) 
     background_x = background_x+254.5 
     screen.blit(cube,(cube_x, cube_y)) 
    #Tube 
    tube_x = tube_x -.075 
    screen.blit(tube,(tube_x, tube_y)) 
    # If exit 
    for event in pygame.event.get(): 
      if event.type == pygame.QUIT: exit_game() 
    # If Space Key is presed 
      elif event.type == pygame.KEYDOWN and event.key == K_SPACE: 
        cube_y = cube_y-10 
    #If Clicked 
      elif pygame.mouse.get_pressed()[0]: 
        cube_y = cube_y-10 
    framerate.tick(60) 
    pygame.display.flip() 
run_game() 

我得到這樣的結果:http://i.stack.imgur.com/MKYRM.png

我必須提高framerate.tick(60)framerate.tick(700)它看起來glichy。當我編程重力時,多個圖像看起來不太好。

我該如何修復屏幕更新前多次繪製的圖像?

+0

嗯我不知道你想做什麼。 :D –

+0

哈哈...在飛過的鳥兒關閉後,我出發了...但我改變管img。 – user3299661

+0

問題是多維數據集的外觀,對吧? – pmoleri

回答

0

我修好了!我只需要在屏幕上顯示「bliting」之前用黑色填充屏幕。

0

嘗試:

cube_unscaled = pygame.image.load("cube.png").convert_alpha() 

你不應該需要提高你的FPS在這樣的方式,60是一個很好的價值。

此外,我更喜歡你的主循環的順序:檢查事件,繪製,更新,打勾。

我認爲這不會對您的問題產生影響,但我認爲這樣更容易理解。

while True: 

    # If exit 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: exit_game() 

     # If Space Key is presed 
     elif event.type == pygame.KEYDOWN and event.key == K_SPACE: 
      cube_y = cube_y-10 

     #If Clicked 
     elif pygame.mouse.get_pressed()[0]: 
       cube_y = cube_y-10 

    #Background 
    screen.blit(background, (background_x,background_y)) 
    background_x = background_x+254.5 

    screen.blit(cube,(cube_x, cube_y)) 

    #Tube 
    tube_x = tube_x -.075 
    screen.blit(tube,(tube_x, tube_y)) 

    pygame.display.flip() 
    framerate.tick(60) 
+0

thx。它沒有幫助。 – user3299661