-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
153 lines (123 loc) · 4.37 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
import pygame
from pygame import mixer
from fighter import Fighter
mixer.init()
pygame.init()
#create game window
SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Brawler")
#set framerate
clock = pygame.time.Clock()
FPS = 60
#define colours
RED = (255, 0, 0)
YELLOW = (255, 255, 0)
WHITE = (255, 255, 255)
#define game variables
intro_count = 3
last_count_update = pygame.time.get_ticks()
score = [0, 0]#player scores. [P1, P2]
round_over = False
ROUND_OVER_COOLDOWN = 2000
#define fighter variables
character_size = 128
character_scale = 2.5
character_offset = [43, 30]
shinobi_steps = [6, 8, 8, 12, 5, 3, 4, 4, 2, 4]
shinobi_data = [character_size, character_scale, character_offset]
fighter_steps = [6, 8, 8, 10, 4, 3, 4, 2, 3, 3]
fighter_data = [character_size, character_scale, character_offset]
#load music and sounds
pygame.mixer.music.load("assets/audio/music.mp3")
pygame.mixer.music.set_volume(0.5)
pygame.mixer.music.play(-1, 0.0, 5000)
sword_fx = pygame.mixer.Sound("assets/audio/sword.wav")
sword_fx.set_volume(0.5)
punch_fx = pygame.mixer.Sound("assets/audio/punch.mp3")
punch_fx.set_volume(0.75)
#load background image
bg_image = pygame.image.load("assets/images/bg_mountain.png").convert_alpha()
#load spritesheets
shinobi_sheet = pygame.image.load('assets/images/character/PSD/Shinobi_Spritelist.png').convert_alpha()
fighter_sheet = pygame.image.load('assets/images/character/PSD/Fighter_Spritelist.png').convert_alpha()
#load vicory image
victory_img = pygame.image.load("assets/images/icons/victory.png").convert_alpha()
#define number of steps in each animation
shinobi_steps = [6, 8, 8, 12, 5, 3, 4, 4, 2, 4]
fighter_steps = [6, 8, 8, 10, 4, 3, 4, 2, 3, 3]
#define font
count_font = pygame.font.SysFont("arialblack", 80)
score_font = pygame.font.SysFont("arialblack", 30)
#function for drawing text
def draw_text(text, font, text_col, x, y):
img = font.render(text, True, text_col)
screen.blit(img, (x, y))
#function for drawing background
def draw_bg():
scaled_bg = pygame.transform.scale(bg_image, (SCREEN_WIDTH, SCREEN_HEIGHT))
screen.blit(scaled_bg, (0, 0))
#function for drawing fighter health bars
def draw_health_bar(health, x, y):
ratio = health / 100
pygame.draw.rect(screen, WHITE, (x - 2, y - 2, 404, 34))
pygame.draw.rect(screen, RED, (x, y, 400, 30))
pygame.draw.rect(screen, YELLOW, (x, y, 400 * ratio, 30))
fighter_1 = Fighter(1, 200, 310, False, shinobi_data, shinobi_sheet, shinobi_steps ,sword_fx)
fighter_2 = Fighter(2, 700, 310, True, fighter_data, fighter_sheet, fighter_steps , punch_fx)
#game loop
run = True
while run:
clock.tick(FPS)
#draw background
draw_bg()
#show player stats
draw_health_bar(fighter_1.health, 20, 20)
draw_health_bar(fighter_2.health, 580, 20)
draw_text("P1: " + str(score[0]), score_font, RED, 20, 60)
draw_text("P2: " + str(score[1]), score_font, RED, 580, 60)
#update countdown
if intro_count <= 0:
#move fighters
fighter_1.move(SCREEN_WIDTH, SCREEN_HEIGHT, screen, fighter_2, round_over)
fighter_2.move(SCREEN_WIDTH, SCREEN_HEIGHT, screen, fighter_1, round_over)
else:
#display count timer
draw_text(str(intro_count), count_font, RED, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 3)
#update count timer
if (pygame.time.get_ticks() - last_count_update) >= 1000:
intro_count -= 1
last_count_update = pygame.time.get_ticks()
#update fighters
fighter_1.update()
fighter_2.update()
#draw fighters
fighter_1.draw(screen)
fighter_2.draw(screen)
#check for player defeat
if round_over == False:
if fighter_1.alive == False:
score[1] += 1
round_over = True
round_over_time = pygame.time.get_ticks()
elif fighter_2.alive == False:
score[0] += 1
round_over = True
round_over_time = pygame.time.get_ticks()
else:
#display victory image
screen.blit(victory_img, (360, 150))
if pygame.time.get_ticks() - round_over_time > ROUND_OVER_COOLDOWN:
round_over = False
intro_count = 3
fighter_1 = Fighter(1, 200, 310, False, shinobi_data, shinobi_sheet, shinobi_steps ,sword_fx)
fighter_2 = Fighter(2, 700, 310, True, fighter_data, fighter_sheet, fighter_steps , punch_fx)
#event handler
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
#update display
pygame.display.update()
#exit pygame
pygame.quit()