Monday, April 7, 2014

Rect

     Look, you may know how to draw circles, but that's not all you can draw in Python. You can also draw square shapes, A.K.A. rectangles. Of course, there is a function for this. This function is called rect (short for rectangle, of course). You define the rect by the area, starting from the top-left corner, in this order: left, top, width, height. This list defines the location and and size. Something like this: rect1=Rect(250, 150, 300, 200). 250 pixels to the right (starting from the top-left of the screen), 150 down, and the size is 300x200 pixels. Take the program that draws the circle and replace line 6 (I'm counting the blank line, so you should be on the line that actually draws the circle) with the code that I stated that draws the rectangle. The program should look like this.

import pygame, sys
pygame.init()
screen=pygame.display.set_mode([640, 480])
screen.fill([255, 255, 255])
pygame.draw.rect(screen, [255, 0, 0], [250, 150, 300, 200] 0)
pygame.display.flip()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

     And when you run it, you get a blank screen with a big, red block slapped on.

     Ta-da! That's how you make a rectangle. Hey, I might as well give you the steps to making a rectangle like what I did for doing a shape in general, in case you forget something or the order. Here is the list:

Location - Left-Right
This is the distance the rectangle will be from the left side of the screen.
Location - Top-Bottom
This is the distance the rectangle will be from the top of the screen.
Size - Width
How wide the rectangle will be.
Size - Height
How tall the rectangle will be.

     That's the end of this little bit on rectangles. Until next time!

Monday, March 17, 2014

Review

     So, remember, when we did the color of the circle, we typed "255, 0, 0"? Well, this is a color system used in most computer languages: R.G.B. It stands for Red, Green, & Blue. Simple. The max number you can have of one of those colors is 255, and the least is evidently 0. The higher the number of one color is, it will look more like that one. There are over 600 colors! Some are 255, 255, 255 (white), 255, 0, 0 (red), 0, 0, 255 (dark blue), 0, 255, 255 (light blue), 255, 0, 255 (pink), 0, 0, 0 (black), 0, 255, 0 (light green), and 255, 255, 0 (yellow). That's only some of the main ones. And that was quite a lot. You can try experimenting around with the colors and see what you find. If you know your color mixtures, then you should already have an idea of what colors you can figure out.
     Okay, off of the topic of color, let's go to coordinates. You might have learned about coordinates in math class, with the X axis (the horizontal line) and the Y axis (the vertical line going through the X axis). You use these same coordinates when placing something on the screen (remember, the X axis goes first, down the hall, and then the Y axis, up the stairs). Remember that (0, 0) starts at the top left corner of the screen. Well, if you have learned about coordinates in math, you would know that (0, 0) should be in the middle, but, apparently, this is one of the many programming mysteries of the world. So, remember, this: The X axis goes along the top of the window, and the Y axis will go down. Kick that knowledge of normal coordinates out and memorize those computer coordinates. With the size of the screen we always use, (320, 240) should be the middle.

import pygame, sys

pygame.init()
screen = pygame.display.set_mode([640, 480])
screen.fill([0, 255, 0])
pygame.draw.circle(screen, [255, 100, 0], [320, 240], 30, 0)
pygame.display.flip()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()


     Well, there's a code to verify. Let's see what happens when we run it.


     Ha! I bet you thought I forgot it was St. Patrick's Day! Nah, those colors are a sure sign I didn't. I wouldn't want you to track me down just so you could pinch me!
     We'll continue the review next time. I have to go slay an evil death dragon in a Pygame now. Bye!

Friday, February 14, 2014

Graphics: The Hollow Circle

     You guessed it--that's graphics. When you zoom in very, very far, you'll see a ton of tiny blocks. Well, of course, these are pixels, picture elements. Everything virtual has these, from the computer to the Wii.
     Let's make our own graphics!
     Well, we all know the black screen of black screens. Open that up and edit the program to this.


