from random importrandintimportpyxelimportgzipclassApp:def __init__(self):
pyxel.init(160, 120, caption="Pyxel Jump")#pyxel.load("jump_game.pyxres")
self.score=0
self.player_x= 72self.player_y= -16self.player_vy=0
self.player_is_alive=True
self.far_cloud= [(-10, 75), (40, 65), (90, 60)]
self.near_cloud= [(10, 25), (70, 35), (120, 15)]
self.floor= [(i * 60, randint(8, 104), True) for i in range(4)]
self.fruit= [(i * 60, randint(0, 104), randint(0, 2), True) for i in range(4)]
pyxel.playm(0, loop=True)
pyxel.run(self.update, self.draw)defupdate(self):ifpyxel.btnp(pyxel.KEY_Q):
pyxel.quit()
self.update_player()for i, v inenumerate(self.floor):
self.floor[i]= self.update_floor(*v)for i, v inenumerate(self.fruit):
self.fruit[i]= self.update_fruit(*v)defupdate_player(self):if pyxel.btn(pyxel.KEY_LEFT) orpyxel.btn(pyxel.GAMEPAD_1_LEFT):
self.player_x= max(self.player_x - 2, 0)if pyxel.btn(pyxel.KEY_RIGHT) orpyxel.btn(pyxel.GAMEPAD_1_RIGHT):
self.player_x= min(self.player_x + 2, pyxel.width - 16)
self.player_y+=self.player_vy
self.player_vy= min(self.player_vy + 1, 8)if self.player_y >pyxel.height:ifself.player_is_alive:
self.player_is_alive=False
pyxel.play(3, 5)if self.player_y > 600:
self.score=0
self.player_x= 72self.player_y= -16self.player_vy=0
self.player_is_alive=Truedefupdate_floor(self, x, y, is_active):ifis_active:if(
self.player_x+ 16 >=xand self.player_x <= x + 40
and self.player_y + 16 >=yand self.player_y <= y + 8
and self.player_vy >0
):
is_active=False
self.score+= 10self.player_vy= -12pyxel.play(3, 3)else:
y+= 6x-= 4
if x < -40:
x+= 240y= randint(8, 104)
is_active=Truereturnx, y, is_activedefupdate_fruit(self, x, y, kind, is_active):if is_active and abs(x - self.player_x) < 12 and abs(y - self.player_y) < 12:
is_active=False
self.score+= (kind + 1) * 100self.player_vy= min(self.player_vy, -8)
pyxel.play(3, 4)
x-= 2
if x < -40:
x+= 240y= randint(0, 104)
kind= randint(0, 2)
is_active=Truereturn(x, y, kind, is_active)defdraw(self):
pyxel.cls(12)#draw sky
pyxel.blt(0, 88, 0, 0, 88, 160, 32)#draw mountain
pyxel.blt(0, 88, 0, 0, 64, 160, 24, 12)#draw forest
offset = pyxel.frame_count % 160
for i in range(2):
pyxel.blt(i* 160 - offset, 104, 0, 0, 48, 160, 16, 12)#draw clouds
offset = (pyxel.frame_count // 16) % 160
for i in range(2):for x, y inself.far_cloud:
pyxel.blt(x+ i * 160 - offset, y, 0, 64, 32, 32, 8, 12)
offset= (pyxel.frame_count // 8) % 160
for i in range(2):for x, y inself.near_cloud:
pyxel.blt(x+ i * 160 - offset, y, 0, 0, 32, 56, 8, 12)#draw floors
for x, y, is_active inself.floor:
pyxel.blt(x, y, 0, 0,16, 40, 8, 12)#draw fruits
for x, y, kind, is_active inself.fruit:ifis_active:
pyxel.blt(x, y, 0,32 + kind * 16, 0, 16, 16, 12)#draw player
pyxel.blt(
self.player_x,
self.player_y,
0,16 if self.player_vy > 0 else0,
0,16,16,12,
)#draw score
s = "SCORE {:>4}".format(self.score)
pyxel.text(5, 4, s, 1)
pyxel.text(4, 4, s, 7)
App()