-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKirbyGame.py
240 lines (211 loc) · 9.52 KB
/
KirbyGame.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
from cmu_112_graphics import *
import random
from Kirby import *
from Enemy import *
from Block import *
def appStarted(app):
startP = 'startPage.png' #screenshot taken from https://kbhgames.com/game/kirby-nightmare-dream-land
app.startPage = app.loadImage(startP)
app.startPage = app.scaleImage(app.startPage,0.8)
url = 'https://bghq.com/bgs/gba/k/knd/001.png' #this image was taken from https://bghq.com/bgs/gba/k/knd/001.png
app.back1 = app.loadImage(url)
app.back1 = app.scaleImage(app.back1,6)
app.back2 = app.loadImage('https://bghq.com/bgs/gba/k/knd/043.png')
app.back2 = app.scaleImage(app.back2,6)
app.back3 = app.loadImage('https://bghq.com/bgs/gba/k/knd/080.png')
app.back3 = app.scaleImage(app.back3,6)
app.spriteCounter = 0
app.scrollMargin = 100
app.isGameOver = False
app.scrollX = 0
app.normBlocks = []
app.enemy1 = Enemy('chilly',1,800,500)
app.enemy2 = Enemy('metaknight',1,-300,500)
app.enemy3 = Enemy('monkey',1,1000,500)
app.float = app.scaleImage(app.loadImage('fly.png'),3) #all sprites were hand drawn on https://www.pixilart.com/draw/kingd-sr2686c7792a4ae
app.floatL = app.float.transpose(Image.FLIP_LEFT_RIGHT)
app.hide = app.scaleImage(app.loadImage('hidden.png'),4)
app.walk = app.scaleImage(app.loadImage('walk.png'),3)
app.walkL = app.walk.transpose(Image.FLIP_LEFT_RIGHT)
app.suck = app.scaleImage(app.loadImage('inhale.png'),3)
app.suckL = app.suck.transpose(Image.FLIP_LEFT_RIGHT)
app.fight = app.scaleImage(app.loadImage('attack.png'),3)
app.fightL = app.fight.transpose(Image.FLIP_LEFT_RIGHT)
app.knight = app.scaleImage(app.loadImage('knight.png'),3)
app.knightR = app.knight.transpose(Image.FLIP_LEFT_RIGHT)
app.knightAR = app.scaleImage(app.loadImage('knightA.png'),4)
app.knightA = app.knightAR.transpose(Image.FLIP_LEFT_RIGHT)
app.sparked = app.scaleImage(app.loadImage('sparked.png'),3)
app.sparkedA = app.scaleImage(app.loadImage('sparkedA.png'),3)
app.sparky = app.scaleImage(app.loadImage('sparky.png'),3)
app.chilly = app.scaleImage(app.loadImage('chilly.png'),3)
app.metaknight = app.scaleImage(app.loadImage('metaknight.png'),3)
app.bat = app.scaleImage(app.loadImage('bat.png'),3)
app.monkey = app.scaleImage(app.loadImage('monkey.png'),3)
app.block = app.scaleImage(app.loadImage('block.png'),6)
app.badBlock = app.scaleImage(app.loadImage('badBlock.png'),6)
app.lifeBlock = app.scaleImage(app.loadImage('life.png'),6)
app.mystery = app.scaleImage(app.loadImage('mystery.png'),6)
app.kingD = ''
app.kirb = Kirby('normal',3,500,app.height*0.8)
app.kirb.nearbyEnemies.append(app.enemy1)
app.kirb.nearbyEnemies.append(app.enemy2)
app.kirb.nearbyEnemies.append(app.enemy3)
app.kingJump = app.scaleImage(app.loadImage('kingDJump.png'),3)
app.king = app.scaleImage(app.loadImage('kingD.png'),3)
app.kingCrush = app.scaleImage(app.loadImage('kingDSmash.png'),3)
app.kingCrushR = app.kingCrush.transpose(Image.FLIP_LEFT_RIGHT)
app.boss = Boss('kingD',5,200,200)
app.boss.target = app.kirb
app.bounds = app.kirb.getKirbyBounds(app)
app.enemy3.target = app.kirb
app.badBlocks = []
app.lifeBlocks = 2
app.collected = 0
app.blocks = []
app.ready = False
app.level = 1
back = 'bossFight.jpeg' #this image was taken from https://www.deviantart.com/princesskirbyswirl/art/The-Magalor-final-boss-fight-background-851147131
app.background = app.loadImage(back)
app.kirb.nearbyEnemies.append(app.boss)
app.placedE = []
app.enemyKinds = ['chilly','metaknight','sparky','monkey','bat']
for i in range(app.level*3):
app.blocks.append((Block('normal',(i+1)*300,400)))
for i in range(app.level*6):
app.blocks.append((badBlock('normal',(i+1)*400,400)))
for i in range(app.level*2):
app.blocks.append((lifeBlock('normal',(i+1)*250,400)))
for i in range(app.level):
app.blocks.append((hideBlock('normal',(i+1)*250,400)))
for i in range(app.level*2):
newE = Enemy(app.enemyKinds[random.randint(0,3)],1,(i+1)*200,400)
app.placedE.append(newE)
app.kirb.nearbyEnemies.append(newE)
app.mode = 'startScreen'
app.timer = 0
def startScreen_redrawAll(app, canvas):
canvas.create_image(app.width//2,app.height//2,
image=ImageTk.PhotoImage(app.startPage))
def startScreen_keyPressed(app,event):
if event.key == "Space":
app.mode = 'boss'
def startScreen_mousePressed(app,event):
if event.x > 300 and event.x < 750 and event.y > 575 and event.y < 625:
app.mode = 'game'
def addBlock(app):
if app.scrollX >= 800:
app.level += 1
app.blocks = []
for i in range(app.level*2):
app.blocks.append((Block('normal',(i+1)*300,400)))
for i in range(app.level*3):
app.blocks.append((badBlock('normal',(i+1)*400,400)))
for i in range(app.level*2):
app.blocks.append((lifeBlock('normal',(i+1)*250,400)))
app.lifeBlocks += app.level*2
app.scrollX = 0
if app.level == 3 and app.scrollX == 600:
app.blocks.append(Portal('normal',800,500))
def game_timerFired(app):
app.bounds = app.kirb.getKirbyBounds(app)
app.kirb.timerFired(app)
if app.kirb.lives == 0:
app.isGameOver = True
addBlock(app)
app.timer += 0.5
def game_keyPressed(app, event):
app.kirb.timerFired(app)
app.kirb.keyPressed(app,event)
for enemy in app.kirb.nearbyEnemies:
enemy.keyPressed(app,event)
if app.ready:
app.mode = 'boss'
def boundsIntersect(app, boundsA, boundsB):
# return l2<=r1 and t2<=b1 and l1<=r2 and t1<=b2
(ax0, ay0, ax1, ay1) = boundsA
(bx0, by0, bx1, by1) = boundsB
return ((ax1 >= bx0) and (bx1 >= ax0) and
(ay1 >= by0) and (by1 >= ay0))
def game_redrawAll(app, canvas):
if app.level == 1:
canvas.create_image(500,400, image=ImageTk.PhotoImage(app.back1))
elif app.level == 2:
canvas.create_image(500,400, image=ImageTk.PhotoImage(app.back2))
elif app.level == 3:
canvas.create_image(500,400, image=ImageTk.PhotoImage(app.back3))
canvas.create_text(60,50,text=f'LEVEL {app.level}', font='Helvetica 26',
fill = 'blue')
canvas.create_text(60,80,text=f'TIME PLAYED: {app.timer}', font='Helvetica 26',
fill = 'blue')
app.kirb.redrawAll(app,canvas)
app.enemy1.redrawAll(app,canvas)
app.enemy1.timerFired(app)
app.enemy2.redrawAll(app,canvas)
app.enemy2.timerFired(app)
app.enemy3.redrawAll(app,canvas)
app.enemy3.timerFired(app)
# (x0,y0,x1,y1) = app.blocks[0].getBlockBounds(app)
# canvas.create_rectangle(x0,y0,x1,y1,outline='orange', width = 5)
# canvas.create_rectangle(app.enemy2.getXpos()-100,app.enemy2.getYpos()-90,app.enemy2.getXpos()+40,app.enemy2.getYpos(),outline='black')
# draw the dots, shifted by the scrollX offset
for block in app.blocks:
block.redrawAll(app,canvas)
for enemy in app.placedE:
enemy.redrawAll(app,canvas)
enemy.timerFired(app)
# draw the instructions and the current scrollX
x = app.width/2
canvas.create_text(x, 20, text='Use arrows to move left or right',
fill='black')
canvas.create_text(x, 40, text=f'app.scrollX = {app.scrollX}',
fill='black')
if app.kirb.lives == 10:
canvas.create_rectangle(app.width//4,app.height//4,
3*app.width//4, 3*app.height//4,fill = 'purple')
canvas.create_text(app.width//2,app.height//2,fill = 'white',
text ='Congrats! You have collected enough life blocks! You are ready for battle.')
if app.ready == True:
canvas.create_rectangle(app.width//4,app.height//4,
3*app.width//4, 3*app.height//4,fill = 'purple')
canvas.create_text(app.width//2,app.height//2,fill = 'white',
text ='you are ready! click control-q to battle the boss KINGD')
if app.isGameOver:
pass
# canvas.create_rectangle(app.width//4,app.height//4,
# 3*app.width//4, 3*app.height//4,fill = 'black')
# canvas.create_text(app.width//2,app.height//2,fill = 'white',
# text ='GAME OVER!')
# (x0,y0,x1,y1) = app.bounds
# canvas.create_rectangle(x0,y0,x1,y1, outline = 'black', fill = 'yellow')
def boss_timerFired(app):
# app.kirb.timerFired(app)
app.boss.timerFired(app)
app.kirb.timerFired(app)
if app.kirb.lives == 0:
app.isGameOver = True
def boss_keyPressed(app, event):
app.kirb.keyPressed(app,event)
app.boss.keyPressed(app,event)
def boss_redrawAll(app, canvas):
canvas.create_image(app.width//2,app.height//4,
image=ImageTk.PhotoImage(app.scaleImage(app.background,2.5)))
app.boss.redrawAll(app,canvas)
# app.boss.timerFired(app)
app.kirb.redrawAll(app,canvas)
# app.kirb.timerFired(app)
# for block in app.blocks:
# block.redrawAll(app,canvas)
if app.isGameOver:
pass
# canvas.create_rectangle(app.width//4,app.height//4,
# 3*app.width//4, 3*app.height//4,fill = 'black')
# canvas.create_text(app.width//2,app.height//2,fill = 'white',
# text ='GAME OVER!')
elif app.boss.lives == 0:
pass
# canvas.create_rectangle(app.width//4,app.height//4,
# 3*app.width//4, 3*app.height//4,fill = 'black')
# canvas.create_text(app.width//2,app.height//2,fill = 'white',
# text ='YOU WIN!')
runApp(width = 1000,height = 800)