Oct-30-2019, 05:31 PM
I've been practicing with classes a little with some code I found.
The original code was not in a class so that is my project to put it in one.
Every thing works but, I can't seem to get the ball to bounce off the wall on the x axis. They go behind the zero marker and the window length.
On a side note the ball kinda jumps a little too.
Any guidence would be great. Thanks
The original code was not in a class so that is my project to put it in one.
Every thing works but, I can't seem to get the ball to bounce off the wall on the x axis. They go behind the zero marker and the window length.
On a side note the ball kinda jumps a little too.
Any guidence would be great. Thanks
import pygame from random import randrange pygame.init() clock = pygame.time.Clock() win_size = [600, 400] #ball_color = (randrange(256), randrange(256), randrange(256)) ball_color = (255, 0, 0) ball_size = 20 #randrange(8, 20) pos = [0, 100] vel = [1, 1] class Ball: def __init__(self, ball_color, ball_size, pos, vel, win_size): self.ball_color = ball_color self.ball_size = ball_size self.pos = pos self.vel = vel self.win_size = win_size def move(self): self.pos[0] += vel[0] if self.pos[0] <= self.ball_size: self.vel[0] = self.vel[0] + 1 if self.pos[0] >= self.win_size[0] - self.ball_size: self.vel[0] = self.vel[0] - 1 self.pos[1] += self.vel[1] if self.pos[1] <= self.ball_size: self.vel[1] = self.vel[1] + 1 if self.pos[1] >= self.win_size[1] - self.ball_size: self.vel[1] = self.vel[1] - 1 def world(ball): win = pygame.display.set_mode((win_size[0], win_size[1])) win.fill((180, 180, 180)) pygame.draw.circle(win, ball_color, (pos[0], pos[1]), ball_size) ball.move() pygame.display.update() def main(): ball = Ball(ball_color, ball_size, pos, vel, win_size) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() world(ball) clock.tick(60) if __name__ == "__main__": main()