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


Messages In This Thread
I have a problem about using OOP I guess :S - by osmanb06 - Nov-22-2018, 09:51 AM

Forum Jump:

User Panel Messages

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