2017-10-05 3 views
0

Je suis nouveau sur pygame et Python en général. J'ai cherché d'autres sujets qui posaient des questions similaires mais il semble qu'ils aient atteint des solutions différentes. S'il vous plaît, quelqu'un me dit quel est le problème avec mon code.pygame ne montre que l'écran noir?

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

vérifiez votre indentation. Je suis à peu près sûr que tout ce qui est vrai est censé être dans cette boucle. –

+0

Merci. Maintenant, je souhaite pouvoir supprimer cette question – Neo

Répondre

0

travaille avec indentation correcte (indentation de 4 espaces dans toute boucle (while, ...) ou if déclaration):

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() 

i aussi utilisé un chemin absolu pour le ballon .jpg