Skip to content

Commit 679e43c

Browse files
committed
Merge pull request CoderDojoSV#1 from CoderDojoSV/feature/wip-day2
Add Day 2 material
2 parents 24bfcad + 93e985f commit 679e43c

File tree

9 files changed

+623
-1
lines changed

9 files changed

+623
-1
lines changed

.gitignore

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,10 @@ docs/_build/
6060
# PyBuilder
6161
target/
6262

63-
63+
### Vim ###
64+
[._]*.s[a-w][a-z]
65+
[._]s[a-w][a-z]
66+
*.un~
67+
Session.vim
68+
.netrwhist
69+
*~

Day2/.spaceinvaders.py.swp

12 KB
Binary file not shown.

Day2/README.md

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
#Installation
2+
3+
If you have Python and pygame already installed on your computer, you should be good to go! Otherwise, the easiest way to get started is to go to http://helloworldbookblog.com/tools/ and download the installer for your operating system. This will include everything you need. We will be using Python version 2.X.
4+
5+
If there are installation issues, http://repl.it/ can be used for the first week. It cannot be used for the second week though, because graphics and pygame are required.
6+
7+
# Review of Python Classes
8+
9+
A class can be thought of as a blueprint that you can use to create many copies of something.
10+
11+
Each "copy" of the class can have their own set of variables (called properties), however, each copy shares the same set of actions it can do (called methods).
12+
13+
Classes begin with the `class` keyword, followed by the class name. Classes can be "instantiated" by calling the class name:
14+
15+
class Person:
16+
name = ""
17+
18+
bob = Person()
19+
bob.name = "Bob"
20+
21+
### Example
22+
23+
The following is a class called Book
24+
25+
class Book:
26+
title = ""
27+
author = ""
28+
29+
As a warm up, type the class into Python and do the following:
30+
31+
1. Add a constructor to set the `title` and the `author` at the same time
32+
33+
2. Add a method to print out both the `title` and the `author`.
34+
35+
3. Create two books by different authors. Get each of them to print out their title and author.
36+
37+
# Project - Space Invaders!
38+
39+
We're going to use classes to make an arcade classic: Space Invaders!
40+
41+
## Running the code
42+
43+
Open up `spaceinvaders.py` using IDLE. Hit F5 or go to Run, Run Module to run the program.
44+
45+
You should see your green spaceship. You can move this spaceship by pressing and holding the left and right keys. You can shoot by pressing space. So far, there's not a lot to shoot. This isn't much fun, is it?
46+
47+
Over the course of this class, we'll implement aliens that you can shoot, and who will also shoot back at you!
48+
49+
## Code walkthrough
50+
51+
At the top of the code, we have two functions (not part of any class) that are used for detecting whether rectangles intersect. This is library code borrowed from the book Invent with Python.
52+
53+
We then define a screen size, initialize pygame, and create the screen that we're going to be using to implement the game
54+
55+
pygame.init()
56+
57+
SCREEN_WIDTH = 640
58+
SCREEN_HEIGHT = 480
59+
60+
screen = pygame.display.set_mode([SCREEN_WIDTH,SCREEN_HEIGHT])
61+
62+
This is followed by some arrays: `player_missiles` and `alien_missiles`. These arrays contain the missiles that the player and the aliens shoot at each other. At the moment, they're empty.
63+
64+
After that come our classes. The first class is our Spaceship. This class has three properties: an X position on the screen, a Y position, and a picture. In this case, we're going to load up `spaceship.png` from the directory that this file is in.
65+
66+
class Spaceship:
67+
xpos = 0
68+
ypos = 0
69+
picture = pygame.image.load('spaceship.png')
70+
71+
We then have a constructor that requires defining the x and y positions of the spaceship
72+
73+
def __init__(self, x, y):
74+
self.xpos = x
75+
self.ypos = y
76+
77+
We also have 5 methods:
78+
79+
1. `moveRight` - Moves the ship 5 pixels to the right
80+
2. `moveLeft` - Moves the ship 5 pixels to the left
81+
3. `shoot` - Creates a missile object with a position near the ship and adds it to the global player missile array
82+
4. `get_rect` - Gets the bounding rectangle for the player. This is used to calculate if the player got hit
83+
5. `draw` - Blits the player's picture at the right position on the screen
84+
85+
We also have classes for Aliens and Missiles. Notice that they are all very, very similar to one another. Hmm...
86+
87+
After the classes, we have an event loop that does the following things
88+
1. Detects key presses from the player and makes the player's ship do things
89+
2. Moves the player's misiles around and draws them
90+
3. Moves the aliens' missiles around and draws them (TO DO!)
91+
4. Moves the aliens around and draws them
92+
5. Detects whether all the aliens are gone, or if one has reached the bottom (TO DO!)
93+
94+
## Finishing up the basic game
95+
96+
At the moment, we need to make this a game! We'll need to add aliens, give them the ability to shoot, and add win/lose conditions for the player.
97+
98+
### Making aliens
99+
100+
We'll need to make some aliens to make the game interesting. In order to do this, we'll need to put some aliens in the alien array.
101+
102+
aliens = []
103+
# Make a grid of aliens
104+
# Your code here!
105+
106+
Make a 8 by 4 grid of aliens. Remember that the constructor for the Alien class requires an x and y position! (x = horizontal position, y = vertical)
107+
108+
Hint: Use for loops - one for the horizontal and one for the vertical direction
109+
110+
### Completing the Alien class
111+
112+
So, we've made a couple aliens, but if we run the program, we can't see them! Where are they?
113+
114+
This is because we don't have any code for the aliens' `draw` method. Let's complete the draw method!
115+
116+
Next up is the `shoot` method. It wouldn't be fun if the aliens couldn't shoot, right?
117+
118+
Finally, we'll need to tell the aliens when to shoot. Keep in mind that there are many of them, and the event loop is very fast. In this part of the code:
119+
120+
for alien in aliens:
121+
alien.draw()
122+
# Aliens shoot randomly
123+
# Your code here!
124+
125+
You need to tell when the aliens when to shoot. (Hint: use random.randint !)
126+
127+
### Detecting collisions between Aliens and player missiles
128+
129+
Now we're getting somewhere - we can shoot the aliens and they can shoot us. However, something is wrong - the missiles fly right by and do nothing!
130+
131+
We'll need to figure out when missiles collide with aliens. In this part of the code:
132+
133+
# Move, detect collisions, and draw the player's missiles
134+
for missile in player_missiles:
135+
missile.move()
136+
# Determine if the missile hit an alien. If so, the missile is spent
137+
# Your code here!
138+
139+
Figure out when the missile has hit any of the aliens.
140+
141+
Hint #1: Loop over the list of aliens and use `doRectsOverlap`. What are the `get_rect()` methods for?
142+
143+
Hint #2: When an alien is hit, remove it from the list of aliens and make the missile "spent"
144+
145+
### Winning and Losing conditions
146+
147+
In Space Invaders, the player wins when the player has shot all the aliens. The player loses when they either get shot by an alien, or if an alien reaches the bottom of the screen.
148+
149+
Let's implement these conditions. First, let's check to see if the player has gotten shot (the spaceship overlaps with an alien's missile) by adding a check to this section of code. The player should immediately lose (and the game is over) whenever the spaceship is hit.
150+
151+
for missile in alien_missiles:
152+
missile.move()
153+
# Detects collisions between the player and the alien missile
154+
# The player loses if they touch an alien missile
155+
# Your code here!
156+
157+
Hint: Take a look at the example for when a player missile hits an alien
158+
159+
Hint: The game is over when `running` is set to `False`
160+
161+
162+
Finally, we need to see if there are any aliens left, or if an alien reaches the bottom of the screen. The player wins if there are no more aliens, but loses if any of the aliens reach the bottom!
163+
164+
Hint: Loop through the aliens to see if any of them have a `ypos` greater than a certain amount...
165+
166+
167+
## Projects!
168+
169+
We now have a complete, but extremely basic game of space invaders. The rest of this class is to make this game _awesome_.
170+
171+
### 1. Make the alien change pictures every frame
172+
173+
In space invaders, the aliens look different every single frame. There's another picture in here called `alien2.png`. Use it and change the appearance of all the aliens every single frame.
174+
175+
(Hint: Change the alien's picture every time its move method is called)
176+
177+
### 2. Keep score
178+
179+
It's much more fun when we can keep the player's score. Whenever the player shoots an alien, give the player 25 points. Then, make a point display to show the player. Here's some of the boilerplate code for adding text to the screen:
180+
181+
myfont = pygame.font.SysFont("Arial", 15)
182+
score_label = myfont.render(str(score1), 1, pygame.color.THECOLORS['white'])
183+
screen.blit(score1_labe, (5, 10))
184+
185+
### 3. Give the player multiple lives
186+
187+
The player shouldn't automatically die whenever they get shot - they should have 3 lives. The player only loses when they lose all 3 of their lives. Make a display for letting the player know how many lives they have left next to the score display.
188+
189+
### 4. Make a flying saucer zip across the top of the screen
190+
191+
Remember how there was a saucer that sometimes appears at the top of the screen in space invaders? Let's implement that!
192+
193+
Make a new class called Saucer, use the `saucer.png` image provided in this repo, and every now and then, move a saucer from the right to the left across the very top of the screen.
194+
195+
Whenever the player shoots the saucer, give the player 100 points.
196+
197+
### Other ideas
198+
199+
- Give the aliens an explosion animation whenever they get shot
200+
- Make a YOU WIN and a YOU LOSE screen in the pygame window
201+
- The aliens' movement isn't too great - they stay in formation even after most of them are gone. Can you make smaller formations of aliens more challenging to shoot?
202+
- Give the player some shields (they can take some hits) to hide behind
203+
- Make different kinds of aliens - there's only 1 kind right now
204+
205+
## Further reading
206+
207+
Remember at the beginning of the class we noted how similar the player, alien, and missile classes were? Doesn't the code look very similar too? This is where `inheritence` comes into play. In Object Oriented Programming, inheritence is a way of saying that "this class is a kind of this other class". For example, an Orange is a Fruit, or a Dog is an Animal.
208+
209+
Recommended reading is here: https://docs.python.org/2/tutorial/classes.html .
210+
211+
Special challenge is to re-write the Player, Alien, and Missiles classes to all inherit from a common class, so we don't have to reimplement `draw` for any of them.

Day2/alien1.png

4.73 KB
Loading

Day2/alien2.png

4.86 KB
Loading

0 commit comments

Comments
 (0)