-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
177 lines (146 loc) · 5.06 KB
/
main.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
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
167
168
169
170
171
172
173
174
175
176
177
import pygame
from entities.entity import Entity, Player
from weapons.switcheroo import Switcheroo
from weapons.magnet import Magnet
from objects.kill_part import KillPart
pygame.init()
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
size = (500, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Gimmick Game")
class Game:
def __init__(self):
self.looping = True
self.player = Player(240, 240)
self.player.weapons.append(Switcheroo())
self.player.weapons.append(Magnet())
self.entities = []
self.entities.append(self.player)
self.entities.append(Entity(100, 380))
self.entities.append(Entity(300, 380))
self.objects = []
self.objects.append(KillPart(350, 100))
self.objects.append(KillPart(160, 200))
self.objects.append(KillPart(240, 400))
self.clock = pygame.time.Clock()
game = Game()
def update_entity(entity):
entity.x = round(entity.x + entity.dx)
entity.y = round(entity.y + entity.dy)
if entity.dx > 0:
entity.dx -= 0.3
if entity.x > 500:
entity.x = 0
if entity.dx < 0:
entity.dx += 0.3
if entity.x < 0:
entity.x = 500
if entity.dy > 0:
entity.dy -= 0.3
if entity.y > 500:
entity.y = 0
if entity.dy < 0:
entity.dy += 0.3
if entity.y < 0:
entity.y = 500
for object in game.objects:
if entity.is_colliding_with(object):
object.on_touch(entity)
render_entity(entity)
def render_entity(entity):
COLOR = (0, 0, 0)
if entity.isPlayer:
COLOR = WHITE
else:
COLOR = RED
pygame.draw.rect(screen, COLOR, (entity.x, entity.y, entity.width, entity.height))
def update_object(object):
object.x = round(object.x + object.dx)
object.y = round(object.y + object.dy)
if object.dx > 0:
object.dx -= 0.3
if object.x > 500:
object.x = 0
if object.dx < 0:
object.dx += 0.3
if object.x < 0:
object.x = 500
if object.dy > 0:
object.dy -= 0.3
if object.y > 500:
object.y = 0
if object.dy < 0:
object.dy += 0.3
if object.y < 0:
object.y = 500
render_object(object)
def render_object(object):
pygame.draw.rect(screen, object.color, (object.x, object.y, object.width, object.height))
def loop():
keys_held = []
while game.looping:
game.clock.tick(30)
screen.fill(BLACK)
font = pygame.font.Font(None, 30)
text = font.render("1: Switcheroo", 1, RED)
screen.blit(text, (15, 15))
text = font.render("2: Magnet", 1, RED)
screen.blit(text, (15, 35))
text = font.render(str(keys_held), 1, RED)
screen.blit(text, (300, 15))
temp_entities = []
for entity in game.entities:
update_entity(entity)
if entity.health > 0:
temp_entities.append(entity)
game.entities = temp_entities
for object in game.objects:
update_object(object)
pygame.display.flip()
for key in keys_held:
if key == "up":
game.player.dy -= game.player.move_speed
if key == "left":
game.player.dx -= game.player.move_speed
if key == "down":
game.player.dy += game.player.move_speed
if key == "right":
game.player.dx += game.player.move_speed
if key == "2":
game.player.weapons[1].use(game.player, game.entities)
for event in pygame.event.get():
if event.type == pygame.QUIT:
game.looping = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
game.looping = False
if event.key == pygame.K_UP:
keys_held.append("up")
if event.key == pygame.K_LEFT:
keys_held.append("left")
if event.key == pygame.K_DOWN:
keys_held.append("down")
game.player.dy += game.player.move_speed
if event.key == pygame.K_RIGHT:
keys_held.append("right")
game.player.dx += game.player.move_speed
if event.key == pygame.K_1:
game.player.weapons[0].use(game.player, game.entities)
if event.key == pygame.K_2:
keys_held.append("2")
elif event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
keys_held.remove("up")
if event.key == pygame.K_LEFT:
keys_held.remove("left")
if event.key == pygame.K_DOWN:
keys_held.remove("down")
if event.key == pygame.K_RIGHT:
keys_held.remove("right")
if event.key == pygame.K_2:
keys_held.remove("2")
loop()
pygame.quit()