Skip to content

Instantly share code, notes, and snippets.

@moshez
Created June 21, 2020 02:26
Show Gist options
  • Save moshez/c38c60946867df22ebd667bd27f851a4 to your computer and use it in GitHub Desktop.
Save moshez/c38c60946867df22ebd667bd27f851a4 to your computer and use it in GitHub Desktop.
Panda Jumping Game
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from twisted.internet import asyncioreactor\n",
"asyncioreactor.install()\n",
"__file__ = None"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def setup(scene):\n",
" global SCENE\n",
" SCENE = scene"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<Deferred at 0x7feb98226f90>"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import txppb\n",
"d = txppb.run(setup)\n",
"d.addBoth(print)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"import ppb"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"class Panda(ppb.Sprite):\n",
" pass"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"import ppb"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
"SCENE.add(Panda(position=(0,-4)))"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"class CreamChoco(ppb.Sprite):\n",
" pass"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"class CreamPink(ppb.Sprite):\n",
" pass"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"from ppb import keycodes\n",
"\n",
"DIRECTIONS = {keycodes.Left: ppb.Vector(-1,0), keycodes.Right: ppb.Vector(1,0),\n",
" keycodes.Up: ppb.Vector(0, 1), keycodes.Down: ppb.Vector(0, -1)}"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"from mzutil import set_in_class, collide"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [],
"source": [
"@set_in_class(Panda)\n",
"def on_button_released(self, mouse_event, signal):\n",
" if mouse_event.position.y < self.position.y:\n",
" return\n",
" how_far = (self.position - mouse_event.position).length\n",
" if how_far > 3:\n",
" return\n",
" creams = itertools.chain(mouse_event.scene.get(kind=CreamChoco), mouse_event.scene.get(kind=CreamPink)) \n",
" for cream in creams:\n",
" if collide(cream, self):\n",
" break\n",
" else:\n",
" if self.position.y > -4:\n",
" return\n",
" self.position = mouse_event.position\n"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"None\n"
]
}
],
"source": [
"import itertools\n",
"\n",
"@set_in_class(Panda)\n",
"def on_update(self, update_event, signal):\n",
" creams = itertools.chain(update_event.scene.get(kind=CreamChoco), update_event.scene.get(kind=CreamPink)) \n",
" for cream in creams:\n",
" if collide(cream, self):\n",
" break\n",
" else:\n",
" if self.position.y <= -4:\n",
" self.position = ppb.Vector(0, -4)\n",
" self.position += update_event.time_delta * 1 * DIRECTIONS[keycodes.Down]"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"CreamChoco.y_offset = 0.2"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"CreamPink.y_offset = 0.2"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
"SCENE.add(CreamChoco(position=(0,-2.3)))"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [],
"source": [
"SCENE.add(CreamPink(position=(2, -1.2)))"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [],
"source": [
"SCENE.add(CreamPink(position=(2, 1.2)))"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [],
"source": [
"SCENE.add(CreamPink(position=(-1.8, -1.2)))"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [],
"source": [
"SCENE.add(CreamChoco(position=(-.1, 2.5)))"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [],
"source": [
"panda, = SCENE.get(kind=Panda)\n",
"panda.position = ppb.Vector(3, 2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment