-
Notifications
You must be signed in to change notification settings - Fork 0
/
Controller.py
139 lines (126 loc) · 5.59 KB
/
Controller.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import pygame
import bullet
import ship
import Char
import aliens
import score
import Start
import End
WIDTH = 1000
HEIGHT = 700
class Controller:
def __init__(self):
'''
Defines the game screen.
param list:(object) only needs self
return: (None)
'''
pygame.init()
self.begin = True
while self.begin:
self.x = WIDTH / 2
self.y = HEIGHT - 140
self.cont = True
self.front = Start.Start()
if self.front.choice():
try:
self.char = Char.Char()
self.image = self.char.start()
self.display = pygame.display.set_mode((WIDTH, HEIGHT))
self.background = pygame.Surface(self.display.get_size()).convert()
self.room = pygame.image.load('classroom2.jpg')
self.room = pygame.transform.scale(self.room,(1000,700))
self.background.blit(self.room,(0,0))
self.ship = ship.ship(self.image[0], self.x,self.y)
self.alien= []
self.bullet = []
self.num =0
self.speed = 1
self.spriteship = pygame.sprite.Group(self.ship)
self.spritealien = pygame.sprite.Group(self.alien)
self.spritebullet = pygame.sprite.Group(self.bullet)
self.font = pygame.font.SysFont('bodoniblack',30)
self.score = score.score()
self.value = self.font.render('Score: '+ str(self.score.count),True,(0,255,0))
self.game = Controller.start(self)
self.end = End.End(self.game)
Controller.end(self)
except:
self.cont = False
else:
self.end = End.End(0)
Controller.end(self)
if not self.cont:
self.begin = False
def start(self):
'''
Runs the game screen.
param list:(object) only needs self
return: (int) returns the score the player got
'''
move = [False,False]
done = False
while not done:
self.alien.append(aliens.aliens(self.image[1]))
self.spritealien.add(self.alien)
if len(self.alien)%15 == 0:
self.speed += 1
for a in range(len(self.alien)):
while self.alien[a].rect.y<700:
pygame.time.delay(5)
self.alien[a].move(self.speed)
self.display.blit(self.background,(0,0))
self.spriteship.draw(self.display)
self.spritealien.draw(self.display)
self.spritebullet.draw(self.display)
self.display.blit(self.value,(30,30))
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
move[0] = True
elif event.key == pygame.K_RIGHT:
move[1] = True
elif event.key == pygame.K_SPACE:
self.bullet.append(bullet.bullet(self.ship.rect.x+60,self.ship.rect.y))
self.spritebullet.add(self.bullet)
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
move[0] = False
elif event.key == pygame.K_RIGHT:
move[1] = False
if move[0]:
self.ship.moveLeft(self.speed)
elif move[1]:
self.ship.moveRight(self.speed)
for b in range(len(self.bullet)):
if self.bullet[b].rect.y>-20:
self.bullet[b].shootBullet()
if self.bullet[b].rect.colliderect(self.alien[a].rect)and self.bullet[b].rect.y>65:
self.bullet[b].kill()
self.alien[a].kill()
self.bullet[b].exit()
self.alien[a].exit()
self.score.increase()
self.value = self.font.render('Score: '+ str(self.score.count),True,(0,255,0))
self.spriteship.draw(self.display)
self.spritealien.draw(self.display)
self.spritebullet.draw(self.display)
pygame.display.flip()
if self.ship.rect.colliderect(self.alien[a].rect):
return self.score.count
elif 640 <= self.alien[a].rect.y <=700:
return self.score.count
self.display.blit(self.background,(0,0))
self.spriteship.draw(self.display)
self.spritealien.draw(self.display)
self.spritebullet.draw(self.display)
self.display.blit(self.value,(30,30))
pygame.display.flip()
def end(self):
if not self.end.choice():
self.cont = False
pygame.quit()
Controller()