pygame中的python图像很糟糕

时间:2014-02-12 01:35:52

标签: python pygame

我在python中有一个程序:

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),这看起来很怪异。当我对重力进行编程时,多个图像看起来不太好。

如何修复在屏幕更新前多次绘制的图像?

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

我修好了!我只需要在“bliting”它之前用黑色填充屏幕。