Skip to content
Snippets Groups Projects
Commit c07cf3d0 authored by Matthew Aked's avatar Matthew Aked
Browse files

Contains classes for the moving objects in the minigame

parent 80014ca1
No related branches found
No related tags found
No related merge requests found
class MovableObject:
def __init__(self, game_info_dict, x, y, w, h, char):
# Give the movable object access to the game map info
self.game_info_dict = game_info_dict
self.x = x
self.y = y
self.w = w
self.h = h
# character to represent object
self.char = char
self.body = self.create_body()
def create_body(self):
body = []
for y_change in range(0, self.h):
for x_change in range(0, self.w):
body.append([self.x + x_change, self.y + y_change])
return body
def add_to_map(self, game_map, char):
for body_part in self.body:
game_map[int(body_part[1])][int(body_part[0])] = char
return game_map
def check_if_move_valid(self, body_part):
if body_part[1] > self.game_info_dict["height"] or body_part[1] < self.game_info_dict["border_y"]:
return False
elif body_part[0] > self.game_info_dict["width"] or body_part[0] < self.game_info_dict["border_x"]:
return False
return True
def handle_invalid_move(self, game_map):
return self.add_to_map(game_map, " "), False
def move(self, game_map, change_x, change_y):
body = []
for body_part in self.body:
new_body_part = [body_part[0]+change_x, body_part[1]+change_y]
if self.check_if_move_valid(new_body_part):
body.append(new_body_part)
else:
return self.handle_invalid_move(game_map)
game_map = self.add_to_map(game_map, " ")
self.body = body
game_map = self.add_to_map(game_map, self.char)
return game_map, True
class Enemy(MovableObject):
def __init__(self, game_info_dict, x, y, w, h, char):
super().__init__(game_info_dict, x, y, w, h, char)
self.direction = 1
def handle_invalid_move(self, game_map):
if self.direction == 1:
self.direction = -1
else:
self.direction = 1
return game_map, True
def move(self, game_map, change_x, change_y):
return super().move(game_map, change_x, int(change_y*self.direction))
class Arrow(MovableObject):
def __init__(self, game_info_dict, x, y, w, h, vel_x, vel_y, char):
super().__init__(game_info_dict, x, y, w, h, char)
self.vel_x = vel_x
self.vel_y = vel_y
self.acc = 0.05
def apply_velocity(self, dt):
#change_x = self.vel_x * dt * 10
#change_y = self.vel_y * dt * 10
change_x = self.vel_x
change_y = self.vel_y
self.vel_y += self.acc
return change_x, change_y
def arrow_move(self, game_map, dt):
change_x, change_y = self.apply_velocity(dt)
return super().move(game_map, change_x, change_y)
class PowerBar(MovableObject):
def __init__(self, game_info_dict, max_length, x, y, w, h, char):
super().__init__(game_info_dict, x, y, w, h, char)
self.max_length = max_length
self.power_index = 0
self.going_up = True
def reset_bar(self):
self.w = 0
self.body = self.create_body()
def get_power(self):
return self.w
def next_level(self, game_map):
if self.going_up:
if self.w + 1 > self.max_length:
self.going_up = False
else:
self.w += 1
else:
if self.w < 1:
self.going_up = True
else:
self.w -= 1
self.add_to_map(game_map, " ")
self.body = self.create_body()
return self.add_to_map(game_map, "")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment