-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
150 lines (117 loc) · 5.12 KB
/
Copy pathgame.py
File metadata and controls
150 lines (117 loc) · 5.12 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
import sys
import os
from sprites import *
from pygame.locals import *
import pygame.mixer
class Game:
def __init__(self):
self.computer_ant_groupid = 1
self.player_ant_groupid = 0
self.pheromone_group = pygame.sprite.Group()
self.ant_groups = []
self.ant_groups.append(pygame.sprite.Group())
self.ant_groups.append(pygame.sprite.Group())
self.food_group = pygame.sprite.Group()
self.all_group = pygame.sprite.Group()
self.cursor_group = pygame.sprite.Group()
Pheromone.groups = self.pheromone_group, self.all_group
Food.groups = self.food_group, self.all_group
Cursor.groups = self.cursor_group
self.show_range = False
self.ant_enemy_groups = [0] * 2
self.ant_enemy_groups[self.player_ant_groupid] = self.get_enemy_groups(self.player_ant_groupid)
self.ant_enemy_groups[self.computer_ant_groupid] = self.get_enemy_groups(self.computer_ant_groupid)
self.time_counter = 0
self.score = 0
self.food_period = 60
self.font = pygame.font.SysFont("Consolas Bold", 50)
self.explosion_sound = pygame.mixer.Sound(os.path.join("sounds", "explosion.wav"))
self.eat_sound = pygame.mixer.Sound(os.path.join("sounds", "eat.wav"))
self.cursor = Cursor()
def get_enemy_groups(self, groupid):
groups = []
for gid, ant_group in enumerate(self.ant_groups):
if gid != groupid:
groups.append(ant_group)
return groups
def spawn_ant(self, groupid):
new_ant = Ant(groupid)
new_ant.enemy_groups = self.ant_enemy_groups[groupid]
self.ant_groups[groupid].add(new_ant)
self.all_group.add(new_ant)
def reset(self):
# clear all groups
self.pheromone_group.empty()
self.food_group.empty()
for ant_group in self.ant_groups:
ant_group.empty()
self.all_group.empty()
self.time_counter = 0
self.score = 0
# spawn new ants
self.spawn_ant(self.computer_ant_groupid)
self.spawn_ant(self.player_ant_groupid)
self.clock = pygame.time.Clock()
def stop(self, main):
main.highscore.add_score(self.score)
main.set_state(main.States.HighScore)
def generate_food(self, screen):
self.time_counter += 1
if self.time_counter >= self.food_period:
self.time_counter = 0
posx = random.uniform(0, screen.get_width())
posy = random.uniform(0, screen.get_height())
Food((posx, posy))
def update(self, screen, background, main):
screen.blit(background, (0, 0))
screen_height = screen.get_height()
screen_width = screen.get_width()
milliseconds = self.clock.tick(60)
seconds = milliseconds / 1000.0
self.cursor.rect.center = pygame.mouse.get_pos()
if self.show_range:
self.cursor_group.draw(screen)
self.generate_food(screen)
for groupid, ant_group in enumerate(self.ant_groups):
pheromone_collide_func = lambda s1, s2: s1.pheromone_rect.colliderect(s2.rect)
killed_pheromone_dict = pygame.sprite.groupcollide(self.pheromone_group, ant_group, True, False, pheromone_collide_func)
killed_food_dict = pygame.sprite.groupcollide(self.food_group, ant_group, True, False)
if groupid == self.player_ant_groupid:
self.score += len(killed_food_dict) * 20
self.score -= len(killed_pheromone_dict) * 50
for foods in killed_food_dict.values():
for food in foods:
self.eat_sound.play()
self.spawn_ant(groupid)
killed_ant_dict = pygame.sprite.groupcollide(self.ant_groups[0], self.ant_groups[1], True, True)
for ant in killed_ant_dict:
self.eat_sound.stop()
self.explosion_sound.play()
self.all_group.clear(screen, background)
self.all_group.update(seconds)
self.all_group.draw(screen)
score_text = self.font.render("Score: " + str(self.score), True, (255, 255, 0))
screen.blit(score_text, (screen_width - score_text.get_width() - 10, screen_height - score_text.get_height() - 10))
############################################
# End game when one group is eliminated
if not self.ant_groups[0]:
# No player Ant left
self.stop(main)
if not self.ant_groups[1]:
# No computer ant left
self.stop(main)
#############################################
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == MOUSEBUTTONDOWN:
mouse_pressed = pygame.mouse.get_pressed()
pos = pygame.mouse.get_pos()
if mouse_pressed[0]:
Pheromone(pos)
elif mouse_pressed[2]:
self.show_range = not self.show_range
elif event.type == KEYDOWN:
if event.key == K_SPACE:
self.stop(main)