【问题标题】:Why ball isnt moving when update is called(pygame Pong)?为什么调用更新时球不移动(pygame Pong)?
【发布时间】:2020-05-29 12:31:37
【问题描述】:

在调用ball.update() 时,我预计球应该会移动,但事实并非如此。

我认为将bpos 定义为矩形对象并将其传递给椭圆以将球绘制到屏幕上可能存在问题。

游戏矩形

bpos = pygame.Rect(screen_width / 2 - 15, screen_height / 2 - 15, 30, 30)

我将 bpos 设为 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

是因为最终,如果可能的话,我想在更新方法中使用 pygame Rect attributes(top,bottom,left,right)。

 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(屏幕) 球.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来画球。您可能希望将您的 draw 方法更改为 pygame.draw.ellipse(screen, light_grey, pygame.Rect(self.x, self.y, 30, 30))

标签: python pygame pong


【解决方案1】:

问题不在于您使用 rect 表示bpos。问题是您没有更新bpos

您用于绘制椭圆的矩形需要在您的Ball.update() 中更新。你更新了 self.x,y 但不是你用来画球的东西。

但是,您使用的 rect 应该是 Ball 实例的属性,而不是外部变量。您应该在 init 中对其进行初始化,并使用 self.rect.x 和 self.rect.y 来更新和跟踪球的位置。不要将位置同时保存在 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

【讨论】:

  • 非常清晰简洁的解释,我现在明白了。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-07
  • 1970-01-01
  • 1970-01-01
  • 2019-11-10
  • 1970-01-01
  • 2021-11-27
相关资源
最近更新 更多