如何解决如何修复空白的 pygame 屏幕?
我正在开发一个基本的 pong pygame,我编写了 main.py、paddle.py 和 ball.py 文件,不幸的是,当我运行游戏时,它只显示一个黑色的 pygame 窗口。我的桨、球、网和比分不可见……它只是空白。 Screenshot of Blank/Black Window
我似乎无法找到导致此错误的原因,因此将不胜感激任何反馈。
MAIN.PY
# Import pygame library and initialize the game engine
import pygame
from paddle import Paddle
from ball import Ball
pygame.init()
# Define some colors
BLACK = (0,0)
WHITE = (255,255,255)
# Open a new window
size = (700,500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Pong")
paddleA = Paddle(WHITE,10,100)
paddleA.rect.x = 20
paddleA.rect.y = 200
paddleB = Paddle(WHITE,100)
paddleB.rect.x = 670
paddleB.rect.y = 200
ball = Ball(WHITE,10)
ball.rect.x = 345
ball.rect.y = 195
# This will be a list that contains all the sprites we intend to use in game.
all_sprites_list = pygame.sprite.Group()
# Add paddles to the list of sprites
all_sprites_list.add(paddleA)
all_sprites_list.add(paddleB)
all_sprites_list.add(ball)
# Loop will carry on until user exits the game (ex. clicks the close button)
carryOn = True
# Clock will be used to control how fast the screen updates
clock = pygame.time.Clock()
# Initialize player scores
scoreA = 0
scoreB = 0
# ----- Main Program Loop -----
while carryOn:
# --- Main Event Loop ---
for event in pygame.event.get(): # user did something
if event.type==pygame.QUIT: # if user clicked close
carryOn==False # Flag that we are done so we exit this loop
elif event.type==pygame.KEYDOWN:
if event.key==pygame.K_x: # Pressing the x key will quit the game
carryOn==False
# Moving the paddles when the user uses the arrow keys (Player A),# or W/S keys (Player B)
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
paddleA.moveUp(5)
if keys[pygame.K_s]:
paddleA.moveDown(5)
if keys[pygame.K_UP]:
paddleB.moveUp(5)
if keys[pygame.K_DOWN]:
paddleB.moveDown(5)
# --- Game Logic ---
all_sprites_list.update()
# Checks if the ball is bouncing against any of the 4 walls
if ball.rect.x>=690:
ball.veLocity[0] = -ball.veLocity[0]
if ball.rect.x<=0:
ball.veLocity[0] = -ball.veLocity[0]
if ball.rect.y>490:
ball.veLocity[1] = -ball.veLocity[1]
if ball.rect.y<0:
ball.veLocity[1] = -ball.veLocity[1]
#Detect collisions between the ball and the paddles
if pygame.sprite.collide_mask(ball,paddleA) or pygame.sprite.collide_mask(ball,paddleB):
ball.bounce()
# --- Drawing Code ---
# clears the screen to black
screen.fill(BLACK)
# draws the net
pygame.draw.line(screen,WHITE,[349,0],500],5)
# Draws all sprites in one go (I only have 2 for Now)
all_sprites_list.draw(screen)
# display scores
font = pygame.font.Font(None,74)
text = font.render(str(scoreA),1,WHITE)
screen.blit(text,(250,10))
text.font.render(str(scoreB),WHITE)
screen.blit(text (420,10))
# update screen with what we've drawn
pygame.display.flip()
# limit to 60 frames per second
clock.tick(60)
# Once we have exited the main program loop we stop the game engine
pygame.quit()
PADDLE.PY
import pygame
BLACK = (0,0)
class Paddle(pygame.sprite.Sprite):
# This class represents a paddle. It derives from the "Sprite" class in Pygame.
def __init__(self,color,width,height):
# Call the parent class (Sprite) constructor
super().__init__()
# Pass in the color of the paddle and it's x and y position (width nd height).
# Set the background color as transparent
self.image = pygame.Surface([width,height])
self.image.fill(BLACK)
self.image.set_colorkey(BLACK)
# Draw the paddle which is a rectangle
pygame.draw.rect(self.image,[0,height])
# Fetch the rectangle object that has the dimensions of the image
self.rect = self.image.get_rect()
def moveUp(self,pixels):
self.rect.y -= pixels
# Check that you are not going too far (off screen)
if self.rect.y < 0:
self.rect.y = 0
def moveDown(self,pixels):
self.rect.y += pixels
# Check that you are not going too far (off screen)
if self.rect.y > 400:
self.rect.y = 400
BALL.PY
import pygame
from random import randint
BLACK = (0,0)
class Ball(pygame.sprite.Sprite):
#This class represents a ball. It derives from the "Sprite" class in Pygame.
def __init__(self,height):
# Call the parent class (Sprite) constructor
super().__init__()
# Pass in the color of the ball,its width and height.
# Set the background color and set it to be transparent
self.image = pygame.Surface([width,height])
self.image.fill(BLACK)
self.image.set_colorkey(BLACK)
# Draw the ball (a rectangle!)
pygame.draw.rect(self.image,height])
self.veLocity = [randint(4,8),randint(-8,8)]
# Fetch the rectangle object that has the dimensions of the image.
self.rect = self.image.get_rect()
def update(self):
self.rect.x += self.veLocity[0]
self.rect.y += self.veLocity[1]
def bounce(self):
self.veLocity[0] = -self.veLocity[0]
self.veLocity[1] = randint(-8,8)
解决方法
您只是没有在主文件中的主循环中翻转屏幕。
实际上,所有内容都在更新,但屏幕上没有呈现任何内容。
只是打算来自
的代码# --- Drawing Code ---
到
clock.tick(60)
我想你忘记缩进了。
注意不要在主循环中包含 pygame.quit()
。
只需添加一个制表层
...
# --- Drawing Code ---
...
以 main.py 结尾
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。