[英]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.