-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
148 lines (132 loc) · 5.92 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
from sprites import *
from tilemap import *
import sqlite3
from pc import *
conn = sqlite3.connect('PokePy.db')
cursor = conn.cursor()
class Game:
def __init__(self):
pg.init()
self.screen = pg.display.set_mode((WIDTH, HEIGHT))
pg.display.set_caption(TITLE)
icon = pg.image.load('Assets\Pokeball.png')
pg.display.set_icon(icon)
self.clock = pg.time.Clock()
self.current_map = 'Hometown.tmx'
self.load_data()
self.battle = Battle()
self.pc = PC()
self.draw_debug = False
self.battle_encounter = False
self.pc_opened = False
# Loading Sounds
game_folder = path.dirname(__file__)
sound_folder = path.join(game_folder, 'Sound Files')
self.bg_music = {}
for sound_type in BG_MUSIC:
self.bg_music[sound_type] = pg.mixer.Sound(path.join(sound_folder, BG_MUSIC[sound_type]))
self.bg_music[sound_type].set_volume(0.5)
self.sound_effects = {}
for sound_type in EFFECTS_SOUNDS:
self.sound_effects[sound_type] = pg.mixer.Sound(path.join(sound_folder, EFFECTS_SOUNDS[sound_type]))
def load_data(self):
game_folder = path.dirname(__file__)
img_folder = path.join(game_folder, 'Assets')
map_folder = path.join(img_folder, 'Maps')
self.map = TiledMap(path.join(map_folder, self.current_map))
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()
def new(self):
# initialize all variables and do all the setup for a new game
self.all_sprites = pg.sprite.Group()
self.walls = pg.sprite.Group()
for tile_object in self.map.tmxdata.objects:
if tile_object.type == 'wall':
Obstacle(self, tile_object.x, tile_object.y, tile_object.width, tile_object.height)
if tile_object.name == 'player' and not self.battle_encounter: # on game start
self.player = Player(self, tile_object.x, tile_object.y)
elif tile_object.name == 'player' and self.battle_encounter: # after battle encounter
self.player = Player(self, self.player.pos.x, self.player.pos.y)
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
self.bg_music['Map_Music'].play(-1)
while self.playing:
self.dt = self.clock.tick(FPS) / 1000
self.events()
self.update()
self.draw()
def update(self):
# update portion of the game loop
self.all_sprites.update()
self.camera.update(self.player)
def draw(self):
self.screen.fill(BGCOLOR)
self.screen.blit(self.map_img, self.camera.apply_rect(self.map_rect))
for sprite in self.all_sprites:
self.screen.blit(sprite.image, self.camera.apply(sprite))
if self.draw_debug:
for wall in self.walls:
pg.draw.rect(self.screen, RED, self.camera.apply_rect(wall.rect), 1)
pg.draw.rect(self.screen, RED, self.camera.apply_rect(self.player.rect), 1)
if self.battle_encounter:
self.bg_music['Map_Music'].stop()
self.battle.start_battle()
self.bg_music['Map_Music'].play(-1)
self.new()
self.battle_encounter = False
if self.pc_opened:
self.pc.start_pc()
self.pc_opened = False
pg.display.flip()
def quit(self):
pg.quit()
sys.exit()
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
# Interact check
if event.key == pg.K_e:
if self.current_map == "Hometown.tmx":
if 580 <= self.player.pos.x <= 600 and 980 <= self.player.pos.y <= 995:
self.current_map = "House.tmx"
self.bg_music['Map_Music'].stop()
self.bg_music['Pokecenter_Music'].play(-1)
self.load_data()
self.new()
elif self.current_map == "House.tmx":
if 512 <= self.player.pos.x <= 576 and self.player.pos.y == 690:
self.current_map = "Hometown.tmx"
self.bg_music['Pokecenter_Music'].stop()
self.bg_music['Map_Music'].play(-1)
self.load_data()
self.new()
if 530 <= self.player.pos.x <= 560 and self.player.pos.y == 159:
print("Pokemon healed")
cursor.execute('''UPDATE User_Pokemon SET Current_HP=
(SELECT HP FROM Pokemon WHERE Pokemon.Pokemon_Name = User_Pokemon.Pokemon_Name)''')
conn.commit()
if 340 <= self.player.pos.x <= 360 and self.player.pos.y == 127:
self.pc_opened = not self.pc_opened
# Battle check
if 1400 <= self.player.pos.x <= 1800 and 1200 <= self.player.pos.y <= 1550 \
and self.current_map == "Hometown.tmx":
if self.player.encounter_chance > 8 and (self.player.vel.x != 0 or self.player.vel.y != 0):
self.battle_encounter = True
def show_start_screen(self):
pass
gameobj = Game()
gameobj.show_start_screen()
while True:
gameobj.new()
gameobj.run()