Learning Contract Step 7 Report: Graphics, Animations, Collision Detection, and Input

My learning contract states the following for Step 7:
    7.  Incorporating Graphics, Animation, Collision Detection, and User Input
        a.  Complete chapter 17
        b.  Complete chapter 18

Score: 10

Evaluation:  This step was HUGE!  I covered two chapters, both of which had two different programs in them with really new stuff.  That makes 4 programs to type in and dissect, as well as 4 programs to modify!  By the end of this week, I am pretty brain dead programmer!

In chapter 17, I was introduced to Pygame, a software library that can be used to create games with graphics, animations, and sounds.  I learned how to put basic graphics on a screen with Pygame, as well as how to make things move across the screen.  This was done by creating two programs, a new helloworld.py program that now has graphics in it, and an animation.py program.  Unfortunately, the textbook I am using didn't do a great job of explaining the different functions that are used in conjunction with Pygame in these programs, so I spent a lot of time during the first half of this chapter messing around with my helloworld.py program to see different parameters were taken in the functions, and what the different parameters did to the graphics.  I also looked up a few things online in the pygame documentation, which can be found here.  By the time I got to the animation.py program, I felt like I had a pretty good handle on the concepts used in this chapter. 

To use these programs, you first need to install Pygame onto your computer.  This can be done here.

I made the following modifications to my pygameHelloWorld.py program:

  • Changed the blue lines from forming a "Z" to forming my initials, "CW"
  • Moved the red ellipse
  • Added the purple star (as well as the color purple)
  • Added the "Made by Courtney" text at the bottom of the screen
  • Changed the font to use "comic sans" instead of the default font
  • Added in another font so that my two text boxes could be different sizes
Here is the source code my pygameHelloWorld.py program!

I made the following modifications to my animation.py program:
  • Slowed down the time (from 0.02 seconds to 0.03 seconds)
  • Added a purple rectangle (as well as the color purple)
  • Moved where the green square starts.  It originally started in the middle of the screen, and because of how much it moves each iteration and because it was a square, it just bounced back and forth from one corner to the next.  I wanted it to be able to actually bounce off walls, so I had it start a little off center. 
Here is the source code for my animation.py program!

In chapter 18 I learned about collision detection and about getting the user's input in a program in Pygame.  The first program I created, collisionDetection.py, taught me how to write a code to detect if two objects have collided by checking to see if the corners of one rectangle were inside of the other rectangle.  This program is the basic concept of having one big white rectangle, "the bouncer", that is moving around the screen and eating the smaller green rectangles, "the food".

I made the following modifications to my collisionDetection.py program:
  • Changed the bouncer from being square shaped to rectangular.  I did this because as I let the program run, I realized that the bouncer stayed in one path the whole time, and was never hitting the middle or the corners of the screen.  Thus, there was a build up of food in those areas.  By making it rectangular, it eventually covers the whole board. 
  • I added another bouncer to make it more interesting.
  • Because I added another bouncer, the "food" was getting eaten pretty fast and not replenished very fast, so I changed the number of NEWFOOD from 40 to 25 so it would add food on the screen faster.  
Here is the source code for my collisionDetection.py program!

The second program I created, pygameInput.py, taught me out to get the user's input in a program.  The program is a similar concept to collisionDetection.py, having a player move around the screen trying to eat the food, however the user can now interact with the program by moving the player.  Here are the movements in my game:
  • Move player up - hold the up arrow or the "w" key
  • Move the player down - the down arrow or the "s" key
  • Move the player to the left - the left arrow or the "a" key
  • Move the player to the right - the right arrow or the "d" key
  • Add more food - click on the screen (the game automatically adds food, but if you want more you can click)
  • Transport the player to a random spot on screen - press the "x" key
  • Quit the program - close out the window or press the "ESC" key
After finishing my program, I made two modifications.
  • I changed the color of the player to purple (so it is a little different than the last program)
  • I added a counter to the program to keep track of how many squares have been eaten.  This required adding text to the screen and adding a new variable, numFoodEaten, to track how many squares had been eaten. 
Here is the source code for my pygameInput.py program!
Comments