2016-06-07 60 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()

相關問題