-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_file.py
147 lines (116 loc) · 5.35 KB
/
game_file.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
from player_file import player_class
from game_state_file import game_state_class
import pygame
import config
import math
from tileset_reader import sprite_sheet_reader
class game:
def __init__(self, screen):
self.screen = screen
self.objects = []
self.game_state = game_state_class.NONE
self.map = []
self.camera = [0, 0]
self.position_change = [0, 0]
self.reader = sprite_sheet_reader('imgs/pokemon_tileset.png', 32)
# to find the coordinates, use geeqie to find the top left corner of the tile you want
map_tile_image["G"] = self.reader.get_tile(round(96 / 32),round(2240 / 32))
map_tile_image["W"] = self.reader.get_tile(round(134 / 32), round(3686 / 32))
map_tile_image["GWTL"] = self.reader.get_tile(round(118 / 32), round(4208 / 32))
def set_up(self):
player = player_class(1,1)
self.player = player
self.objects.append(player)
print("Setup")
self.game_state = game_state_class.RUNNING
self.load_map("01")
def update(self):
# temporary screen clearance
self.screen.fill(config.BLACK)
#print("update")
self.render_map(self.screen)
for object in self.objects:
object.render(self.screen, self.camera)
self.move_unit( self.player, self.position_change)
def handle_events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.game_state = game_state_class.ENDED
# handle key events
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
self.game_state = game_state_class.ENDED
if event.key == pygame.K_LEFT or event.key == ord('a'):
self.position_change[0] += -0.1
if event.key == pygame.K_RIGHT or event.key == ord('d'):
self.position_change[0] += 0.1
if event.key == pygame.K_UP or event.key == ord('w'):
self.position_change[1] += -0.1
if event.key == pygame.K_DOWN or event.key == ord('s'):
self.position_change[1] += 0.1
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == ord('a'):
self.position_change[0] += 0.1
if event.key == pygame.K_RIGHT or event.key == ord('d'):
self.position_change[0] += -0.1
if event.key == pygame.K_UP or event.key == ord('w'):
self.position_change[1] += 0.1
if event.key == pygame.K_DOWN or event.key == ord('s'):
self.position_change[1] += -0.1
def load_map(self, file_name):
with open('maps/'+file_name+'.txt') as map_file:
for line in map_file:
tiles=[]
for i in range(0, len(line)-1, 2):
tiles.append(line[i])
self.map.append(tiles)
def render_map(self, screen):
self.determine_camera()
y_pos = 0
for line in self.map:
x_pos = 0
for tile in line:
image = map_tile_image[tile]
rect = pygame.Rect(x_pos*config.SCALE-(self.camera[0]*config.SCALE),
y_pos*config.SCALE-(self.camera[1]*config.SCALE),
config.SCALE,
config.SCALE)
screen.blit(image, rect)
x_pos += 1
y_pos += 1
def move_unit(self, unit, position_change):
if position_change is None:
return
new_position = [unit.position[0] + position_change[0], unit.position[1] + position_change[1]]
# Test for collision with map boundaries
if new_position[0] < 0 or new_position[0] > (len(self.map[0])-1):
return
if new_position[1] < 0 or new_position[1] > (len(self.map)-1):
return
# Test for collision with water tiles
if self.map[round(new_position[1])][round(new_position[0]+0.25)] == 'W' or \
self.map[round(new_position[1]+0.5)][round(new_position[0] + 0.25)] == 'W' or \
self.map[round(new_position[1])][round(new_position[0] - 0.25)] == 'W' or \
self.map[round(new_position[1]+0.5)][round(new_position[0] - 0.25)] == 'W':
return
unit.update_position(new_position)
def determine_camera(self):
max_y_position=len(self.map) - config.SCREEN_HEIGHT/config.SCALE
y_position = self.player.position[1] - math.ceil(round(config.SCREEN_HEIGHT/config.SCALE/2))
if y_position <= max_y_position and y_position >= 0:
self.camera[1] = y_position
elif y_position < 0:
self.camera[1] = 0
else:
self.camera[1] = max_y_position
max_x_position = len(self.map[0]) - config.SCREEN_WIDTH / config.SCALE
x_position = self.player.position[0] - math.ceil(round(config.SCREEN_WIDTH / config.SCALE / 2))
if x_position <= max_x_position and x_position >= 0:
self.camera[0] = x_position
elif x_position < 0:
self.camera[0] = 0
else:
self.camera[0] = max_x_position
## To add more tiles, add image to imgs folder and place here with a code for it
map_tile_image = {
}