import pygame, sys
pygame.init()
screen=pygame.display.set_mode([640, 480])
screen.fill([255, 255, 255])
pygame.draw.circle(screen, [255, 0, 0], [100, 400], 30, 10)
pygame.display.flip()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()


     Colors. They're hard to explain. ([255, 255, 255]) and [255, 0, 0] is computer for white and red. Here's the 5 steps to creating a shape:
Surface
On what "surface" to draw the circle (in our case, screen).
Color
What color the circle will be.
Location
Where on the surface the circle will appear.
Size
Self-explanatory. The size (the radius measured by pixels).
Width
How thick the line is. If the thickness is 0, it will be a solid circle.
     Yeah, it's Valentines Day. What's the theme? Red and White are Valentine colors. That's all I got. It's not easy for something like this.
Remember, EVERYTHING is measured by pixels.
     I challenge you to experiment with the colors. Here's a tip: There are always 3 sets of numbers.

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!

Saturday, December 28, 2013

Python's Standard Library

     Have you ever heard someone say Python has batteries included? Have you wondered what the heck that means? Well, I can explain.
     Screw creating your own modules all the time, Python has standard ones built in! These standard modules let you find files, tell/count time, and much more. Remember everything we used with the import function? All of those are modules. The standard ones are the ones we never created in a different program! Should there be something you want to do that isn't a standard module, you should be able to find a download for them, hopefully for free (if you do that, be very careful, as it could be a virus or have viruses attached). So, what standard modules have we been using? Well, up until this point, you never knew you were doing the things in this article all along. The standard modules we know about so far are time and random.
     We already talked about time and random and how to use them, but there's a little more. With time.sleep(), you can make the program do nothing for a certain amount of time. There's a faster way to do it, though.

from time import sleep

     When you type that, it will make things a little faster to type, like this.

sleep(5)

     We do sleep(5) instead of time.sleep(5). We can do that because we imported sleep from time.
     There's also another thing to talk about with random. We all know about random.randint, the function we used in the fortune teller program, the multiplication one, and so on. We all know how random.randint works, but there's something else: random.random().
     random.random() will give you a random number between 0 and 1. Try it!

>>> import random
>>> print(random.random())
0.19406666194760636

     If you want a number that isn't between 0 and 1, you can multiply the answer. Let's try getting a number between 0 and 10.

>>> print(random.random() * 10)
8.2903170046567

     If you don't want a super specific float, you can use the int() function or the round() function, like this.

>>> print(int(random.random() * 10))
8

     The int function took our number and turned it into an integer!
     In this article, you learned about importing from a module, and random.random(). I challenge you to go to one of the old programs and change it with these methods!

Saturday, December 14, 2013

Namespaces

     There's more about modules. This topic is pretty complicated, it's on namespaces. What are they?
     Well, let's say you were in this school. We'll just simply call it School A. There is one person in your class (Class A) named Gerry, but in another class (Class B), someone else is named Gerry. Each class is its own namespace, and the students are inside of it.
     So, if you were to say "Gerry got glasses" in Class A, everyone would assume you mean the Gerry in that class' namespace. You would have to say something like "The other Gerry" for the class to know it's not the Gerry it has. If the principal calls Gerry to come to her office over the loudspeaker, she would get everyone with that name. She'd have to be specific, like saying the last name (i.e. Gerry Vargas) or saying which class the Gerry she wants is from. To make things easier, she could just go to the classroom and tell Gerry in person. The principal has to be specific which namespace has the Gerry she wants.
     So, let's move on to importing namespaces. Let's say a class from another school (Class C) has to teach in one of School A's portables for a while. If someone from Class C is named Harry, but no one from School A is, and the principal calls for Harry, no one will come. She'd have to hook up the loudspeaker to the portables, and to do that, we have to import the namespace (Class C). We could simply do this:

from ClassC import Harry

    Or, if we want to make things faster, we could just do

from ClassC import *
   
     Ta-da! * will automatically import everything in the namespace we chose (ClassC), and now the principal can call anyone from that class whenever she wants. She can just do callToOffice(Harry) or call someone else. Something like that, at least.
     I know, my explanation is a little unclear. We should be able to give my examples a nice shine later on.