import pygame, sys
import time
pygame.init()
screen=pygame.display.set_mode([640, 480])
screen.fill([255, 255, 255])
ball = pygame.image.load("Ball.png")
x=50
y=50
yspeed=5
screen.blit(ball, [x, y])
pygame.display.flip()
while True:
pygame.time.delay(20)
pygame.draw.rect(screen, [255, 255, 255], [x, y, 150, 150], 0)
y = y + yspeed
if y > screen.get_height():
y = -150
screen.blit(ball, [x, y])
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
This makes it so that if the y position goes past the bottom of the screen, it reappears 150 pixels (the ball's height) above the screen and sink back down again. This is nice and all, but the ball completely disappears before reappearing up top. That's when I started thinking, "What if you want to make a game with a topview but do not want players to hide freakin' off screen?" I, my friend, spent countless minutes to figure out the answer, but I have discovered it.
The trick I found is that we have to add TWO balls. This way, when a ball starts going off of the bottom of the screen, the other one starts appearing up top, so it's half of a ball on the bottom, half on the top.
Why don't you think about how to use this idea for a bit. If you want to be a programmer, you have to REALLY think and do your own things. Try tinkering with the code for a while and see what you come up with.
Done yet?
Okay. Well, I'll show you my thought process, step by step.
To start off, the 2nd ball has to be at a different height than the 1st one. The screen is 480 pixels tall, so that's how far apart they need to be at all times (because half on top, half on bottom? there is a 480 pixel difference). Ball 1 is at position 50, so at 480 pixels above is ball 2 at position -430.
import pygame, sys
import time
pygame.init()
screen=pygame.display.set_mode([640, 480])
screen.fill([255, 255, 255])
ball = pygame.image.load("Ball.png")
x=50
y=50
yspeed=5
y2 = -430
screen.blit(ball, [x, y])
pygame.display.flip()
That will set a ball way offscreen, but it will show itself as ball 1 is leaving. Speaking of leaving, when they reach point 480 (the number retrieved from the get_height function), they have to reappear back up. At first, I just set their points to 430, but I noticed that the top ball came down before the bottom ball even actually hit the bottom. Fatal common sense mistake, when in the first part of the program, ball 2 is at point 0 when ball 1 is at point 480. They ALWAYS have to be 480 pixels apart, so the point they teleport to has to be -480.
(Also, don't forget to do all the normal functions you did to ball 1 to ball 2 as well.)
while True:
pygame.time.delay(20)
pygame.draw.rect(screen, [255, 255, 255], [x, y, 150, 150], 0)
pygame.draw.rect(screen, [255, 255, 255], [x, y2, 150, 150], 0)
y = y + yspeed
y2 = y2 + yspeed
if y > screen.get_height():
y = -480
if y2 > screen.get_height():
y2 = -480
screen.blit(ball, [x, y])
screen.blit(ball, [x, y2])
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
while True:
pygame.time.delay(20)
pygame.draw.rect(screen, [255, 255, 255], [x, y, 150, 150], 0)
pygame.draw.rect(screen, [255, 255, 255], [x, y2, 150, 150], 0)
y = y + yspeed
y2 = y2 + yspeed
if y > screen.get_height():
y = -480
if y2 > screen.get_height():
y2 = -480
screen.blit(ball, [x, y])
screen.blit(ball, [x, y2])
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
Now run that program. the balls should move perfectly. Also, you might be wondering, "But this is insufficient! What if a deadly object hits one ball offscreen?" Come on, use your head! You can turn off the ball's reaction to anything if it is not onscreen. And if you're skeptic if this really is timed perfectly, just try recording it with something and slowing it down. I did that to answer the question, and it is flawless. Your eyes are just messing with you, dude.
Well, that's all I have to say on the matter. See you next time! Until then, I challenge you to, of course, experiment. Also, I believe you now know enough about blitting that you can try creating some sort of animation! Just, try it if you want to.
Oh... I forgot to say something. Just so you know, the pygame.time.delay function measures time in milliseconds, so that's why I set it to 2,000 a couple articles ago. Just thought you should know that.
Oh... I forgot to say something. Just so you know, the pygame.time.delay function measures time in milliseconds, so that's why I set it to 2,000 a couple articles ago. Just thought you should know that.