2017-10-05 154 views
0

我是pygame和Python的新手。我查找了其他問題,提出類似的問題,但似乎他們達到了不同的解決方案。請有人告訴我我的代碼有什麼問題。pygame只顯示黑屏?

import pygame 
import sys 

pygame.init() 

size = (width, height) = (720, 720) 

background_color = (0, 255, 0) 

screen = pygame.display.set_mode(size) 

ball = pygame.image.load("img/pokeball.jpg") 
speed = [2, 3] 

ballrect = ball.get_rect() 

ballrect.move_ip(0, 0) 

while True: 

    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: sys.exit() 

ballrect = ballrect.move(speed) 

if ballrect.left < 0 or ballrect.right > width: 
    speed[0] = -speed[0] 

if ballrect.top < 0 or ballrect.bottom > height: 
    speed[1] = -speed[1] 

screen.fill(background_color) 
screen.blit(ball, ballrect) 
pygame.display.flip() 
+1

檢查您的縮進。我非常確定在True時代的一切:應該是在這個循環中。 –

+0

謝謝。現在我希望能刪除這個問題 – Neo

回答

0

正在與正確的縮進(4個空格縮進任何環路(while,...)或if語句):

import pygame 
import sys 

pygame.init() 

size = (width, height) = (720, 720) 

background_color = (0, 255, 0) 

screen = pygame.display.set_mode(size) 

ball = pygame.image.load("/absolute_path/pokeball.jpg") 
speed = [2, 3] 

ballrect = ball.get_rect() 

ballrect.move_ip(0, 0) 

while True: 

    for event in pygame.event.get(): 

     if event.type == pygame.QUIT: sys.exit() 

     ballrect = ballrect.move(speed) 

     if ballrect.left < 0 or ballrect.right > width: 
      speed[0] = -speed[0] 

     if ballrect.top < 0 or ballrect.bottom > height: 
      speed[1] = -speed[1] 

    screen.fill(background_color) 
    screen.blit(ball, ballrect) 
    pygame.display.flip() 

我還使用絕對路徑球的.jpg