Reputation: 13
The goal of this program is to make a green circle bounce up and down on the screen in a loop. I have made it go down and back up again, but not to make it go down again.
import pygame, sys, time, random
from pygame.locals import *
from time import *
pygame.init()
windowSurface = pygame.display.set_mode((500, 400), 0, 32)
pygame.display.set_caption("Bounce")
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
info = pygame.display.Info()
sw = info.current_w
sh = info.current_h
y= 0
yy= sh
while True:
windowSurface.fill(BLACK)
pygame.draw.circle(windowSurface, GREEN , (250,y), 13, 0)
sleep(.006)
y+= 1
if y>sh:
pygame.draw.circle(windowSurface, GREEN , (250,yy), 13, 0)
sleep(.0001)
yy +=-1
if yy<-.00001:
y=0
y+= 1
pygame.draw.circle(windowSurface, GREEN , (250,y), 13, 0)
pygame.display.update()
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
Upvotes: 1
Views: 3127
Reputation: 1974
A easy fix you can add is a counter variable so every time you ball changes directions the variable
+=1
so after a set number of changes the program stops. Assuming thats what you want it to do.
Upvotes: 0
Reputation: 58
Use a while loop for your ball to bounce. It should loop the code over and over again. What happens when it doesn't go down a second time? Does it throw an error? Does it just freeze?
Upvotes: 0
Reputation: 2534
In your if yy < -.00001:
block, I think you're trying to reset the state back to how it was at the beginning of the program. If that's the case, you forgot to set yy = sh
.
In other words:
if yy<-.00001:
y=0
yy=sh # <- add this line
y+= 1
pygame.draw.circle(windowSurface, GREEN , (250,y), 13, 0)
In general though, I agree with halflings. This code is way too complicated. I'd suggest using a state variable to record whether the ball is moving up or down, then in the loop check that variable and either increment or decrement the position. When you get to the other end of the screen, swap the state variable to change the direction of the ball.
EDIT: Try this:
import pygame, sys, time, random
from pygame.locals import *
from time import *
pygame.init()
windowSurface = pygame.display.set_mode((500, 400), 0, 32)
pygame.display.set_caption("Bounce")
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
info = pygame.display.Info()
sw = info.current_w
sh = info.current_h
y = 0
# Initial direction is down
direction = 1
while True:
windowSurface.fill(BLACK)
pygame.draw.circle(windowSurface, GREEN , (250,y), 13, 0)
#print "Drawing at 250,", y
sleep(.006)
y += direction
if y >= sh:
# Changes the direction from down to up
direction = -1
elif y <= 0:
# Changes the direction from up to down
direction = 1
pygame.display.update()
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
Note: Hopefully this runs for you. I don't have pygame installed so I've only run it with the pygame stuff commented out and print
calls substituted instead of draw commands. It works for me, anyway.
Upvotes: 3
Reputation: 1538
Your code seem way too complicated for the animation you're trying to do.
What you're looking for to simulate a "bouncing effect" may be the sin function (as it ossilates between -1 and 1) :
t = 0
speed = 0.01
maxHeight = 200
while True:
windowSurface.fill(BLACK)
pygame.draw.circle(windowSurface, GREEN , (250,y), 13, 0)
sleep(.006)
t += 1
y = int( ( sin(t*speed) + 1) * maxHeight / 2 )
pygame.draw.circle(windowSurface, GREEN , (250, y , 13, 0)
pygame.display.update()
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
Do not forget to put from math import sin
at the top of your code.
If you're trying to achieve something realistic, you should probably use the speed and acceleration of the ball instead of using the 'sin' function to simulate a bouncing effect.
Upvotes: 2