-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
166 lines (125 loc) · 4.6 KB
/
main.py
File metadata and controls
166 lines (125 loc) · 4.6 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# Import the pygame module
import pygame
import math
import globalvar as g
from player import Player
from background import Background
from ui import UI
from enemies import Enemies
from pygame.locals import (
K_ESCAPE,
K_RCTRL,
KEYDOWN,
QUIT,
)
# Initialize pygame
pygame.init()
# Create the screen object
# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
screen = pygame.display.set_mode((g.SCREEN_WIDTH, g.SCREEN_HEIGHT))
# Instantiate player. Right now, this is just a rectangle.
bg = Background()
player = Player(bg)
ui = UI(bg)
clock = pygame.time.Clock()
enemies = Enemies(bg, ui)
# Variable to keep the main loop running
running = True
newProj = False
projAim = (0, 0)
stopTime = False
selectedTurret = 0
GANTT = []
pos = (0, 0)
alphaTurret = None
bluTurret = pygame.image.load("sprites\\blu_turret.png").convert_alpha()
redTurret = pygame.image.load("sprites\\red_turret.png").convert_alpha()
violetTurret = pygame.image.load("sprites\\violet_turret.png").convert_alpha()
turretW = bluTurret.get_rect().width
turretH = bluTurret.get_rect().height
# Main loop
while running:
dt = clock.tick(g.FPS)
if not stopTime:
g.currentFrame = (g.currentFrame + 1) % (g.FPS)
if math.floor(g.currentFrame) == 0:
g.currentFrame = 0
mousePos = pygame.mouse.get_pos()
screen.fill((0, 0, 0))
# for loop through the event queue
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False
elif event.key == K_RCTRL:
stopTime = not stopTime
elif event.key == ord('1'):
if selectedTurret == 1:
selectedTurret = 0
else:
selectedTurret = 1
elif event.key == ord('2'):
if selectedTurret == 2:
selectedTurret = 0
else:
selectedTurret = 2
elif event.key == ord('3'):
if selectedTurret == 3:
selectedTurret = 0
else:
selectedTurret = 3
# Check for QUIT event. If QUIT, then set running to false.
elif event.type == QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
if pygame.mouse.get_pressed()[2]:
if selectedTurret != 0:
player.placeTurret(bg, mousePos[0], mousePos[1], selectedTurret - 1)
else:
turretToRemove = []
for turret in player.turretList:
if turret.rect.collidepoint(mousePos[0], mousePos[1]):
turretToRemove.append(turret)
for turret in turretToRemove:
player.turretNumber[turret.type] -= 1
player.turretPrice[turret.type] = 4**(player.turretNumber[turret.type]) * 100
player.cash += 0.8 * player.turretPrice[turret.type]
player.turretList.remove(turret)
pressed_keys = pygame.key.get_pressed()
player.update(pressed_keys, dt, enemies, bg)
bg.update(g.currentFrame)
ui.update(player, selectedTurret, dt)
enemies.updateAll(g.currentFrame, player, dt)
enemies.spawnAll(bg, player.score)
if pygame.mouse.get_pressed()[0]:
projAim = pygame.mouse.get_pos()
player.shoot(projAim)
pos = pygame.mouse.get_pos()
# DRAW
screen.blit(bg.surf, (bg.colOffset + bg.left, bg.top))
screen.blit(bg.surf, (bg.colOffset - bg.width + bg.left, bg.top))
for activity in enemies.GANTT:
screen.blit(activity.surf, activity.rect)
for accollo in enemies.accolloList:
screen.blit(accollo.surf, accollo.rect)
screen.blit(ui.surf, (0,0))
for turret in player.turretList:
screen.blit(turret.surf, turret.rect)
for proj in player.projList:
screen.blit(proj.surf, proj.rect)
for turret in player.turretList:
for proj in turret.projList:
screen.blit(proj.surf, proj.rect)
if selectedTurret != 0:
if selectedTurret == 1:
alphaTurret = bluTurret
elif selectedTurret == 2:
alphaTurret = redTurret
elif selectedTurret == 3:
alphaTurret = violetTurret
alphaTurret.set_alpha(96)
turretX = max(min(mousePos[0], bg.right - turretW / 2), bg.left + turretW / 2)
screen.blit(alphaTurret, (turretX - turretW / 2 , bg.bottom - turretH))
screen.blit(player.surf, player.rect)
# Update the display
pygame.display.flip()