Sunday, November 2, 2014

Bouncing

     Looks like I missed an article on Halloween... I've been trying to write an article but other stuff keeps happening... Oh well. I probably wouldn't be able to find a good Halloween theme for this topic, anyway.
     Welp, here's today's! Hopefully you aren't caught up by the time you read this.
     Anyway, I hope you still have the picture of that ball in your Python folder--we're using it some more! As you can see from the title of the article, we are going to "bounce the ball."
     Secretly, objects cannot make contact with the sides of the window, so you just have to determine the edge of the screen. For fun, I am going to make the ball move up and down this time (and don't forget, its picture size is 150 x 150 pixels!). In order to allow the ball to change directions, it's best to use a variable. Another thing you shouldn't forget is that when you increase the Y axis, the image goes downward. But, enough chit-chat, let's finally get down to business!
     We already have a lot of it done already from the previous article. All we have to do is change the x = x + 5 into y + [yvariable]. In order to do this, we will add a new variable at the top of the code (outside of the block). I will be calling mine yspeed, and its value will be 5. This variable will make the speed easily managable if we ever need to change it (and we will).
     Now, take the x = x + 5 line and change it to y = y + yspeed (or whatever your variable is). Now, here's a word of advice: there is a function called screen.get_height() (as well as screen.get_width()). This is an easy way to... well, it's obvious. it finds the screen's height and stores it as a number for the following code it ties into. Inside our True block, we will add this function in an "if" block by typing, "if y > screen.get_height() - 150 or y <= 0:"
     Inside this line, we will say "yspeed = - yspeed," turning the 5 into a negative 5 (because, you know, of the -). The "y <= 0" part will check if the number representing y is above or equal to the number 0 (the top of the screen) and reverses its sign again, turning it back into a positive.
     Keep in mind that the y variable is only a number, not actually the screen's height. That's pretty confusing, but since y always the same number as the ball's position, it works.
     So, long story short, the entire program should look like this:

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() - 150 or y < 0:
        yspeed = - yspeed
    screen.blit(ball, [x, y])
    pygame.display.flip()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

     When you run the program, the ball should start moving up & down, never leaving the screen. It may not be a huge game produced by a popular corporation, but, hey, it's still pretty cool! We're finally getting somewhere!
     Well, that's all there is about bouncing. See you next time!.. Should I find the time and inspiration to write the next article! Until then, I challenge you to make the ball bounce diagonally! Heres a tip: The screen.get_width() and screen.get_height() functions find the position of the right and bottom sides of the screen.
     Here's something for an extra challenge: Try to get the ball to appear on one side of the screen when it reaches the other! You can just have it move vertically or horizontally if you'd like.