-
Notifications
You must be signed in to change notification settings - Fork 1
/
stonehunt.py
executable file
·185 lines (159 loc) · 5.32 KB
/
stonehunt.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
178
179
180
181
182
183
184
185
#!/usr/bin/python
# coding: latin-1
import pygame, os, random, sys, results
from pygame.locals import *
from constants import *
from stage import Stage
from player import Player
# Initialise pygame
os.environ["SDL_VIDEO_CENTERED"] = "1"
pygame.init()
class Info(object):
def __init__(self):
self.score = [0]*c.PLAYERS
self.time = 0.0
self.turn = 0
self.font = pygame.font.Font("accid.ttf", 18)
self.reload_score()
self.arrows = pygame.image.load("images/arrows.png").convert_alpha()
def reload_score(self):
'''Creates the text for the score board.'''
self.score_text = ''
for i in range(c.PLAYERS):
self.score_text += 'Player ' + str(i + 1) +' has ' + str(self.score[i]) + ' points >> '
def draw(self,screen, players):
'''Draws the scoreboard into the screen.'''
score_label = self.font.render(self.score_text, 1, (230,230,230))
pygame.draw.rect(screen, (20, 30, 20), (0, c.FIELD_SIZE*25, c.FIELD_SIZE*30, 30))
screen.blit(score_label, (15, c.FIELD_SIZE*25 + 5))
time = str(self.time).split('.')
time_stamp = ' Current elapsed time: ' + str(time[0])+'.'+str(time[1][0])+' s >>'
time_label = self.font.render(time_stamp, 1, (255,255,255))
screen.blit(time_label, (15 + score_label.get_width() + 15, c.FIELD_SIZE*25 + 5))
for p in range(c.PLAYERS):
if not players[p].alive:
continue
if players[p].team == 'A':
x_off = 0
else:
x_off = 1
screen.blit(self.arrows, (players[p].rect.x, players[p].rect.y - 40), (30*x_off, 16*p, 30, 16))
def wait(seconds):
time_limit = pygame.time.get_ticks() + seconds*1000
clock = pygame.time.Clock()
events = []
while pygame.time.get_ticks() <= time_limit:
clock.tick(20)
for event in pygame.event.get():
if event.type == QUIT:
return 1
elif event.type == KEYDOWN and event.key == K_ESCAPE:
return 1
def create_players(info,stage):
'''Create the players in accordance with the turn and the current team.'''
players = []
p_img = "images/hunterA1.png"
p2_img = "images/hunterB1.png"
for x in range(c.PLAYERS):
if c.TEAM[info.turn][x] == "A":
pos = stage.get_base()
img = p_img
team = 'A'
else:
pos = stage.get_floor()
img = p2_img
team = 'B'
#print "Creating player at", pos
players.append(Player(img, stage, team, c.KEYS[x], pos))
return players
def start():
'''Generates the loop to draw into the screen with the game logic.'''
screen = pygame.display.set_mode((30*c.FIELD_SIZE, 25*c.FIELD_SIZE + 30), DOUBLEBUF | HWSURFACE, 32)
pygame.mouse.set_visible(False)
info = Info()
clock = pygame.time.Clock()
c.GAME_SONG.play(-1)
stage = Stage()
stage.set_stones()
spawners = []
players = create_players(info,stage)
running = True
while running:
# We make the game run at desired FPS
time_passed = clock.tick(c.FPS)
# We take the current time to show it in the scoreboard
time_passed_seconds = time_passed / 1000.0
info.time += time_passed_seconds
# Here we check if it's other players turn
if c.HUNTING_TIME*(info.turn + 1) <= int(info.time):
print "Ended turn time..."
info.turn += 1
if info.turn > (c.PLAYERS - 1):
running = False
continue
players = create_players(info,stage)
# If a player is death and have to respawn, here we check
# if it's time to do it.
if spawners:
offset = 0
for s in range(len(spawners)):
if spawners[s+offset][0] <= int(info.time):
for p in spawners[s+offset][1:3]:
players[p].alive = True
pos = stage.get_base()
players[p].rect.x = pos[0]
players[p].rect.y = pos[1]
del spawners[s+offset]
offset -= 1
# The game logic is here
for p in range(c.PLAYERS):
if not players[p].alive:
continue
if players[p].team == 'B':
for p2 in range(c.PLAYERS):
if p != p2 and players[p2].team == 'A' and players[p2].alive:
if players[p].rect.colliderect(players[p2].rect) and not players[p2].is_in_water():
if players[p2].own:
stage.stones[players[p2].stone] = stage.set_stone()
players[p2].stone = -1
players[p2].own = False
players[p2].alive = False
spawners.append([info.time + c.SPAWN_TIME,p2])
info.score[p] += 1
info.reload_score()
elif players[p].team == 'A':
for s in range(len(stage.stones)):
if players[p].rect.colliderect(stage.stones[s].rect) and not players[p].own:
players[p].own = True
players[p].stone = s
stage.stones[s].owned = True
stage.stones[s].set_player(players[p])
if players[p].own:
for elem in stage.base:
if players[p].rect.colliderect(elem.rect):
info.score[p] += 1
info.reload_score()
players[p].own = False
stage.stones[players[p].stone] = stage.set_stone()
players[p].stone = -1
# Here we crontol the events
for e in pygame.event.get():
if e.type == pygame.QUIT:
running = False
if e.type == pygame.KEYDOWN and e.key == pygame.K_ESCAPE:
running = False
# And at the same time, we draw the stage, the users and the scoreboard
# to the screen
stage.draw(screen)
key = pygame.key.get_pressed()
for p in range(c.PLAYERS):
if not players[p].alive:
continue
players[p].get_move(key, time_passed_seconds)
players[p].draw(screen)
info.draw(screen, players)
pygame.display.update()
# At the end, we stop the music and start the menu one, with the results
c.GAME_SONG.stop()
c.MENU_SONG.play(-1)
results.start(info.score)