-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
149 lines (106 loc) · 4.07 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
#!/usr/bin/env python
import random
import pygame
import pygame.gfxdraw
import Actor
import cfg
def main():
init()
main_loop()
def init():
pygame.init()
cfg.SURFACE_MAIN = pygame.display.set_mode((cfg.SCREEN_WIDTH, cfg.SCREEN_HEIGHT))
cfg.PLAYER = Actor.Player()
cfg.PLAYER.x = (int)(cfg.SCREEN_WIDTH / 2 - (cfg.PLAYER.width / 2))
cfg.PLAYER.y = (int)(cfg.SCREEN_HEIGHT / 2 - (cfg.PLAYER.height / 2))
cfg.PLAYER.add(cfg.ACTORS, cfg.INFECTED)
for i in range(0, cfg.INIT_ACTORS):
new_actor = Actor.Circle()
new_actor.x = random.randint(0, cfg.SCREEN_WIDTH - new_actor.width)
new_actor.y = random.randint(0, cfg.SCREEN_HEIGHT - new_actor.height)
new_actor.add(cfg.ACTORS, cfg.AI_ACTORS, cfg.HEALTHY)
def main_loop():
run_loop = True
while run_loop:
run_loop = process_events()
process_player_input()
process_ai_input()
move_actors()
handle_collisions()
kill_actors()
generate_new_actors()
draw()
pygame.time.wait(cfg.REFRESH_DELAY)
pygame.quit()
exit()
def process_events():
# Returns whether to keep running the main loop
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
return False
return True
def process_player_input():
pressed = pygame.key.get_pressed()
if pressed[pygame.K_UP]:
cfg.PLAYER.vert_accel(1)
if pressed[pygame.K_DOWN]:
cfg.PLAYER.vert_accel(-1)
if pressed[pygame.K_RIGHT]:
cfg.PLAYER.hori_accel(1)
if pressed[pygame.K_LEFT]:
cfg.PLAYER.hori_accel(-1)
if pressed[pygame.K_ESCAPE]:
print("abort!")
# todo: abort
def move_actors():
for actor in cfg.ACTORS:
actor.move()
def handle_collisions():
collisions = set()
for infected in cfg.INFECTED.sprites():
collisions.update(infected.collided_with(cfg.HEALTHY))
for collision in collisions:
# 75% chance of infection every 50ms of contact
if checkTimeInterval(cfg.INFECTION_INTERVAL) and checkProbability(cfg.INFECTION_PROB):
print("INFECTED! size: %d" % collision.width)
collision.color = cfg.INFECTED_COLOR
collision.image = collision.load_sprite()
cfg.INFECTED.add(collision)
cfg.HEALTHY.remove(collision)
def kill_actors():
"""Infected actors have a 20% chance of dying every 1000ms"""
for actor in cfg.INFECTED.sprites():
if actor is cfg.PLAYER:
continue
if checkTimeInterval(cfg.KILL_INTERVAL) and checkProbability(cfg.KILL_PROB):
actor.die()
def generate_new_actors():
"""
50% chance of generation every 200ms if less than MAX_ACTORS
Circles shoot in from the sides
"""
if len(cfg.AI_ACTORS) < cfg.MAX_ACTORS and checkTimeInterval(cfg.GENERATION_INTERVAL) and checkProbability(cfg.GENERATION_PROB):
new_circle = Actor.Circle()
new_circle.x = 0 - new_circle.width if checkProbability(0.5) else cfg.SCREEN_WIDTH
new_circle.y = random.randint(0, cfg.SCREEN_HEIGHT - new_circle.height)
initial_v = random.randint(new_circle.MOVE_SPEED, new_circle.MAX_SPEED)
direction = 1 if new_circle.x < 0 else -1
new_circle.x_speed = initial_v * direction
new_circle.add(cfg.ACTORS, cfg.AI_ACTORS, cfg.HEALTHY)
def process_ai_input():
"""Each actor has a 25% chance of random movement every 100ms"""
for actor in cfg.AI_ACTORS.sprites():
if checkTimeInterval(cfg.MOVE_INTERVAL) and checkProbability(cfg.MOVE_PROB):
actor.hori_accel(random.randint(-1 * actor.MOVE_SPEED, actor.MOVE_SPEED))
actor.vert_accel(random.randint(-1 * actor.MOVE_SPEED, actor.MOVE_SPEED))
def checkTimeInterval(interval):
return pygame.time.get_ticks() % interval < cfg.REFRESH_DELAY
def checkProbability(probability):
return random.random() < probability
def draw():
cfg.SURFACE_MAIN.fill(cfg.BG_COLOR)
for actor in cfg.ACTORS:
cfg.SURFACE_MAIN.blit(actor.image, actor.get_pos())
pygame.display.flip()
main()