0

I am trying to code a paddle that hits a ball with pygame, and this is my first time using classes in a game: could someone tell me if I am on a wrong track with the code, so if it is too redundant and poorly structured.Plus, I have a bug.When the ball is hit with the lateral side, the ball gets a speed that is so high that after a few seconds the game crashes.I am not asking to break down my code but just some tips or warnings or anything that makes sense.Thank you =)

import pygame



class Ball:

    def __init__(self,posx,posy,wid,hei,speedx=3,speedy=5):
        self.posx = posx
        self.posy = posy
        self.wid = wid
        self.hei = hei
        self.color = None
        self.speedx = speedx
        self.speedy = speedy
    def draw_rectangle(self):
        ball.color = 'Red'
        rect = pygame.Rect(ball.posx,ball.posy,ball.wid,ball.hei)
        pygame.draw.rect(App.screen,ball.color,rect)
    def moving_ball(self):
        ball.posx += ball.speedx
        ball.posy += ball.speedy
        if (ball.posx+ball.wid)>width or ball.posx<0:
            ball.speedx *= -1
        if  (ball.posy + ball.hei)>height or ball.posy<0:
            ball.speedy *= -1
ball = Ball(200,200,30,30)



class Block:
    pass

class Player:
    def __init__(self,x,y,widdy,heighty,velx=5,color=(255,255,255)):
        self.x = x
        self.y = y
        self.widdy = widdy
        self.heighty = heighty
        self.color = color
        self.velx = velx
    def draw_player(self):
        recty = pygame.Rect(table.x,table.y, table.widdy, table.heighty)
        pygame.draw.rect(App.screen,table.color,recty)
    def control_player(self):
        keyspressed = pygame.key.get_pressed()
        if keyspressed[pygame.K_a] and table.x>table.velx:
            table.x -= table.velx
        if keyspressed[pygame.K_d] and table.x<(width-table.velx-table.widdy):
            table.x += table.velx
    def hit(self):
        if (ball.posx>table.x-30) and ((ball.posx+ball.wid)<(table.x+table.widdy)+30) and ((ball.posy+ball.hei)>table.y):

            '''three different areas three different bounces'''
            if (ball.posx + ball.wid)>table.x and (ball.posx + ball.wid)<table.x+20:
                ball.speedx *= -1.3
                ball.speedy *= -1.05
            if (ball.posx + ball.wid) > table.x+20 and (ball.posx + ball.wid) < table.x + 40:
                ball.speedx *= -1.3
                ball.speedy *= -1
            if (ball.posx + ball.wid) > table.x +40 and (ball.posx + ball.wid) < table.x + 60:
                ball.speedx *= -1.1
                ball.speedy *= -1
            if (ball.posx + ball.wid) > table.x + 60 and (ball.posx + ball.wid) < table.x + 80:
                ball.speedx *= 0.8
                ball.speedy *= -1.05
            if (ball.posx + ball.wid) > table.x + 80 and (ball.posx + ball.wid) < table.x + 100:
                ball.speedx *= 1.2
                ball.speedy *= -1.05
            if (ball.posx + ball.wid) > table.x + 100 and (ball.posx)>table.x + 70:
                ball.speedx *= 1.2
                ball.speedy *= -1.0



table = Player(200,480, 100,20)
class App():
    def __init__(self):
        pygame.init()
        global width,height,FPS,lives
        width = 500
        height = 500
        FPS = 60
        App.running = True
        App.screen = pygame.display.set_mode((width, height))
        pygame.display.set_caption("Bouncy ball")
    def run(self):
        lives = 3
        while App.running:
            clock = pygame.time.Clock()
            clock.tick(FPS)
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    App.running = False

            if ball.posy+ball.hei>height:
                lives-=1
                print(lives)
            if lives <=0:
                print("game over")
            App.screen.fill((0,0,0))
            ball.draw_rectangle()
            ball.moving_ball()
            table.draw_player()
            table.control_player()
            table.hit()
            pygame.display.update()

        pygame.quit()
app = App()





if __name__ == '__main__':
        App().run()



Alex D'ago
  • 122
  • 6

0 Answers0