Mar-19-2019, 03:29 AM
(This post was last modified: Mar-19-2019, 03:30 AM by TheHumbleIdiot.)
Hello. Well, essentially I'm following this book and so far I was understanding the fundamentals for PYGAME. The thing that tripped me up is when you're moving an icon and try to bounce it across the screen or to reverse the order. The code involved is like this.. Basically, the problem in my understanding involves the
998 = 998 + 3 = 1001
then it continues and continues. I'm just so confused. What's the math involved allowing you to reverse the order? I just don't see it.
if self.x < 0 or self.x > 1000: self.dx = self.dx * -1 # 3 = 3 * -1 = -3/ 1001 + self.x += self.dx #1001 = 1001 + -3 = 998/ 998 = 998 + 3/ self.y += self.dyThe math doesn't really seem to add up. After you hit 1000, what's allowing you to go back to 0?
998 = 998 + 3 = 1001
then it continues and continues. I'm just so confused. What's the math involved allowing you to reverse the order? I just don't see it.
import pygame from pygame import * import sys import random pygame.init() screen = pygame.display.set_mode((1000,2000)) title = pygame.display.set_caption("Rain") clock = pygame.time.Clock() mike_image = pygame.image.load("Mike_umbrella.png").convert() rain_drop_time = 0 class Mike: def __init__(self): self.x = random.randint(0,570) self.y = -45 self.dy = 1 self.dx = 3 def move(self): if self.x < 0 or self.x > 1000: self.dx = self.dx * -1 # 3 = 3 * -1 = -3/ 1001 + self.x += self.dx #1001 = 1001 + -3 = 998/ 998 = 998 + 3/ self.y += self.dy def draw(self): screen.blit(mike_image,(self.x,self.y)) mike = Mike() while True: for event in pygame.event.get(): if event.type == QUIT: sys.exit() screen.fill((255,255,255)) mike.draw() mike.move() pygame.display.update()