我正在开发一款乒乓球游戏,想提高编程水平,但我在移动球时遇到了麻烦。当调用 move_right 方法时,椭圆会向右拉伸,而不是向右移动。我尝试将球变量放入 init 方法中,但这只会让它根本不移动,尽管变量应该会根据 move_right 方法而改变。我也尝试过将 x 和 y 位置设置为 Ball 类中的参数,但这也只会拉伸它。我不明白为什么当我运行以下代码时,我试图移动的球会向右拉伸,而不是向右移动。有人能解释为什么会发生这种情况吗?我已经尝试了所有我能想到的方法,但我无法让它做我想做的事情。
import pygame,sys import random class Ball: def __init__(self): self.size = 30 self.color = light_grey self.x_pos = width/2 -15 self.y_pos = height/2 -15 self.speed = 1 #self.ball = pygame.Rect(self.x_pos, self.y_pos,self.size,self.size) def draw_ball(self): ball = pygame.Rect(self.x_pos, self.y_pos,self.size,self.size) pygame.draw.ellipse(screen,self.color,ball) def move_right(self): self.x_pos += self.speed class Player: def __init__(self,x_pos,y_pos,width,height): self.x_pos = x_pos self.y_pos = y_pos self.width = width self.height = height self.color = light_grey def draw_player(self): player = pygame.Rect(self.x_pos,self.y_pos,self.width,self.height) pygame.draw.rect(screen,self.color,player) class Main: def __init__(self): self.ball=Ball() self.player=Player(width-20,height/2 -70,10,140) self.opponent= Player(10,height/2-70,10,140) def draw_elements(self): self.ball.draw_ball() self.player.draw_player() self.opponent.draw_player() def move_ball(self): self.ball.move_right() pygame.init() size = 30 clock = pygame.time.Clock() pygame.display.set_caption("Pong") width = 1000 height = 600 screen = pygame.display.set_mode((width,height)) bg_color = pygame.Color('grey12') light_grey = (200,200,200) main = Main() #ball = pygame.Rect(main.ball.x_pos, main.ball.y_pos,main.ball.size,main.ball.size) #player = pygame.Rect(width-20,height/2 -70,10,140) #opponent = pygame.Rect(10,height/2-70,10,140) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() #ball = pygame.Rect(main.ball.x_pos, main.ball.y_pos,main.ball.size,main.ball.size) #pygame.draw.rect(screen,light_grey,player) #pygame.draw.rect(screen,light_grey,opponent) #pygame.draw.ellipse(screen,light_grey,ball) main.draw_elements() main.move_ball() main.ball.x_pos += main.ball.speed pygame.display.flip() clock.tick(60)
你的代码中存在几个问题,其中一个主要问题是每次在 draw_ball 方法中重新创建 pygame.Rect 对象。这会导致在移动球时出现拉伸的效果。为了正确移动球,你应该保持一个 pygame.Rect 对象,并在每次绘制时更新它的位置,而不是重新创建它。
draw_ball
pygame.Rect
另外,你在主循环中调用了两次 main.ball.x_pos += main.ball.speed,这也是不必要的。
main.ball.x_pos += main.ball.speed
以下是改进后的代码:
import pygame import sys import random # 初始化 Pygame pygame.init() # 设置显示窗口 width, height = 1000, 600 screen = pygame.display.set_mode((width, height)) pygame.display.set_caption("Pong") # 颜色 bg_color = pygame.Color('grey12') light_grey = (200, 200, 200) # Ball 类 class Ball: def __init__(self): self.size = 30 self.color = light_grey self.x_pos = width / 2 - self.size / 2 self.y_pos = height / 2 - self.size / 2 self.speed = 1 self.rect = pygame.Rect(self.x_pos, self.y_pos, self.size, self.size) def draw_ball(self): pygame.draw.ellipse(screen, self.color, self.rect) def move_right(self): self.rect.x += self.speed # Player 类 class Player: def __init__(self, x_pos, y_pos, width, height): self.rect = pygame.Rect(x_pos, y_pos, width, height) self.color = light_grey def draw_player(self): pygame.draw.rect(screen, self.color, self.rect) # Main 类 class Main: def __init__(self): self.ball = Ball() self.player = Player(width - 20, height / 2 - 70, 10, 140) self.opponent = Player(10, height / 2 - 70, 10, 140) def draw_elements(self): self.ball.draw_ball() self.player.draw_player() self.opponent.draw_player() def move_ball(self): self.ball.move_right() # 主游戏循环 main = Main() clock = pygame.time.Clock() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() # 移动球 main.move_ball() # 填充背景色 screen.fill(bg_color) # 绘制所有元素 main.draw_elements() # 更新显示 pygame.display.flip() # 控制帧率 clock.tick(60)
__init__
self.rect
move_right
self.rect.x
pygame.draw.ellipse
Player
rect
draw_player
ball
player
opponent
draw_elements
move_ball
这样修改后,你的球应该会按照预期移动,而不是拉伸。