-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgraphics.py
176 lines (145 loc) · 7.88 KB
/
graphics.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import keyboard
import time
import objects
import key
try:
from tkinter import * # handles python 3 compiler
except ImportError:
from Tkinter import * # handles python 2 compiler
from PIL import Image, ImageTk
class Graphics:
def __init__(self):
self.root = Tk() #This creates a window, but it won't show up
self.root.wm_title("CSCI 166 Project by: Joshua Holland") #Makes the title that will appear in the top left
self.root.geometry("%dx%d+%d+%d" % (objects.board.width * objects.board.size, objects.board.height * objects.board.size, 130, 100))
self.playerGraphic = None
self.job = None
self.canvas = Canvas(self.root, width = objects.board.width * objects.board.size, height= objects.board.height * objects.board.size)
# initialize keyboard listeners and assign them to player movement function
self.root.bind("<Up>", lambda event: objects.player.move(event, newDirection = "Up"))
self.root.bind("<Down>", lambda event: objects.player.move(event, newDirection = "Down"))
self.root.bind("<Left>", lambda event: objects.player.move(event, newDirection = "Left"))
self.root.bind("<Right>", lambda event: objects.player.move(event, newDirection = "Right"))
self.canvas.pack()
self.keys = dict() # stores keys of type key.id, which is integer, and returns a canvas opal object.
self.wormholes = list()
def drawBackground(self):
self.canvas.pack(fill=BOTH, expand=1)
img = Image.open("./assets/background.jpg")
img = img.resize((objects.board.height*objects.board.size, objects.board.width*objects.board.size))
self.background = ImageTk.PhotoImage(img)
self.backgroundPic = self.canvas.create_image(0, 0, anchor=NW, image=self.background)
self.canvas.pack(fill=BOTH, expand=1)
def drawLine(self, x0, y0, x1, x2):
# renders line behind player after movement
line = self.canvas.create_line(x0, y0, x1, x2, width = 3, fill='red')
self.root.update()
time.sleep(.1)
self.canvas.delete(line)
def teleportPlayer(self, newX, newY):
boardSize = objects.board.size
playerSize = objects.player.size
x1 = newX * boardSize + (boardSize/2)
y1 = newY * boardSize + (boardSize/2)
self.canvas.coords(self.playerGraphic, x1, y1)
def moveCanvas(self, xAmount, yAmount):
# Called from player.move()
self.canvas.move(self.playerGraphic, xAmount, yAmount)
def moveKey(self, key, xAmount, yAmount):
if key not in self.keys:
return
self.canvas.move(self.keys[key], xAmount, yAmount)
def removeKey(self, key):
self.canvas.delete(self.keys[key])
del self.keys[key]
def updateBoard(self):
tileSize = objects.board.size
boardHeight = objects.board.height
boardWidth = objects.board.width
# redraw the board
self.canvas.delete("all")
self.drawBackground()
for i in range(boardWidth):
for j in range(boardHeight):
xpos = i * tileSize
ypos = j * tileSize
if objects.board.tiles[i][j].isWall():
rect = self.canvas.create_rectangle(xpos, ypos, xpos + tileSize, ypos + tileSize, fill='black')
#elif objects.board.tiles[i][j].isEmpty():
#self.canvas.itemconfig(rect, fill='white')
elif objects.board.tiles[i][j].isLava():
rect = self.canvas.create_rectangle(xpos, ypos, xpos + tileSize, ypos + tileSize, fill='red')
elif objects.board.tiles[i][j].isWormhole():
self.canvas.pack(fill=BOTH, expand=1)
if objects.board.tiles[i][j].direction == "up":
img = Image.open("./assets/enterUp.png")
elif objects.board.tiles[i][j].direction == "down":
img = Image.open("./assets/enterDown.png")
elif objects.board.tiles[i][j].direction == "left":
img = Image.open("./assets/enterLeft.png")
elif objects.board.tiles[i][j].direction == "right":
img = Image.open("./assets/enterRight.png")
img = img.resize((60,60))
self.wormholes.append(ImageTk.PhotoImage(img))
self.canvas.create_image( xpos + (tileSize/2),
ypos + (tileSize/2),
anchor=CENTER, image=self.wormholes[len(self.wormholes) - 1])
self.canvas.pack(fill=BOTH, expand=1)
elif objects.board.tiles[i][j].isWormholeExit():
self.canvas.pack(fill=BOTH, expand=1)
if objects.board.tiles[i][j].direction == "up":
img = Image.open("./assets/exitUp.png")
elif objects.board.tiles[i][j].direction == "down":
img = Image.open("./assets/exitDown.png")
elif objects.board.tiles[i][j].direction == "left":
img = Image.open("./assets/exitLeft.png")
elif objects.board.tiles[i][j].direction == "right":
img = Image.open("./assets/exitRight.png")
img = img.resize((60,60))
self.wormholes.append(ImageTk.PhotoImage(img))
self.canvas.create_image( xpos + (tileSize/2),
ypos + (tileSize/2),
anchor=CENTER, image=self.wormholes[len(self.wormholes) - 1])
self.canvas.pack(fill=BOTH, expand=1)
elif objects.board.tiles[i][j].isExit():
rect = self.canvas.create_rectangle(xpos, ypos, xpos + tileSize, ypos + tileSize, fill='gold')
def drawPlayer(self):
tileSize = objects.board.size
playerSize = objects.player.size
if self.playerGraphic:
del self.playerGraphic
#image stuff
self.canvas.pack(fill=BOTH, expand=1)
img = Image.open("./assets/star.jpg")
img = img.resize((playerSize,playerSize))
self.pic = ImageTk.PhotoImage(img)
self.playerGraphic = self.canvas.create_image( objects.player.x * tileSize + tileSize/2,
objects.player.y * tileSize + tileSize/2,
anchor=CENTER, image=self.pic)
self.canvas.pack(fill=BOTH, expand=1)
def drawKeys(self):
boardSize = objects.board.size
playerSize = objects.player.size
self.keys.clear() # clear any keys so that we can reset graphics without closing program.
for key in objects.board.keys:
self.keys[key] = self.canvas.create_oval(key.x *boardSize + (boardSize - playerSize) / 2,
key.y*boardSize + (boardSize - playerSize) / 2,
key.x*boardSize + boardSize - (boardSize - playerSize) / 2,
key.y*boardSize + boardSize - (boardSize - playerSize) / 2,
fill='yellow')
def initializeGraphics(self):
# this function initializes the graphics and starts tkinter's main loop.
# it begins concurrent execution of main() inside main.py
# This is called from start() in main.py
import main
self.updateBoard()
self.drawPlayer()
self.drawKeys()
self.job = self.root.after(1000, main.main) # after() allows us to say "after 10 ms, run main()". This allows us to make a game loop inside main.py
self.root.mainloop()
def redrawBoard(self):
# this function is called on by main when a game ends and the game must restart
import main
self.updateBoard()
self.drawPlayer()
self.drawKeys()