forked from chriszf/oop_lesson
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame_ref.py
More file actions
103 lines (83 loc) · 2.29 KB
/
game_ref.py
File metadata and controls
103 lines (83 loc) · 2.29 KB
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
import core
import pyglet
from pyglet.window import key
from core import GameElement
import sys
#### DO NOT TOUCH ####
GAME_BOARD = None
DEBUG = False
KEYBOARD = None
PLAYER = None
######################
GAME_WIDTH = 5
GAME_HEIGHT = 5
class Rock(GameElement):
IMAGE = "Rock"
SOLID = True
class Gem(GameElement):
IMAGE = "BlueGem"
SOLID = False
def interact(self, player):
player.inventory.append(self)
GAME_BOARD.draw_msg("You just acquired a gem!")
class Character(GameElement):
IMAGE = "Cat"
def __init__(self):
GameElement.__init__(self)
self.inventory = []
def next_pos(self, direction):
if direction == "up":
return (self.x, self.y-1)
elif direction == "down":
return (self.x, self.y+1)
elif direction == "left":
return (self.x-1, self.y)
elif direction == "right":
return (self.x+1, self.y)
return None
def keyboard_handler():
direction = None
if KEYBOARD[key.UP]:
direction = "up"
if KEYBOARD[key.DOWN]:
direction = "down"
if KEYBOARD[key.LEFT]:
direction = "left"
if KEYBOARD[key.RIGHT]:
direction = "right"
if direction:
next_location = PLAYER.next_pos(direction)
next_x = next_location[0]
next_y = next_location[1]
existing_el = GAME_BOARD.get_el(next_x, next_y)
if existing_el:
existing_el.interact(PLAYER)
if existing_el is None or not existing_el.SOLID:
GAME_BOARD.del_el(PLAYER.x, PLAYER.y)
GAME_BOARD.set_el(next_x, next_y, PLAYER)
def initialize():
"""Put game initialization code here"""
rock_positions = [
(2, 1),
(1, 2),
(3, 2),
(2, 3),
]
rocks = []
for pos in rock_positions:
rock = Rock()
GAME_BOARD.register(rock)
GAME_BOARD.set_el(pos[0], pos[1], rock)
rocks.append(rock)
rocks[-1].SOLID = False
for rock in rocks:
print rock
global PLAYER
PLAYER = Character()
GAME_BOARD.register(PLAYER)
GAME_BOARD.set_el(2, 2, PLAYER)
print PLAYER
GAME_BOARD.draw_msg("This game is wicked awesome.")
gem = Gem()
GAME_BOARD.register(gem)
GAME_BOARD.set_el(3, 1, gem)