2016-06-07 158 views
-1
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的,但画面只是一片空白,所以我搜索了如果任何人有同样的问题,但他们都有不同的答案,所以我不能解决它。我添加了一些额外的部分,但如果你认为我不需要他们,或者你想我添加一些东西,只是说你认为我应该做的。 如果有什么建议请回答,也有建议也回答。每当我我的代码运行时出现黑屏与错误消息

+0

修复您的缩进请 – 2016-06-07 06:44:26

+0

您确定没有收到任何错误讯息?我可以看到几个错误: 1)'bilt'而不是'blit' 2)你有timer.tick(60)但你命名了你的时钟对象'Clock' - 没有任何'timer'变量 除了那个缩进的代码很难读懂。修复这个问题,我们可以尝试运行你的代码,看看还有什么不对。 – Chris

+0

在尝试在代码中实现它之前,您需要了解很多事情。从基础开始,如显示白色屏幕或在屏幕上移动正方形。然后你可以掌握基本的游戏编程。 –

回答

0

你将不得不之前pygame.quit()

这样您就可以在pygame.display.flip()pygame.quit()之前添加从线picx += speedx缩进代码,到线(和缩进它!)

后再行text.rect.y = 10没有意义。 font.render()返回一个表面,当您blit()时,您只能设置xy的位置。

也行:

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

有一些奇怪的压痕。

您还必须在导入pygame后的某个地方添加timer = pygame.time.Clock()

相关问题