简体   繁体   English

pygame只显示黑屏?

[英]pygame only shows black screen?

I'm new to pygame and Python in general. 我是pygame和Python的新手。 I looked up other threads that asked similar questions but it seems that they reached different solutions. 我查找了其他询问类似问题的线程,但似乎它们达成了不同的解决方案。 Please, someone tell me what is wrong with my code. 拜托,有人告诉我我的代码有什么问题。

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

is working with correct indentation ( 4 spaces indentation in any loop ( while ,...) or if statement) : 正在使用正确的缩进(任何循环( while ,...)或if语句中有4个空格的缩进):

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 also used an absolute path for the ball .jpg 我也为球.jpg使用了绝对路径

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM