[英]Why ball isnt moving when update is called(pygame Pong)?
调用ball.update()
时,我预计球应该移动,但事实并非如此。
我在想,将bpos
定义为 rect object 并将其传递给椭圆以将球绘制到屏幕上可能存在问题。
bpos = pygame.Rect(screen_width / 2 - 15, screen_height / 2 - 15, 30, 30)
我将 bpos 设为 rect object 然后将其传入以绘制椭圆的原因:
class Ball:
def __init__(self):
self.x = screen_width // 2
self.y = screen_height // 2
self.speed = 5
self.vx = self.speed
self.vy = self.speed
def draw(self, screen):
pygame.draw.ellipse(screen, light_grey, bpos)
def update(self):
self.x += self.vx
self.y += self.vy
是因为最终,当要保持球在界时,如果可能的话,我想在更新方法中使用 pygame 矩形属性(顶部、底部、左侧、右侧)。
def update(self):
self.x += self.vx
self.y += self.vy
**if self.top <= 0 or self.bottom >= screen_height:
self.vy *= -1
if ball.left <= 0 or self.right >= screen_width:
self.vx *= -1**
But the ball isnt moving.
在游戏逻辑中,您可以看到我正在调用:
ball.draw(屏幕) ball.update()
将不胜感激一些输入。 谢谢。 下面是我的代码。
import pygame, sys
class Paddle:
def __init__(self):
self.rect = pygame.Rect(10, screen_height / 2 - 70, 10, 140)
def draw(self, screen):
pygame.draw.rect(screen, light_grey, self.rect)
class Ball:
def __init__(self):
self.x = screen_width // 2
self.y = screen_height // 2
self.speed = 5
self.vx = self.speed
self.vy = self.speed
def draw(self, screen):
pygame.draw.ellipse(screen, light_grey, bpos)
def update(self):
self.x += self.vx
self.y += self.vy
# General setup
pygame.init()
clock = pygame.time.Clock()
# Main Window
screen_width = 1280
screen_height = 960
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Pong')
# Colors
light_grey = (200, 200, 200)
bg_color = pygame.Color('grey12')
# Game Rectangles
ball = Ball()
bpos = pygame.Rect(screen_width / 2 - 15, screen_height / 2 - 15, 30, 30)
left_paddle = Paddle()
right_paddle = Paddle()
right_paddle.rect.x = screen_width - right_paddle.rect.width
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Game logic
screen.fill(bg_color)
ball.draw(screen)
left_paddle.draw(screen)
right_paddle.draw(screen)
ball.update()
pygame.display.flip()
clock.tick(60)
问题不在于您说的每个bpos
都使用 rect。 问题是您没有更新bpos
。
用于绘制椭圆的矩形需要在Ball.update()
中更新。 你更新了 self.x,y 但不是你用来画球的东西。
但是,您使用的 rect 应该是 Ball 实例的属性,而不是外部变量。 您应该在 init 中对其进行初始化并使用 self.rect.x 和 self.rect.y 来更新和跟踪球 position。 不要将 position 同时保存在 self.rect.x 和 self.rect.y 中,也不要单独保存在 self.x、self.y 中。 这只会导致冗余以及可能的不一致和错误。
我会建议这些改变。
改变这个:
# Game Rectangles
ball = Ball()
bpos = pygame.Rect(screen_width / 2 - 15, screen_height / 2 - 15, 30, 30)
像这样:
# Game Rectangles
ball = Ball(screen_width / 2 - 15, screen_height / 2 - 15, 30, 30)
和这个:
class Ball:
def __init__(self):
self.x = screen_width // 2
self.y = screen_height // 2
self.speed = 5
self.vx = self.speed
self.vy = self.speed
def draw(self, screen):
pygame.draw.ellipse(screen, light_grey, bpos)
def update(self):
self.x += self.vx
self.y += self.vy
像这样:
class Ball:
def __init__(self, x, y, width, height):
self.rect = pygame.Rect(x, y, width, height))
self.speed = 5
self.vx = self.speed
self.vy = self.speed
def draw(self, screen):
pygame.draw.ellipse(screen, light_grey, self.rect)
def update(self):
self.rect.x += self.vx
self.rect.y += self.vy
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.