Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pygame Movement X/Y coord
#1
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
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 
The 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()
Reply
#2
only this is what is changing your velocity.
self.dx = self.dx * -1
and it only executes when self.x is below 0 or above 1000, so after it moves the first frame back under 1000, this does not execute again until it hits 0. Vice-versa the when it hits 0, etc.

A positive number will result in a negative number, a negative number will result in a positive number.
>>> 3 * -1 #hit right wall
-3
>>> -3 * -1 #hit left wall
3
3 or -3 gets added to x which moves the x position in the opposite direction of the wall and will continue to do so until it hits the next
>>> 1000 + -3 #hit right wall
997
>>> 0 + 3 #hit left wall
3
It doesnt matter what the number is (0 or 1000). It will always do this. However then it would look odd and not like it is hitting the wall. Which in your case is happening. This is due to objects drawn are in relation to the top-left corner. Your self.x is the left side. It would be better to detect collision using pygame rects. You can simply do
if mike.right > 1000 or mike.left is < 0:
because your object is moving past the screen. This would ensure that when the right side of the object hits the right screen, that it will reverse direction....and not go past it.
Recommended Tutorials:
Reply
#3
Thank you -SO- much for the help. You're very good at what you do and I am grateful for the time you took to write this response. So what I'm seeing is that when it hits the right edge, the game, since the self.dx = -3 will continue to go down so... 997,994, 991 until it hits 0. Then on the left it'd turn it into a positive number. I SEE it now. Now I can return to my work and struggle through it again.

You even added a fix for why the right edge was looking wonky. I'll make the necessary changes but thank you again.
PS. I tried to find where to like your post but couldn't find it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Laggy Game Movement game_slayer_99 12 4,220 Oct-05-2022, 11:34 AM
Last Post: metulburr
  [PyGame] Particle movement mystery XavierPlatinum 10 2,872 Jul-09-2022, 04:28 AM
Last Post: deanhystad
  [PyGame] Isometric Movement on Tiled Map Josselin 0 2,325 Nov-02-2021, 06:56 AM
Last Post: Josselin
  [PyGame] object's movement to left leave shadow on the screen Zhaleh 3 3,082 Aug-02-2020, 09:59 PM
Last Post: nilamo
  Using classes for room movement (text game) Lawr3y 3 6,484 Aug-20-2019, 12:40 AM
Last Post: Lawr3y
  Problem with coding for movement keys Aresofthesea 3 3,429 Jul-05-2019, 07:05 PM
Last Post: nilamo
  Movement after KEYUP, only after pause func esteel 2 3,249 Aug-22-2018, 03:03 PM
Last Post: Windspar
  [PyGame] How to stop the sprite's movement when it touches the edges of the screen? mrmn 5 11,451 May-13-2018, 06:33 PM
Last Post: mrmn

Forum Jump:

User Panel Messages

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