[PyGame] I have a problem about using OOP I guess :S - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Game Development (https://python-forum.io/forum-11.html) +--- Thread: [PyGame] I have a problem about using OOP I guess :S (/thread-14273.html) |
I have a problem about using OOP I guess :S - osmanb06 - Nov-22-2018 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? RE: I have a problem about using OOP I guess :S - inarma - Nov-23-2018 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. RE: I have a problem about using OOP I guess :S - osmanb06 - Nov-26-2018 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. |