Tuesday, January 21, 2014

X

     So, last time, we created a Pygame program. Uh, but it's just a black screen. And it stays open for a mere second. Not even the X button works. So, today we're trying to get X to work. How does that work? Let's just find out.
     A standard module of Python known as sys is our key. A function from sys is sys.exit(). "No questions asked. Just close the window." So, this is how we're going to use it.

import pygame, sys
pygame.init()
screen=pygame.display.set_mode([640, 480])
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

     Wow, just getting a black screen to be able to close takes a lot of code. So, not only did we do that, but now we can drag the screen around without having it crash! But, well, it will crash if you hit the X button. Man, that icon is hard to work with!
     Oh well... Next time, we're getting into the gooder--I mean better--stuff. We're going to do graphics.

Wednesday, January 1, 2014

Print("Hello") to Pygame!

     Have you had fun reading these articles and creating basic games over the course of 2013? I hope so, because my New Years resolution is to make things a lot more entertaining! Print("Hello") to graphics!
     Not only graphics, but getting sound as well is difficult. It has to do with operating systems, graphics cards, and a lot of other random things you most likely don't know about. Don't worry, though. Remember last article, when I talked about downloading standard modules? I have (a safe) one here, at pygame.org!
     Just so you know, Pygame doesn't run on Python 3.3.0. You'll have to find a version of Python that supports it. And me? I'm using Python 2.5. Also, Pygame needs to be supported by Numeric, which can also be found on the site.
     Alright, I hope everything's cleared up. Type this in the new window of your new Python IDLE window.

import pygame
pygame.init()
screen=pygame.display.set_mode([640, 480])

     If you run this program, you should get a window that's just a black screen for a second.


     Either that, or it crashes instantly.
     Games aren't independent. They interact with the user. Pygame is meant for creating games. But, it has something called event loops. They constantly check for things the user does, such as jamming on the keyboard or flinging the mouse. The "game" we just made had no event loop started in the first place. The Pygame window stays open while the program is running. Let's keep it running. Add this to the bottom of the program.

while True:
    pass

     You see, True means... True, and pass means... Do nothing. This may seem confusing at first. Seeing the entire program might help.

import pygame
pygame.init()
screen=pygame.display.set_mode([640, 480])
while True:
    pass

     So, you might have found out by now. This basically means, "while the window is open, do NOTHING." So, it does nothing. That means it doesn't even close or crash. So, let's run it. Remember, you can use CTRL+C to close it (as long as IDLE is the active window).


     It didn't crash that time! Alright! We're getting somewhere! But, try running the program again. What happens when you press that X button in the corner, you know, the one that closes every window?


     Not again! We've got to make that thing work.
     I suppose we'll discuss X next article. See ya!