[英]Why ball isnt moving when update is called(pygame Pong)?
When calling ball.update()
I expected the ball should move, but it didn't.调用ball.update()
时,我预计球应该移动,但事实并非如此。
I'm thinking that perhaps there is a problem with defining bpos
as a rect object and passing it in to an ellipse to draw the ball to the screen.我在想,将bpos
定义为 rect object 并将其传递给椭圆以将球绘制到屏幕上可能存在问题。
bpos = pygame.Rect(screen_width / 2 - 15, screen_height / 2 - 15, 30, 30)
The reason i make bpos a rect object and then passs it in as to draw ellipse:我将 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
is because eventually, when it will come to keeping the ball in bounds I would like to use pygame Rect attributes(top,bottom,left,right) in the update method like this if possible.是因为最终,当要保持球在界时,如果可能的话,我想在更新方法中使用 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.
In the game logic you can see that I'm calling:在游戏逻辑中,您可以看到我正在调用:
ball.draw(screen) ball.update() ball.draw(屏幕) ball.update()
Would greatly appreciate some input.将不胜感激一些输入。 Thanks.谢谢。 Below is my code.下面是我的代码。
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)
The problem is not that you are using a rect for bpos
per say.问题不在于您说的每个bpos
都使用 rect。 The problem is that you do not update bpos
.问题是您没有更新bpos
。
The rect you use for drawing the ellipse needs to be updated in your Ball.update()
.用于绘制椭圆的矩形需要在Ball.update()
中更新。 You update the self.x,y but not the thing you use to draw the ball.你更新了 self.x,y 但不是你用来画球的东西。
However, the rect that you use should be a attribute of the Ball instance, not an external variable.但是,您使用的 rect 应该是 Ball 实例的属性,而不是外部变量。 You should initialize it in the init and use the self.rect.x and self.rect.y to update and track the balls position.您应该在 init 中对其进行初始化并使用 self.rect.x 和 self.rect.y 来更新和跟踪球 position。 Do not keep the position both in a self.rect.x,and self.rect.y and separately in a self.x, self.y.不要将 position 同时保存在 self.rect.x 和 self.rect.y 中,也不要单独保存在 self.x、self.y 中。 That will just lead to redundancy and possible inconsistency and error.这只会导致冗余以及可能的不一致和错误。
I would suggest these changes.我会建议这些改变。
Change this:改变这个:
# Game Rectangles
ball = Ball()
bpos = pygame.Rect(screen_width / 2 - 15, screen_height / 2 - 15, 30, 30)
to something like this:像这样:
# Game Rectangles
ball = Ball(screen_width / 2 - 15, screen_height / 2 - 15, 30, 30)
and this:和这个:
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
to something like this:像这样:
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.