-
-
Notifications
You must be signed in to change notification settings - Fork 498
/
Copy pathmain.py
160 lines (146 loc) · 5.69 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
# KidsCanCode - Game Development with Pygame video series
# Tile-based game - Part 15
# Simple Visual Effects (and a bug fix)
# Video link: https://youtu.be/ZapYMuV8f1g
import pygame as pg
import sys
from os import path
from settings import *
from sprites import *
from tilemap import *
# HUD functions
def draw_player_health(surf, x, y, pct):
if pct < 0:
pct = 0
BAR_LENGTH = 100
BAR_HEIGHT = 20
fill = pct * BAR_LENGTH
outline_rect = pg.Rect(x, y, BAR_LENGTH, BAR_HEIGHT)
fill_rect = pg.Rect(x, y, fill, BAR_HEIGHT)
if pct > 0.6:
col = GREEN
elif pct > 0.3:
col = YELLOW
else:
col = RED
pg.draw.rect(surf, col, fill_rect)
pg.draw.rect(surf, WHITE, outline_rect, 2)
class Game:
def __init__(self):
pg.init()
self.screen = pg.display.set_mode((WIDTH, HEIGHT))
pg.display.set_caption(TITLE)
self.clock = pg.time.Clock()
self.load_data()
def load_data(self):
game_folder = path.dirname(__file__)
img_folder = path.join(game_folder, 'img')
map_folder = path.join(game_folder, 'maps')
self.map = TiledMap(path.join(map_folder, 'level1.tmx'))
self.map_img = self.map.make_map()
self.map_rect = self.map_img.get_rect()
self.player_img = pg.image.load(path.join(img_folder, PLAYER_IMG)).convert_alpha()
self.bullet_img = pg.image.load(path.join(img_folder, BULLET_IMG)).convert_alpha()
self.mob_img = pg.image.load(path.join(img_folder, MOB_IMG)).convert_alpha()
self.wall_img = pg.image.load(path.join(img_folder, WALL_IMG)).convert_alpha()
self.wall_img = pg.transform.scale(self.wall_img, (TILESIZE, TILESIZE))
self.gun_flashes = []
for img in MUZZLE_FLASHES:
self.gun_flashes.append(pg.image.load(path.join(img_folder, img)).convert_alpha())
def new(self):
# initialize all variables and do all the setup for a new game
self.all_sprites = pg.sprite.LayeredUpdates()
self.walls = pg.sprite.Group()
self.mobs = pg.sprite.Group()
self.bullets = pg.sprite.Group()
# for row, tiles in enumerate(self.map.data):
# for col, tile in enumerate(tiles):
# if tile == '1':
# Wall(self, col, row)
# if tile == 'M':
# Mob(self, col, row)
# if tile == 'P':
# self.player = Player(self, col, row)
for tile_object in self.map.tmxdata.objects:
if tile_object.name == 'player':
self.player = Player(self, tile_object.x, tile_object.y)
if tile_object.name == 'zombie':
Mob(self, tile_object.x, tile_object.y)
if tile_object.name == 'wall':
Obstacle(self, tile_object.x, tile_object.y,
tile_object.width, tile_object.height)
self.camera = Camera(self.map.width, self.map.height)
self.draw_debug = False
def run(self):
# game loop - set self.playing = False to end the game
self.playing = True
while self.playing:
self.dt = self.clock.tick(FPS) / 1000.0 # fix for Python 2.x
self.events()
self.update()
self.draw()
def quit(self):
pg.quit()
sys.exit()
def update(self):
# update portion of the game loop
self.all_sprites.update()
self.camera.update(self.player)
# mobs hit player
hits = pg.sprite.spritecollide(self.player, self.mobs, False, collide_hit_rect)
for hit in hits:
self.player.health -= MOB_DAMAGE
hit.vel = vec(0, 0)
if self.player.health <= 0:
self.playing = False
if hits:
self.player.pos += vec(MOB_KNOCKBACK, 0).rotate(-hits[0].rot)
# bullets hit mobs
hits = pg.sprite.groupcollide(self.mobs, self.bullets, False, True)
for hit in hits:
hit.health -= BULLET_DAMAGE
hit.vel = vec(0, 0)
def draw_grid(self):
for x in range(0, WIDTH, TILESIZE):
pg.draw.line(self.screen, LIGHTGREY, (x, 0), (x, HEIGHT))
for y in range(0, HEIGHT, TILESIZE):
pg.draw.line(self.screen, LIGHTGREY, (0, y), (WIDTH, y))
def draw(self):
pg.display.set_caption("{:.2f}".format(self.clock.get_fps()))
# self.screen.fill(BGCOLOR)
self.screen.blit(self.map_img, self.camera.apply_rect(self.map_rect))
# self.draw_grid()
for sprite in self.all_sprites:
if isinstance(sprite, Mob):
sprite.draw_health()
self.screen.blit(sprite.image, self.camera.apply(sprite))
if self.draw_debug:
pg.draw.rect(self.screen, CYAN, self.camera.apply_rect(sprite.hit_rect), 1)
if self.draw_debug:
for wall in self.walls:
pg.draw.rect(self.screen, CYAN, self.camera.apply_rect(wall.rect), 1)
# pg.draw.rect(self.screen, WHITE, self.player.hit_rect, 2)
# HUD functions
draw_player_health(self.screen, 10, 10, self.player.health / PLAYER_HEALTH)
pg.display.flip()
def events(self):
# catch all events here
for event in pg.event.get():
if event.type == pg.QUIT:
self.quit()
if event.type == pg.KEYDOWN:
if event.key == pg.K_ESCAPE:
self.quit()
if event.key == pg.K_h:
self.draw_debug = not self.draw_debug
def show_start_screen(self):
pass
def show_go_screen(self):
pass
# create the game object
g = Game()
g.show_start_screen()
while True:
g.new()
g.run()
g.show_go_screen()