Math problem with Pygame
I everyone! I'm having a little math trouble with a simple physics program I'm writing in python w/ pygame. If you notice in the code where it says: "((-ballSpeed[1]/4*3))", my ball on the screen just sits there and does nothing!! Whats wrong with my program?
import sys
import pygame
SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480
BLACK = (0, 0, 0)
ballSpeed = [0,4]
pygame.init()
screen = pygame.display.set_mode( (SCREEN_WIDTH,SCREEN_HEIGHT) )
ballImage = pygame.image.load( "box.bmp" )
ballRect = ballImage.get_rect()
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
pygame.display.set_caption("Testing! "+str(ballSpeed[0])+str(ballSpeed[1]))
ballRect = ballRect.move( ballSpeed )
if ballRect.left < 0 or ballRect.right > SCREEN_WIDTH:
ballSpeed[0] = -ballSpeed[0]
if ballRect.top < 0 or ballRect.bottom > SCREEN_HEIGHT:
ballSpeed[1] = ((-ballSpeed[1]/4*3))
screen.fill( BLACK )
screen.blit( ballImage, ballRect )
pygame.display.flip()
besktrap
Junior Poster in Training
58 posts since Jul 2008
Reputation Points: 10
Solved Threads: 1
Gribouillis
Posting Maven
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
Ok so I changed the code (I forgot to remove to second pair of parenthesis. I don't think there is a difference between one or two pair of parenthesis):
ballSpeed[1] = (-ballSpeed[1]/4*3)
print ballSpeed[1]
So this code lets me view the value of ballSpeed[1].
pygame.display.set_caption("Testing! "+str(ballSpeed[0])+str(ballSpeed[1]))
This code let's me see the value of ballSpeed[0] and ballSpeed[1] in the caption bar (a.k.a the window bar)
Although, I when using the "print ballSpeed[1]" code, I stopped the command line scroll bar to slowly let me see the outcome of "ballSpeed[1]". I noticed the very first number that was recorded (the first time the ball hit the floor) was -3!! That's the number I wanted...but then it just goes back to 0's. I have an image right here ( http://i510.photobucket.com/albums/s342/besktrap/-3.jpg )
besktrap
Junior Poster in Training
58 posts since Jul 2008
Reputation Points: 10
Solved Threads: 1
may be its because of this:
>>> print 3/4*3
0
Gribouillis
Posting Maven
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
Gribouillis
Posting Maven
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
Also
ballSpeed = [0,4]
means move the ball 0 pixels along the x axis and 4 pixels along the y axis. Pixels have to be integers. When you set it to [0,0] it stops of course.
Ene Uran
Posting Virtuoso
1,723 posts since Aug 2005
Reputation Points: 625
Solved Threads: 213
Wow, why is Python so specific about the 3 being a float in order for it not to round the answer? Thanks though Gribouillis, as I now learned something new today :)
Most computer languages consider integer/integer an integer division and for instance 1/2 will give you 0. Conversely float/integer or integer/float will be considered a floating point division, such that 1.0/2 will give 0.5
The new version 3.0 of Python will depart from this traditional concept and make / a floating point division and // an integer division.
Ene Uran
Posting Virtuoso
1,723 posts since Aug 2005
Reputation Points: 625
Solved Threads: 213
I changed my code from "ballSpeed = [0,4]" to "ballSpeed[0.0,4.0]". It works for the first millisecond (changing 4 to -3), but then turns into 2.25, -1.6875, ect. Link to image.
The ball goes up, but then all of the other numbers take over to control the ball! Ahhhh!!
besktrap
Junior Poster in Training
58 posts since Jul 2008
Reputation Points: 10
Solved Threads: 1
Heh, whoops. Tequila makes you do funny things.
jlm699
Veteran Poster
1,112 posts since Jul 2008
Reputation Points: 355
Solved Threads: 292
Read the first page of posts.
besktrap
Junior Poster in Training
58 posts since Jul 2008
Reputation Points: 10
Solved Threads: 1
I think you should modify your test like this
if (ballRect.left < 0 and ballSpeed[0] < 0) or (ballRect.right > SCREEN_WIDTH and ballSpeed[0]>0):
ballSpeed[0] = -ballSpeed[0]
if (ballRect.top < 0 and ballSpeed[1] < 0) or (ballRect.bottom > SCREEN_HEIGHT and ballSpeed[1]>0):
ballSpeed[1] = ((-ballSpeed[1]/4*3))
so, that if the ball is above the top of the window, you change the sign of the velocity only if the ball is going upwards, and if it's below the bottom of the window, you reverse velocity only if it's going downwards, etc. Tell us if it works.
Gribouillis
Posting Maven
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
The first part of the code looks fine, but the math still doesn't work right. Link.
I though of just doing the math without multiplication; and I'm trying to also add a bounce effect:
# Gravity Part of the code
if ballRect.bottom == 50:
a = 0
ballSpeed[1] = (ballSpeed[1] + 1)
if ballRect.bottom == 180:
a = 0
ballSpeed[1] = (ballSpeed[1] + 1)
if ballRect.bottom == 260:
a = 0
ballSpeed[1] = (ballSpeed[1] + 1)
if ballRect.bottom == 340:
a = 0
ballSpeed[1] = (ballSpeed[1] + 1)
if ballRect.bottom == 380:
a = 0
ballSpeed[1] = (ballSpeed[1] + 1)
if ballRect.bottom == 440:
a = 0
ballSpeed[1] = (ballSpeed[1] + 1)
A = 0 could be used to identify if the ball is going down, or if it is bouncing.
besktrap
Junior Poster in Training
58 posts since Jul 2008
Reputation Points: 10
Solved Threads: 1
I think you shoudn't add new features before the first effect work. I don't understand why your speed is multiplied by 3/4 at each loop (at least if you implemented the tests as I said before).
I don't have pygame here, so I cant test your code :)
Gribouillis
Posting Maven
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
I'm trying to make it multiply by 3/4 every time the ball hits the bottom of the screen, but it just loops on that if statment.
besktrap
Junior Poster in Training
58 posts since Jul 2008
Reputation Points: 10
Solved Threads: 1
By the way I think you should exchange ballRect.top and ballRect.bottom in the tests.
Gribouillis
Posting Maven
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
Did you modify the tests the way I told you ?
Gribouillis
Posting Maven
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
I just tested the code from the first page and after creating my own "box.bmp" in MSPaint I was able to observe the box "bounce" from the top of the screen to the bottom and back again before finally coming to a stop at the bottom of the window.
Looks like the "speed" value doesn't like anything below 1.0?
jlm699
Veteran Poster
1,112 posts since Jul 2008
Reputation Points: 355
Solved Threads: 292