/
rpg_game.py
executable file
·149 lines (120 loc) · 4.65 KB
/
rpg_game.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
#!/usr/bin/python
import sys
import time
import pygame
import chars
import menu
import npc_mod
import sounds
import tiled_screen
def main():
pygame.init()
pygame.mouse.set_visible(0)
screen = tiled_screen.Screen(rows=9, columns=13, tile_width=64, tile_height=64)
import maps.castle
start_zone = maps.castle.Castle()
screen.set_zone(start_zone)
screen.update_grid()
clock = pygame.time.Clock()
start = time.time()
game_on = True
pending_inputs = []
keys_down = set([])
time_elapsed = 0
while game_on:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_on = False
continue
if "key" not in event.dict:
continue
key = event.dict["key"]
if event.type == pygame.KEYDOWN and key == 27:
#Escape
game_on = False
continue
if screen.game_state == tiled_screen.WORLD and \
event.type == pygame.KEYDOWN:
keys_down.add(key)
if key == 273:
#Up key
screen.walking_up()
elif key == 276:
#Left
screen.walking_left()
elif key == 274:
#Down
screen.walking_down()
elif key == 275:
#Right
screen.walking_right()
elif key == 32:
#Spacebar
screen.set_walking_speed(2.5*tiled_screen.MPS)
elif key == 122:
#Z
if screen.motioning:
screen.stop_walking()
pygame.event.post(event)
continue
sounds.beep_sound.play()
screen.open_menu()
continue
if screen.game_state == tiled_screen.FIGHT and \
event.type == pygame.KEYDOWN:
action = screen.zone.combat_manager.input(screen, event, screen.total_time)
if action == "fight_over":
screen.zone.combat_manager.current_fight = None
screen.game_state = tiled_screen.WORLD
screen.zone.music()
continue
if event.type == pygame.KEYUP:
if key in keys_down:
keys_down.remove(key)
if key == 32:
screen.set_walking_speed(tiled_screen.MPS)
if len(keys_down.intersection((273, 274, 275, 276))) == 0:
#Any array key
screen.stop_walking()
if screen.game_state == tiled_screen.MENU and \
event.type == pygame.KEYDOWN:
if key == 120:
#X
screen.close_menu()
elif key in (273, 274, 275, 276):
screen.menu[-1].move_selection(key, screen.total_time)
elif key == 122:
#Z
response = screen.menu[-1].selected()
if not response:
screen.draw(time_elapsed)
time_elapsed = clock.tick(tiled_screen.MAX_FPS)
continue
if response["action"] == "close":
for num in range(response["num_to_close"]):
screen.close_menu()
elif response["action"] == "close_all":
screen.close_all_menus()
elif response["action"] == "talk":
row, col = screen.get_facing_square()
npc = screen.zone.get_npc_at(row, col)
if not npc:
tile_info = screen.zone.get_tile_info(row, col)
dialogue = tile_info.get("dialogue", [["There is no one here."]])
talk_menu = menu.TalkMenu(dialogue)
screen.open_menu(talk_menu)
continue
npc.talk(screen)
elif response["action"] == "menu":
screen.open_menu(response["menu"])
screen.draw(time_elapsed)
time_elapsed = clock.tick(tiled_screen.MAX_FPS)
end = time.time()
print "Quitting..."
print "Total Time = %.2f" % (end-start)
print "Total Frames = %d" % (screen.total_frames)
print "Average FPS = %.2f" % (screen.total_frames / (end - start))
print "Average FPS = %.2f" % (clock.get_fps(),)
pygame.quit()
if __name__ == "__main__":
main()