A simple Python app is behaving erratically for no reason? -
I am going through some Python tutorials using Python 2.7 and PyGame, and I have decided to challenge myself. The tutorial showed how to make a ball move (right) on the screen, then pop back to the other (left) side of the screen at a particular speed I wanted to push the ball forward and back from left to right, so I wrote it: Import
pygame.init () screen = pygame.display Set_mode (816,460), 0,32) background = pygame.image.load (bif) .convert () ball = pygame.image.load (mif) .convert_alpha () x = 0 clock = pygame.time.Clock () While true: pygame.event.get () For event in: if event.type == QUIT: pygame.quit () sys.exit () screen. Blilt (background, (0,0)) screen. Blit (ball, (x, 160)) speed = 500 milli = clock.tick () # a tick 1 millisecond seconds = ml / 1000.000000 DM = second * speed if x == 0: a = DM elif x == 770: A = -dm x + = a pygame.display.update ()
"bg.jpg" is a JPEG image which is 816 x 460 pixels and "BIF." There is a PNG image of a ball with a 50 pixel radius. Rather than moving forward and moving 500 pixels per second, the ball moves at a random speed to the right, then bounces on the left side of the screen at the right side, and it repeats a random number. Then the ball is going in one direction and does not come back. I can not understand why this is doing this every time I run it, behaves differently. If someone came to know why I really am grateful.
The time has passed since the last call from the tick. Depending on your speed in your use, which is always different, each time you get different speeds. Change it to
speed = 500 from the end:
speed = 1 if x == 0 or x == 770: speed = - Speed x + = speed pygame.display.update () clock.tick (60)
Comments
Post a Comment