Reputation: 35
When calling ball.update()
I expected the ball should move, but it didn't.
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 = 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:
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.
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()
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)
Upvotes: 1
Views: 88
Reputation: 2790
The problem is not that you are using a rect for bpos
per say. The problem is that you do not update bpos
.
The rect you use for drawing the ellipse needs to be updated in your Ball.update()
. You update the self.x,y but not the thing you use to draw the ball.
However, the rect that you use should be a attribute of the Ball instance, not an external variable. You should initialize it in the init and use the self.rect.x and self.rect.y to update and track the balls position. Do not keep the position both in a self.rect.x,and self.rect.y and separately in a 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
Upvotes: 1