gpt4 book ai didi

python - 如何将 PyGame 游戏分解为单独的文件?

转载 作者:行者123 更新时间:2023-12-01 00:41:05 28 4
gpt4 key购买 nike

我正在为一群十几个 super 聪明的年轻爱好者教授每两周一次的编码类(class)。我们已经介绍了 OOP 并使用 OOP 创建了基于文本的冒险。

现在我计划教 PyGame 并继续使用对象,我想知道是否可以以每个对象的代码都在单独文件中的方式构建游戏?,这真的很简洁并且更容易构建在。

对于下面的代码,我尝试为每个对象创建单独的文件。这只是部分成功,因为绘制方法从来都不能很好地工作,我相信问题是我不能有单独的文件引用同一个 pygame 屏幕。

import pygame
import random
import time

# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0,0,255)

SCREEN_WIDTH = 700
SCREEN_HEIGHT = 500
pygame.init()

class Paddle:
'''Class to keep players location'''
def __init__(self,x=350, y=480, width =70,height=20):
self.x = x
self.y = y
self.change_x = 0
self.change_y = 0
self.width = width
self.height = height
self.score = 0

def move(self):
self.x += self.change_x
self.y += self.change_y

def draw(self):
pygame.draw.rect(screen, BLUE, [self.x,self.y, self.width, self.height])

def check_collision(self,ball):
if ball.y>460:
if abs(35+ self.x - ball.x) < 30:
self.score += 1
ball.draw(BLUE)
ball.y = 0
ball.x = random.randint(0,650)
ball.change_y = random.randint(2,3+int(self.score/5))

class Ball:
"""Class to keep track of a ball's location and vector."""
def __init__(self,x=350,y=250,size=25):
self.x = x
self.y = y
self.change_x = 0
self.change_y = 0
self.size = size

def move(self):
self.x += self.change_x
self.y += self.change_y

def draw(self,colour = WHITE):
pygame.draw.circle(screen,WHITE, [self.x, self.y], self.size)


# Set the height and width of the screen
size = [SCREEN_WIDTH, SCREEN_HEIGHT]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Bouncing Balls")
done = False
clock = pygame.time.Clock()
screen.fill(BLACK)
ball = Ball()
player = Paddle()
ball.change_y = 2
ball.draw()

while not done:
screen.fill(BLACK)
# --- Event Processing
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
pass
if event.key == pygame.K_LEFT:
player.change_x = -5
if event.key == pygame.K_RIGHT:
player.change_x = 5
else:
ball.change_x = 0
player.change_x = 0

if ball.y > 500:
print('YOUR SCORE: ',player.score)
time.sleep(2)
pygame.quit()
#move ball and player and check if they collide
ball.move()
player.move()
player.check_collision(ball)
#draw ball and player
ball.draw()
player.draw()

#render frame
clock.tick(60)
pygame.display.flip()

# Print score and exit
print('YOUR SCORE: ',player.score)
pygame.quit()

当我有单独的文件时,这是我遇到的与屏幕相关的错误

line 20, in draw
pygame.draw.circle(screen,WHITE, [self.x, self.y], self.size)
NameError: name 'screen' is not defined

最佳答案

PaddleBall 类的 draw() 方法添加一个 surface 参数,并绘制传递给方法的表面上的对象:

class Paddle:
# [...]

def draw(self, surface):
pygame.draw.rect(surface, BLUE, [self.x,self.y, self.width, self.height])
class Ball:
# [...]

def draw(self, surface, colour = WHITE):
pygame.draw.circle(surface, colour, [self.x, self.y], self.size)

现在您可以在任何 pygame.Surface 上绘制对象你想要,例如屏幕:

ball.draw(screen)
player.draw(screen)

关于python - 如何将 PyGame 游戏分解为单独的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57345085/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com