每当我运行我的代码时,屏幕变为空白,没有错误消息

时间:2016-06-07 02:54:58

标签: python pygame

    import pygame       #setup

pygame.init()
screen = pygame.display.set_mode((800,600))                    
DISPLAYSURF = pygame.display.set_caption("Smley Pong")
keepGoing = True
pic = pygame.image.load("Crazysmile.bmp")
colorkey = pic.get_at((0,0))
pic.set_colorkey(colorkey)
picx = 0
picy = 0
BLACK = (0,0,0)
WHITE = (255,255,255)
Clock = pygame.time.Clock()
speedx = 5
speedy = 5
paddlew = 200
paddleh = 25
paddley = 550
picw = 100
pich = 100
points = 0
lives = 5
font = pygame.font.SysFont("Times", 24)

while keepGoing:      # Game loop

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            keepGoing = False
picx += speedx
picy += speedy

if picx <= 0 or picx + pic.get_width() >= 800:
    speedx = -speedx
if picy <= 0:
    speedy = -speedy
if picy >= 500:
    lives -= 1
    speedy = -speedy

screen.fill(BLACK)
screen.blit(pic, (picx, picy))

# Draw Paddle
paddlex = pygame.mouse.get_pos()[0]
paddlex = paddlew/2
pygame.draw.rect(screen, WHITE, (paddlex, paddley, paddlew, paddleh))

# Check for paddle bounce
if picy + pich >=paddley and picy + pich <=paddley + paddleh \
   and speedy > 0:
    if picx +picw / 2 >= paddlex and picx +picw / 2 <= paddlex + \
       paddlew:
        points += 1
        speedy = -speedy

# Draw text on screen
draw_screen = "Lives: " + str(lives) + "points: " + str(points)
# Check whether game is over
if lives < 1:
    speedx = speedy = 0
     draw_String = "Game Over.Your score was: " + str(points)                                                 draw_string += ". Press F1 to play again. "

text = font.render(draw_string, True, WHITE)
text_rect = text.get_rect()
text_rect.centerx = screen.get_rect().centerx
text.rect.y = 10
screen.bilt(text, text_rect)
pygame.display.update()
timer.tick(60)

pygame.quit()    # Exit 

这是一个使用书籍帮助的乒乓球游戏,我想测试一下 pygame,但屏幕只是空白,所以我搜索了如果有人有相同的问题,但他们都有不同的答案,所以我无法解决它。我添加了一些额外的部分,但如果你认为我不需要他们或你想要我添加一些东西只是说你认为我应该做什么。  如果有任何建议请回答,也建议也回答。

1 个答案:

答案 0 :(得分:0)

您必须将代码从picx += speedx行缩进到pygame.quit()之前的行

然后,您必须在pygame.display.flip()行之前添加pygame.quit()(并缩进它!)

然后行text.rect.y = 10没有意义。 font.render()会返回一个表面,您只能在x时设置yblit()位置。

也是这一行:

draw_String = "Game Over.Your score was: " + str(points)                                                 draw_string += ". Press F1 to play again. "

有一些奇怪的缩进。

在导入pygame后,您还必须在某处添加timer = pygame.time.Clock()