Skip to content

Commit cf2860e

Browse files
committed
Start README, add player shot limit
1 parent 4f12821 commit cf2860e

File tree

3 files changed

+296
-2
lines changed

3 files changed

+296
-2
lines changed

Day2/README.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
22+
### Example
23+
24+
The following is a class called Book
25+
26+
class Book:
27+
title = ""
28+
author = ""
29+
30+
As a warm up, type the class into Python and do the following:
31+
32+
1. Add a constructor to set the `title` and the `author` at the same time
33+
34+
2. Add a method to print out both the `title` and the `author`.
35+
36+
3. Create two books by different authors. Get each of them to print out their title and author.
37+
38+
39+
40+
1. Give each student a favorite color property. Then write a method that will have the student tell us his favorite color.
41+
42+
2. Write a class method that will have each Student say his name and age and favorite color.
43+
44+
3. Create a few more students: `jane` and `betty`. Give them ages, names, and favorite colors.
45+
46+
4. Bonus: Give each student a best friend property (hint: use classes!). Write a method to return his/her best friend's name
47+
48+
## Constructors
49+
50+
In the last section, we were creating students without names and ages. We then gave names and ages to the students later. It doesn't make any sense to make Students with names and ages, so let's do the folowwing:
51+
52+
def __init__(self, name, age):
53+
self.name = name
54+
self.age = age
55+
56+
Try to run your program again, and then use the same console command as before:
57+
58+
bobby = Student()
59+
60+
What happens?
61+
62+
You get an error. Since we have an constructor now, you can no longer create a Student without providing enough information to fill in the constructor arguments. Try this:
63+
64+
bobby = Student("bobby", 10)
65+
66+
It should work.
67+
68+
The constructors in Python have the name `__init__`, and have an extra argument you do not need to provide when you instantiate an object - in this case: `self`.
69+
70+
## Lab - Deck of Cards
71+
72+
You will be implementing a deck of cards. Open up the file `DeckCards.py` in this directory inside IDLE.
73+
74+
1. Complete the card class. Each card has a suit (hearts, diamonds, spades, clubs) and a value (A, J, K, Q, 2-10)
75+
76+
2. Complete the deck class. Each deck will have 52 cards - 13 Hearts, 13 Spades, 13 Clubs, 13 Diamonds, each suit with cards A, J, K, Q, 2 to 10.
77+
78+
3. Complete the `dealCards` and `remainingCards` methods. See the code for details.
79+
80+
4. Now write a program that uses the Deck class to deal random cards.
81+
82+
5. Try to implement some simple card games using your Card and Deck classes.

