/
game.py
116 lines (90 loc) · 3.48 KB
/
game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import itertools
import sys, pygame
from shooter import Shooter
from centipedebody import CentipedeBody
from centipede import Centipede
from mushroom import Mushroom
from random import randrange
from enum import Enum
def randomlyGenerateMushrooms(width, height):
''' Returns a list of mushrooms that were randomly positioned on the board
given the width of the board, and the height of the board.'''
mushroomList = []
mushrooms = 45 #hardcoded for now...
for mushroom in range(mushrooms):
newX = randrange(0, width)
newY = randrange(0, height)
mushroomList.append(Mushroom([newX,newY]))
return mushroomList
pygame.init()
size = width, height = 800, 600
black = 0, 0, 0
screen = pygame.display.set_mode(size)
pos = [300,400]
imageLocation = "./images/shooter.png"
Status = Enum('Status', 'win lose')
shooter = Shooter(imageLocation, pos, width, height)
centipede = Centipede([-1,0],[500,1], width, height, 13)
allSpriteGroup = pygame.sprite.Group()
allSpriteGroup.add(shooter)
allSpriteGroup.add(centipede)
# A group containing all of the lasers the Shooter shoots
laserGroup = pygame.sprite.Group()
# A group containing all mushrooms in the game
mushroomGroup = pygame.sprite.Group()
mushroomGroup.add(randomlyGenerateMushrooms(width, height))
# All the non player character sprites
npcGroups = [laserGroup, centipede, mushroomGroup]
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
newLaser = shooter.shoot()
laserGroup.add(newLaser)
allSpriteGroup.add(newLaser)
pressedKeys = pygame.key.get_pressed()
shooter.update(pressedKeys)
# Move everything in the game (referring to the underlying positions of game objects)
for gameSprite in list(itertools.chain(npcGroups)):
gameSprite.update()
screen.fill(black)
# Draw all the game objects on the screen in their new positions
for gameSprite in allSpriteGroup:
screen.blit(gameSprite.image, gameSprite.rect)
# Deal with centipede segments that were shot
collisions = pygame.sprite.groupcollide(laserGroup, centipede, True, False)
segmentsHit = []
for laser in collisions:
segmentsHit.append(collisions[laser])
freshMushroom = centipede.damage([segment for segmentList in segmentsHit for segment in segmentList])
if freshMushroom != None:
mushroomGroup.add(freshMushroom)
# Deal with centipede segments that collide with mushrooms
collisions = pygame.sprite.groupcollide(centipede, mushroomGroup, False, False)
centipede.collide(collisions)
#Delete mushrooms that get shot
pygame.sprite.groupcollide(laserGroup, mushroomGroup, True, True)
allSpriteGroup.add(itertools.chain(npcGroups))
# Player killed the centipede!
if len(centipede) == 0:
gameStatus = Status.win
break
# Shooter was killed :(
if pygame.sprite.spritecollideany(shooter, centipede):
gameStatus = Status.lose
break
pygame.display.flip()
if gameStatus == Status.lose:
statusImage = pygame.image.load("./images/youlose.png")
else:
statusImage = pygame.image.load("./images/youwin.png")
statusRect = statusImage.get_rect()
statusRect.x = 75
statusRect.y = 100
screen.fill(black)
screen.blit(statusImage, statusRect)
pygame.display.flip()
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()