Python Forum
[PyGame] I have a problem about using OOP I guess :S
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] I have a problem about using OOP I guess :S
#1
import pygame
import time

pygame.init()

display_width = 600
display_height = 400

gameDisplay = pygame.display.set_mode((display_width,display_height))

white = (255,255,255)
red = (255,0,0)

clock = pygame.time.Clock()

gameExit = False

class Ball:
    def __init__(self,x,y,x_update,y_update):
        self.x = x
        self.y = y
        self.g= -10
        self.t = 0.0001
        self.x_update = x_update
        self.y_update = y_update

    def move(self):
        self.x += self.x_update
        self.y_update = self.y_update - (self.g * self.t)
        vy = self.y_update * self.t + (0.5 * self.g*(self.t**2))
        self.y += vy

        if self.x > display_width - 15 or self.x < 15:
            self.x_update *= -1
        if int(self.y+10)+self.y > display_height:
            self.y_update *= -1

        pygame.draw.circle(gameDisplay,red,[self.x,int(self.y)],10,10)

ball = Ball(300,200,5,0)
while not gameExit:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            gameExit = True
            
    gameDisplay.fill(white)
    ball.move()
    pygame.display.update()
    clock.tick(30)

pygame.quit()
quit()
I wanted to create gravity and a bouncing ball into this gravity.But I encountered a problem.Problem about ball's height.When ball was came the program's bottom border,bouncing ball's center as I far as I can see.How can I fix this problem?
Reply
#2
import pygame
import time
 
pygame.init()
 
display_width = 600
display_height = 400
 
gameDisplay = pygame.display.set_mode((display_width,display_height))
 
white = (255,255,255)
red = (255,0,0)
 
clock = pygame.time.Clock()
 
gameExit = False
 
class Ball:
    def __init__(self,x,y,x_update,y_update):
        self.x = x
        self.y = y
        self.g= -10
        self.t = 0.1
        self.x_update = x_update
        self.y_update = y_update
 
    def move(self):
        self.x += self.x_update
        self.y_update = self.y_update - (self.g * self.t)
        vy = self.y_update * self.t + (0.5 * self.g*(self.t**2))
        self.y += vy
        print("y={} y_update={} vy={}".format(int(self.y),int(self.y_update),int(vy)))

        if self.x > display_width - 15 or self.x < 15:
            self.x_update *= -1
        if self.y > display_height-15 or self.y<15:
            self.y_update *= -1
            print("reversing direction")
        
 
        pygame.draw.circle(gameDisplay,red,[self.x,int(self.y)],10,10)
 
ball = Ball(300,200,5,10)
while not gameExit:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            gameExit = True
             
    gameDisplay.fill(white)
    ball.move()
    pygame.display.update()
    clock.tick(30)
 
pygame.quit()
quit()
Is this what you wanted? I think you mainly needed to change your t variable because the one you were using was verryyyy slow.
Reply
#3
Thank you so much :) But I have a question for your code.Ball is reversing direction ago about 2 px of border height.How can fix ?
I have solved my second problem.

if self.x > display_width - 15 or self.x < 15:
            self.x_update *= -1
        if self.y > display_height-(10+vy) or self.y <10:
            self.y_update *= -1
            print("reversing direction")
As you can see on here(this is edited code),ball's ordinate must be bigger than display's height - (radius of ball(10)(px) + vertical velocity(vy)) and the exact opposite.
I am sorry my english by the way.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020