Day2/answers/spaceinvaders.py

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
#Credit the Invent With Python book (http://inventwithpython.com)
2+
#for doRectsOverlap and isPointInsideRect functions
3+
4+
#used to detect collisions in our game
5+
def doRectsOverlap(rect1, rect2):
6+
for a, b in [(rect1, rect2), (rect2, rect1)]:
7+
# Check if a's corners are inside b
8+
if ((isPointInsideRect(a.left, a.top, b)) or
9+
(isPointInsideRect(a.left, a.bottom, b)) or
10+
(isPointInsideRect(a.right, a.top, b)) or
11+
(isPointInsideRect(a.right, a.bottom, b))):
12+
return True
13+
14+
return False
15+
16+
#used the by the doRectsOverlap function (won't be called directly from game code)
17+
def isPointInsideRect(x, y, rect):
18+
if (x > rect.left) and (x < rect.right) and (y > rect.top) and (y < rect.bottom):
19+
return True
20+
else:
21+
return False
22+
23+
import pygame, sys, random
24+
pygame.init()
25+
26+
SCREEN_WIDTH = 640
27+
SCREEN_HEIGHT = 480
28+
29+
screen = pygame.display.set_mode([SCREEN_WIDTH,SCREEN_HEIGHT])
30+
31+
# List of missiles flying across the screen
32+
player_missiles = []
33+
alien_missiles = []
34+
black = [0, 0, 0]
35+
white = [255, 255, 255]
36+
37+
class Spaceship:
38+
xpos = 0
39+
ypos = 0
40+
picture = pygame.image.load('spaceship.png')
41+
42+
def __init__(self, x, y):
43+
self.xpos = x
44+
self.ypos = y
45+
46+
def moveRight(self):
47+
self.xpos += 5
48+
49+
def moveLeft(self):
50+
self.xpos -= 5
51+
52+
def shoot(self):
53+
missile = Missile(self.xpos + 16, self.ypos - 1, -10)
54+
player_missiles.append(missile)
55+
56+
def get_rect(self):
57+
return pygame.Rect(self.xpos, self.ypos, 32, 32)
58+
59+
def draw(self):
60+
screen.blit(self.picture, (self.xpos, self.ypos))
61+
62+
class Alien:
63+
xpos = 0
64+
ypos = 0
65+
xspeed = 2
66+
picture1 = pygame.image.load('alien1.png')
67+
picture2 = pygame.image.load('alien2.png')
68+
69+
def __init__(self, x, y):
70+
self.xpos = x
71+
self.ypos = y
72+
73+
def move(self, x, y):
74+
self.xpos += x
75+
self.ypos += y
76+
self.picture3 = self.picture1
77+
self.picture1 = self.picture2
78+
self.picture2 = self.picture3
79+
80+
def shoot(self):
81+
missile = Missile(self.xpos + 16, self.ypos + 32, 10)
82+
alien_missiles.append(missile)
83+
84+
def get_rect(self):
85+
return pygame.Rect(self.xpos, self.ypos, 32, 32)
86+
87+
def draw(self):
88+
screen.blit(self.picture1, (self.xpos, self.ypos))
89+
90+
91+
class Missile:
92+
xpos = 0
93+
ypos = 0
94+
speed = 0
95+
spent = False
96+
97+
def __init__(self, x, y, speed):
98+
self.xpos = x
99+
self.ypos = y
100+
self.speed = speed
101+
102+
def draw(self):
103+
pygame.draw.rect(screen, white, [self.xpos, self.ypos, 2, 10], 2)
104+
105+
def move(self):
106+
self.ypos += self.speed
107+
if self.ypos < 0 or self.ypos > SCREEN_HEIGHT:
108+
self.spent = True
109+
110+
def get_rect(self):
111+
return pygame.Rect(self.xpos, self.ypos, 2, 10)
112+
113+
def isSpent(self):
114+
return self.spent
115+
116+
# Create all the objects and variables!
117+
spaceship = Spaceship(200, 440)
118+
aliens = []
119+
for x in range(1,8):
120+
for y in range(1,4):
121+
aliens.append(Alien(x * 50 + 10, y * 50 + 10))
122+
123+
running = True
124+
ticks = 0
125+
alien_formation_x = 0
126+
alien_formation_x_speed = 10
127+
128+
# Main event loop. Here we:
129+
# 1. Handle keyboard input for the player and move the player around
130+
# 2. Move the missiles around
131+
# 3. Move the aliens around
132+
# 4. Detect collisions and handle them
133+
while running:
134+
for event in pygame.event.get():
135+
if event.type == pygame.QUIT:
136+
running = False
137+
138+
keys = pygame.key.get_pressed()
139+
if keys[pygame.K_RIGHT]:
140+
spaceship.moveRight()
141+
if keys[pygame.K_LEFT]:
142+
spaceship.moveLeft()
143+
if keys[pygame.K_SPACE]:
144+
spaceship.shoot()
145+
146+
screen.fill(black)
147+
148+
# Move, detect collisions, and draw the player's missiles
149+
for missile in player_missiles:
150+
missile.move()
151+
for alien in aliens:
152+
if doRectsOverlap(alien.get_rect(), missile.get_rect()):
153+
aliens.remove(alien)
154+
missile.spent = True
155+
156+
if missile.isSpent():
157+
player_missiles.remove(missile)
158+
missile.draw()
159+
160+
# Move, detect collisions, and draw the player's missiles
161+
for missile in alien_missiles:
162+
missile.move()
163+
164+
if doRectsOverlap(spaceship.get_rect(), missile.get_rect()):
165+
print "YOU LOSE"
166+
running = False
167+
missile.spent = True
168+
169+
if missile.isSpent():
170+
alien_missiles.remove(missile)
171+
missile.draw()
172+
173+
# Move all the aliens
174+
# The more aliens there are, the slower they move
175+
if ticks % (len(aliens) + 1) == 0:
176+
# The alien formation moves left and right
177+
alien_formation_x += alien_formation_x_speed
178+
for alien in aliens:
179+
alien.move(alien_formation_x_speed, 0)
180+
# Until the formation reaches a limit, after which it reverses
181+
# direction and moves down 5 pixels
182+
if alien_formation_x >= 200 or alien_formation_x <= 0:
183+
alien_formation_x_speed = -1 * alien_formation_x_speed
184+
for alien in aliens:
185+
alien.move(0, 5)
186+
187+
# Draw all the aliens and give them a chance to shoot!
188+
for alien in aliens:
189+
alien.draw()
190+
# Aliens shoot randomly
191+
# 0.05 percent change per frame that an alien shoots
192+
if (random.randint(1,1000) < 5):
193+
alien.shoot()
194+
195+
# If there are no more aliens, the player wins
196+
if len(aliens) == 0:
197+
print "YOU WIN!"
198+
running = False
199+
200+
# If any alien reaches the bottom of the screen, the player loses
201+
for alien in aliens:
202+
if alien.ypos > SCREEN_HEIGHT - 64:
203+
print "YOU LOSE"
204+
running = False
205+
206+
# Draw the spaceship
207+
spaceship.draw()
208+
pygame.display.update()
209+
ticks = ticks + 1
210+
211+
pygame.quit()

Day2/spaceinvaders.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,11 @@ def isSpent(self):
137137

138138
keys = pygame.key.get_pressed()
139139
if keys[pygame.K_RIGHT]:
140-
spaceship.moveRight()
140+
spaceship.moveRight()
141141
if keys[pygame.K_LEFT]:
142-
spaceship.moveLeft()
142+
spaceship.moveLeft()
143143
if keys[pygame.K_SPACE]:
144+
if len(player_missiles) == 0:
144145
spaceship.shoot()
145146

146147
screen.fill(black)

0 commit comments

Comments
 (0)