python - Why isn't the ball bouncing back? -


i'm making simple pong game, when ball drops ceiling, doesn't bounce , forth. goes off screen. can't figure out why happening! concerned ball bouncing off top , bottom of screen , want bounce , forth in straight path. appreciated!

edit: i've found problem! help!

here's base code:

import math import random import sys, pygame pygame.locals import *  import ball import colors import paddle  # draw scene def draw(screen, ball1, paddle1) :    screen.fill((128, 128, 128))    ball1.draw_ball(screen)    paddle1.draw_paddle(screen)  #function start main drawing def main():     pygame.init()    width = 600    height = 600    screen = pygame.display.set_mode((width, height))     ball1 = ball.ball(300, 1, 40, colors.yellow, 0, 5)    paddle1 = paddle.paddle(250, 575, colors.green, 100, 20)     while 1:       event in pygame.event.get():          if event.type == quit: sys.exit()          elif event.type == pygame.keydown:             if event.key == pygame.k_right:                paddle1.update_paddle('right', 20)             if event.key == pygame.k_left:                paddle1.update_paddle('left', 20)        ball1.test_collide_top_ball(600)       ball1.test_collide_bottom_ball(0)       ball1.update_ball()       draw(screen, ball1, paddle1)       pygame.display.flip()  if __name__ == '__main__':    main() 

and here code ball class/methods:

import pygame  class ball:    def __init__(self, x, y, radius, color, dx, dy):       self.x = x       self.y = y       self.radius = radius       self.color = color       self.dx = dx       self.dy = dy     def draw_ball(self, screen):       pygame.draw.ellipse(screen, self.color,          pygame.rect(self.x, self.y, self.radius, self.radius))     def update_ball(self):       self.x += self.dx       self.y += self.dy     def test_collide_top_ball(self, top_height):       if (self.y >= top_height):          self.dy *= -1     def test_collide_bottom_ball(self, coll_height):       if (self.y >= coll_height):          self.dy *= -1 

your test collide functions return value of velocity. never use anywhere.

you call update ball dx=0 dy=5.

instead of returning value after collision, better hold dx , dy in object. become:

class ball:    def __init__(self, x, y, radius, color):       self.x = x       self.y = y       self.radius = radius       self.color = color       self.dx = 0       self.dy = 5     def draw_ball(self, screen):       pygame.draw.ellipse(screen, self.color,          pygame.rect(self.x, self.y, self.radius, self.radius))     def update_ball(self):       self.x += self.dx       self.y += self.dy     def test_collide_top_ball(self, top_height):       if (self.y >= top_height):          self.dy *= -1     def test_collide_bottom_ball(self, coll_height):       if (self.y >= coll_height):          self.dy *= -1 

Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

c# - Unity IoC Lifetime per HttpRequest for UserStore -

I am trying to solve the error message 'incompatible ranks 0 and 1 in assignment' in a fortran 95 program. -