-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
249 lines (212 loc) · 7.94 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import random
import sys
import time
import pygame
import pygame.gfxdraw
from menu import *
from math import *
from pygame.locals import *
SPEED = 10 # frames per second setting
WINWIDTH = 1280 # width of the program's window, in pixels
WINHEIGHT = 720 # height in pixels
RADIUS = 5 # radius of the circles
PLAYERS = 1 # number of players
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
P1COLOUR = RED
P2COLOUR = GREEN
P3COLOUR = BLUE
def main():
# main loop
global FPS_CLOCK, SCREEN, DISPLAYSURF, MY_FONT
pygame.init()
FPS_CLOCK = pygame.time.Clock()
SCREEN = pygame.display.set_mode((WINWIDTH, WINHEIGHT))
DISPLAYSURF = pygame.Surface(SCREEN.get_size())
pygame.display.set_caption('FarBy!')
# pygame.mixer.music.load('test.mp3')
# pygame.mixer.music.play(-1, 0.0)
MY_FONT = pygame.font.SysFont('bauhaus93', 37)
while True:
start_screen()
rungame()
gameover()
class Player(object):
# Class which can be used to generate random position and angle, to compute movement values and to draw player
def __init__(self):
self.running = True
self.colour = None
self.score = 0
def gen(self):
# generates random position and direction
self.x = random.randrange(50, WINWIDTH - 165)
self.y = random.randrange(50, WINHEIGHT - 50)
self.angle = random.randrange(0, 360)
def move(self):
# computes current movement
self.x += int(RADIUS * 2 * cos(radians(self.angle)))
self.y += int(RADIUS * 2 * sin(radians(self.angle)))
def draw(self):
# drawing players
pygame.gfxdraw.aacircle(DISPLAYSURF, self.x, self.y, RADIUS, self.colour)
pygame.gfxdraw.filled_circle(DISPLAYSURF, self.x, self.y, RADIUS, self.colour)
def rungame():
global WINNER
DISPLAYSURF.fill(BLACK)
pygame.draw.aaline(DISPLAYSURF, WHITE, (WINWIDTH-115, 0), (WINWIDTH-115, WINHEIGHT))
WINNER = []
first = True
run = True
players_running = PLAYERS
if PLAYERS == 3:
max_score = 10
else:
max_score = 5
# generating players
player1 = Player()
player2 = Player()
player3 = Player()
player_t = [player1, player2, player3]
for i in range(PLAYERS):
player_t[i].gen()
while run:
# checking how many players are needed running
if PLAYERS < 3:
player3.running = False
# initializing players colours
player1.colour = P1COLOUR
player2.colour = P2COLOUR
player3.colour = P3COLOUR
# generating random holes
hole = random.randrange(1, 20)
if hole == 3:
player1.move()
player1.colour = BLACK
elif hole == 5:
player2.move()
player2.colour = BLACK
elif hole == 7 and PLAYERS == 3:
player3.move()
player3.colour = BLACK
for i in range(PLAYERS): # loop for checking positions, drawing, moving and scoring for all players
if player_t[i].running and players_running > 1:
if player_t[i].angle < 0:
player_t[i].angle += 360
elif player_t[i].angle >= 360:
player_t[i].angle -= 360
# checking if someone fails
if (player_t[i].x > WINWIDTH-125 or player_t[i].x < 3 or
player_t[i].y > WINHEIGHT-3 or player_t[i].y < 3 or
DISPLAYSURF.get_at((player_t[i].x, player_t[i].y)) != BLACK):
player_t[i].running = False
players_running -= 1
if i == 0:
if player2.running:
player2.score += 1
if player3.running:
player3.score += 1
elif i == 1:
if player1.running:
player1.score += 1
if player3.running:
player3.score += 1
elif i == 2:
if player1.running:
player1.score += 1
if player2.running:
player2.score += 1
player_t[i].draw()
player_t[i].move()
for event in pygame.event.get():
if event.type == QUIT:
shutdown()
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
shutdown()
# steering
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
player1.angle -= 10
if keys[pygame.K_RIGHT]:
player1.angle += 10
if keys[pygame.K_a]:
player2.angle -= 10
if keys[pygame.K_s]:
player2.angle += 10
if keys[pygame.K_k]:
player3.angle -= 10
if keys[pygame.K_l]:
player3.angle += 10
# drawing scores
scoring(player1.score, player2.score, player3.score, P1COLOUR, P2COLOUR, P3COLOUR)
# drawing all on the screen
SCREEN.blit(DISPLAYSURF, (0, 0))
pygame.display.update()
# checking if someone reach max score and win
if players_running == 1:
if any(n >= max_score for n in (player1.score, player2.score, player3.score)):
run = False
for i in range(PLAYERS):
if player_t[i].score == max(player1.score, player2.score, player3.score):
WINNER.append(i + 1)
continue
pygame.time.wait(1000)
DISPLAYSURF.fill(BLACK)
pygame.draw.aaline(DISPLAYSURF, WHITE, (WINWIDTH-115, 0), (WINWIDTH-115, WINHEIGHT))
first = True
players_running = PLAYERS
for i in range(PLAYERS):
player_t[i].gen()
player_t[i].running = True
continue
if first: # if the game starts, wait some time
pygame.time.wait(1500)
first = False
FPS_CLOCK.tick(SPEED)
def start_screen():
# initializing menu, getting number of players after choosing game mode
global PLAYERS
menu = Menu(['2 Players', '3 Players', 'Help', 'Exit'])
menu.init(SCREEN)
menu.draw()
PLAYERS = menu.start()
def scoring(play1score, play2score, play3score, colour1, colour2, colour3):
# drawing scores
colour0 = WHITE
if PLAYERS == 2:
colour3 = BLACK
elif PLAYERS == 1:
colour3 = colour2 = colour1 = colour0 = BLACK
score_msg = MY_FONT.render("Score:", 1, colour0, BLACK)
score1_msg = MY_FONT.render("P1: " + str(play1score), 1, colour1, BLACK)
score2_msg = MY_FONT.render("P2: " + str(play2score), 1, colour2, BLACK)
score3_msg = MY_FONT.render("P3: " + str(play3score), 1, colour3, BLACK)
DISPLAYSURF.blit(score_msg, (WINWIDTH - 110, WINHEIGHT/10))
DISPLAYSURF.blit(score1_msg, (WINWIDTH - 108, WINHEIGHT/10 + 40))
DISPLAYSURF.blit(score2_msg, (WINWIDTH - 108, WINHEIGHT/10 + 80))
DISPLAYSURF.blit(score3_msg, (WINWIDTH - 108, WINHEIGHT/10 + 120))
def gameover():
# drawing winner/s and waiting for key press
if len(WINNER) == 1:
end_msg = "Player %d wins! Press button to go to main menu." % WINNER[0]
elif len(WINNER) == 2:
end_msg = "Players %d and %d ties! Press button to go to main menu." % (WINNER[0], WINNER[1])
end_msg_render = MY_FONT.render(end_msg, 1, WHITE, BLACK)
SCREEN.blit(end_msg_render, ((WINWIDTH - MY_FONT.size(end_msg)[0]) / 2, WINHEIGHT/5))
pygame.display.update()
end = True
while end:
for event in pygame.event.get():
if event.type == QUIT:
shutdown()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
shutdown()
else:
end = False
FPS_CLOCK.tick(10)
if __name__ == '__main__':
main()