Permalink
Browse files

Space invaders game.

  • Loading branch information...
0 parents commit 67e1bfcf8ff86655dc94a9ca1fb1a1d693c4f308 @mithro committed Aug 13, 2009
@@ -0,0 +1,3 @@
+Code under Apache 2.0
+Graphics under CC-BY or Public Domain (your choice)
+Talk (and recordings) under CC-BY-SA
BIN alien.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN gun.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,109 @@
+import rabbyt
+import pyglet
+
+#
+import random
+
+win = pyglet.window.Window(800, 600)
+rabbyt.set_default_attribs()
+
+aliens = [rabbyt.Sprite('alien.png') for i in range(0, 10)]
+for i, alien in enumerate(aliens):
+ alien.y = win.height-alien.texture.height/2
+ alien.x = (alien.texture.width+5)*i+alien.texture.width/2-10
+
+
+# Create the thing which shoots....
+gun = rabbyt.Sprite('gun.png')
+gun.y = gun.texture.height/2
+
+# Make the gun move...
+from pyglet.window import mouse
+@win.event
+def on_mouse_motion(x, y, dx, dy):
+ gun.x += dx
+ # Make sure we can't go outside the window
+ gun.x = min(max(0, gun.x), win.width)
+
+pyglet.clock.schedule(rabbyt.add_time)
+
+@pyglet.clock.schedule
+def update(dt):
+ # Change the direction of the aliens
+ if aliens:
+ if aliens[0].x-aliens[0].texture.width/2 < 0:
+ print "going ---->"
+ for i, alien in enumerate(aliens):
+ alien.x = rabbyt.lerp(alien.x, win.width+alien.x, dt=5.0)
+ alien.y = rabbyt.lerp(alien.y, alien.y-alien.texture.height/2, dt=0.5)
+
+ if aliens[-1].x+aliens[-1].texture.width/2 > win.width:
+ print "going <---"
+ for i, alien in enumerate(aliens):
+ alien.x = rabbyt.lerp(alien.x, -win.width+alien.x, dt=5.0)
+ alien.y = rabbyt.lerp(alien.y, alien.y-alien.texture.height/2, dt=0.5)
+ else:
+ print "You win man!"
+
+ # Check for collisions
+ for a, b in rabbyt.collisions.aabb_collide([gun]+bullets_alien):
+ if a is gun or b is gun:
+ print "Game over man!"
+ # Game over man!
+ gun.alpha = 0
+
+ for a, b in rabbyt.collisions.aabb_collide(aliens+bullets_my):
+ if b in aliens:
+ bullets_my.remove(a)
+ aliens.remove(b)
+ if a in aliens:
+ bullets_my.remove(b)
+ aliens.remove(a)
+
+ # Clean up any bullets which have left the screen
+ for bullets in bullets_my, bullets_alien:
+ for bullet in list(bullets):
+ if bullet.y > win.height:
+ bullets.remove(bullet)
+ if bullet.y < 0:
+ bullets.remove(bullet)
+
+
+
+
+# Make it possible for me to shoo the aliens
+bullets_my = []
+@win.event
+def on_mouse_press(x, y, b, m):
+ if len(bullets_my) < 3:
+ bullet = rabbyt.Sprite("bullet.png")
+ bullet.x = gun.x
+ bullet.y = rabbyt.lerp(gun.y+gun.texture.height/2+bullet.texture.height/2,
+ win.height+10, dt=2.0)
+ bullets_my.append(bullet)
+
+# Make the aliens shoot at me
+bullets_alien = []
+def shoot(dt):
+ if aliens:
+ shooter = random.choice(aliens)
+ bullet = rabbyt.Sprite("bullet-alien.png")
+ bullet.x = shooter.x
+ bullet.rot = 180
+ bullet.y = rabbyt.lerp(shooter.y-shooter.texture.height/2-bullet.texture.height/2,
+ -10, dt=2.0)
+ bullets_alien.append(bullet)
+
+pyglet.clock.schedule_interval(shoot, 1)
+
+# Draw everything..
+@win.event
+def on_draw():
+ rabbyt.clear()
+ gun.render()
+ rabbyt.render_unsorted(aliens)
+ rabbyt.render_unsorted(bullets_my)
+ rabbyt.render_unsorted(bullets_alien)
+ win.flip()
+
+pyglet.app.run()
@@ -0,0 +1,51 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"
+"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
+<!-- Created with Sodipodi ("http://www.sodipodi.com/") -->
+<svg
+ id="svg602"
+ sodipodi:version="0.34"
+ xmlns="http://www.w3.org/2000/svg"
+ xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
+ xmlns:xlink="http://www.w3.org/1999/xlink"
+ width="295.28pt"
+ height="405.89pt"
+ xml:space="preserve"
+ sodipodi:docbase="/mnt/sda1/elementi_decorativi"
+ sodipodi:docname="/mnt/sda1/elementi_decorativi/green_alien_mask.svg"><defs
+ id="defs604"><linearGradient
+ id="linearGradient623"><stop
+ style="stop-color:#002f00;stop-opacity:1;"
+ offset="0"
+ id="stop624" /><stop
+ style="stop-color:#ffffff;stop-opacity:1;"
+ offset="1"
+ id="stop625" /></linearGradient><linearGradient
+ xlink:href="#linearGradient623"
+ id="linearGradient626"
+ x1="0.61450392"
+ y1="0.55952376"
+ x2="0.59923673"
+ y2="0.55952376"
+ spreadMethod="reflect" /></defs><sodipodi:namedview
+ id="base" /><path
+ style="font-size:12;fill:url(#linearGradient626);fill-rule:evenodd;"
+ d="M 352.4677 199.001 C 352.4677 214.8347 343.2547 222.2469 314.4022 234.3098 C 285.5496 246.3725 237.0574 263.086 182.9797 293.442 C 152.5123 276.5776 105.5297 262.7474 73.30416 248.1549 C 41.07876 233.5624 20.23737 208.0889 20.23737 192.2551 C 20.23737 176.4213 129.1685 225.3283 153.3833 241.0919 C 177.5981 256.8554 126.5899 233.2739 172.861 249.5943 C 179.1334 294.2648 208.4381 236.8694 241.7898 223.6426 C 329.6945 189.1853 352.4677 183.1672 352.4677 199.001 z "
+ id="path628"
+ sodipodi:nodetypes="cscscscsc"
+ transform="translate(0.964759,9.371185)" /><path
+ style="font-size:12;fill:#0f804c;fill-rule:evenodd;stroke:#000000;stroke-width:5.375;"
+ d="M 349.0949 157.2531 C 349.0949 211.2478 261.3825 187.1959 183.8229 268.1454 C 136.6195 207.4333 23.61027 216.6295 23.61027 162.6347 C 23.61027 108.6402 87.71236 -0.000276 185.5093 -0.000276 C 283.3064 -0.000276 349.0949 103.2585 349.0949 157.2531 z "
+ id="path622"
+ sodipodi:nodetypes="ccccc"
+ transform="translate(0.964759,9.371185)" /><path
+ style="font-size:12;fill:#0f804c;fill-rule:evenodd;"
+ d="M 8.529873 357.6899 C 8.529873 407.9905 74.33416 237.6638 101.6532 270.6271 C 128.9723 303.5907 227.598 390.1283 269.2855 390.1283 C 310.973 390.1283 218.6847 516.0832 246.0037 483.1196 C 273.3227 450.1562 310.4574 407.9905 310.4574 357.6899 C 310.4574 307.3893 379.5691 197.7657 352.25 164.8022 C 324.931 131.8386 310.973 340.223 269.2855 340.223 C 227.598 340.223 80.06526 195.9237 52.74616 228.8872 C 25.42707 261.8507 8.529873 307.3893 8.529873 357.6899 z "
+ id="path607"
+ sodipodi:nodetypes="cscscscsc"
+ transform="translate(0.964759,9.371185)" /><path
+ style="font-size:12;fill:#0f804c;fill-rule:evenodd;"
+ d="M 358.6405 357.6898 C 358.6405 407.9904 292.8363 237.6637 265.5173 270.627 C 238.1982 303.5906 139.5725 390.1282 97.88496 390.1282 C 56.19756 390.1282 148.4858 516.0831 121.1668 483.1195 C 93.84786 450.1561 56.71316 407.9904 56.71316 357.6898 C 56.71316 307.3892 -12.39863 197.7656 14.92047 164.8022 C 42.23956 131.8386 56.19756 340.2229 97.88496 340.2229 C 139.5725 340.2229 287.1052 195.9237 314.4243 228.8871 C 341.7433 261.8506 358.6405 307.3892 358.6405 357.6898 z "
+ id="path619"
+ sodipodi:nodetypes="cscscscsc"
+ transform="translate(0.964759,9.371185)" /></svg>
@@ -0,0 +1,222 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<!-- Created with Inkscape (http://www.inkscape.org/) -->
+<svg
+ xmlns:dc="http://purl.org/dc/elements/1.1/"
+ xmlns:cc="http://web.resource.org/cc/"
+ xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
+ xmlns:svg="http://www.w3.org/2000/svg"
+ xmlns="http://www.w3.org/2000/svg"
+ xmlns:xlink="http://www.w3.org/1999/xlink"
+ xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
+ xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
+ width="64px"
+ height="64px"
+ id="svg5527"
+ sodipodi:version="0.32"
+ inkscape:version="0.44.1"
+ sodipodi:docname="rocket.svg"
+ sodipodi:docbase="/archive/home/sasha/nodes-ai/guipics"
+ inkscape:export-filename="/archive/home/sasha/nodes-ai/guipics/rocket.png"
+ inkscape:export-xdpi="300"
+ inkscape:export-ydpi="300">
+ <defs
+ id="defs5529">
+ <linearGradient
+ id="linearGradient4652">
+ <stop
+ style="stop-color:#4842aa;stop-opacity:1;"
+ offset="0"
+ id="stop4654" />
+ <stop
+ style="stop-color:white;stop-opacity:1;"
+ offset="1"
+ id="stop4656" />
+ </linearGradient>
+ <linearGradient
+ id="linearGradient3755">
+ <stop
+ style="stop-color:#ff00b2;stop-opacity:1;"
+ offset="0"
+ id="stop3757" />
+ <stop
+ style="stop-color:#ff4d00;stop-opacity:1;"
+ offset="1"
+ id="stop3759" />
+ </linearGradient>
+ <linearGradient
+ id="linearGradient3743">
+ <stop
+ style="stop-color:fuchsia;stop-opacity:1;"
+ offset="0"
+ id="stop3745" />
+ <stop
+ id="stop3751"
+ offset="0.10650887"
+ style="stop-color:#ff2700;stop-opacity:1;" />
+ <stop
+ style="stop-color:#ffc500;stop-opacity:1;"
+ offset="1"
+ id="stop3747" />
+ </linearGradient>
+ <linearGradient
+ id="linearGradient6422">
+ <stop
+ style="stop-color:#f01;stop-opacity:1;"
+ offset="0"
+ id="stop6424" />
+ <stop
+ id="stop6430"
+ offset="0.72189349"
+ style="stop-color:#fa0;stop-opacity:1;" />
+ <stop
+ style="stop-color:#fefefe;stop-opacity:1;"
+ offset="1"
+ id="stop6426" />
+ </linearGradient>
+ <linearGradient
+ inkscape:collect="always"
+ xlink:href="#linearGradient6422"
+ id="linearGradient6428"
+ x1="27.282116"
+ y1="44.038261"
+ x2="56.872448"
+ y2="-1.0526485"
+ gradientUnits="userSpaceOnUse"
+ gradientTransform="matrix(0.609998,0.511593,-0.579925,0.684549,36.90183,-16.44058)" />
+ <linearGradient
+ inkscape:collect="always"
+ xlink:href="#linearGradient6422"
+ id="linearGradient2836"
+ x1="24.225807"
+ y1="46.589256"
+ x2="21.870232"
+ y2="16.952894"
+ gradientUnits="userSpaceOnUse"
+ gradientTransform="matrix(0.658682,0.607822,-0.607822,0.658682,29.51559,-10.06454)" />
+ <linearGradient
+ inkscape:collect="always"
+ xlink:href="#linearGradient6422"
+ id="linearGradient2844"
+ gradientUnits="userSpaceOnUse"
+ gradientTransform="matrix(-0.701548,-0.557799,-0.557799,0.701548,77.35793,31.08792)"
+ x1="24.225807"
+ y1="46.589256"
+ x2="21.870232"
+ y2="16.952894" />
+ <linearGradient
+ inkscape:collect="always"
+ xlink:href="#linearGradient6422"
+ id="linearGradient3739"
+ x1="22.525192"
+ y1="51.097511"
+ x2="51.614052"
+ y2="24.733877"
+ gradientUnits="userSpaceOnUse"
+ gradientTransform="matrix(0.896275,0,0,0.896275,4.529942,1.414171)" />
+ <radialGradient
+ inkscape:collect="always"
+ xlink:href="#linearGradient3743"
+ id="radialGradient3749"
+ cx="22.155714"
+ cy="48.436775"
+ fx="22.155714"
+ fy="48.436775"
+ r="10.481507"
+ gradientTransform="matrix(-0.790043,1.258185,-0.710071,-0.44587,77.70536,37.66495)"
+ gradientUnits="userSpaceOnUse" />
+ <radialGradient
+ inkscape:collect="always"
+ xlink:href="#linearGradient3755"
+ id="radialGradient3763"
+ cx="24.436749"
+ cy="49.889297"
+ fx="24.436749"
+ fy="49.889297"
+ r="5.1389585"
+ gradientTransform="matrix(-0.612706,-0.65414,0.862397,-0.807771,-0.217132,98.76782)"
+ gradientUnits="userSpaceOnUse" />
+ <linearGradient
+ inkscape:collect="always"
+ xlink:href="#linearGradient4652"
+ id="linearGradient4658"
+ x1="40.08128"
+ y1="23.104429"
+ x2="53.095104"
+ y2="17.814928"
+ gradientUnits="userSpaceOnUse" />
+ </defs>
+ <sodipodi:namedview
+ id="base"
+ pagecolor="#ffffff"
+ bordercolor="#666666"
+ borderopacity="1.0"
+ inkscape:pageopacity="0.0"
+ inkscape:pageshadow="2"
+ inkscape:zoom="3.7413723"
+ inkscape:cx="22.191005"
+ inkscape:cy="31.829632"
+ inkscape:current-layer="layer1"
+ showgrid="true"
+ inkscape:document-units="px"
+ inkscape:grid-bbox="true"
+ inkscape:window-width="1063"
+ inkscape:window-height="634"
+ inkscape:window-x="61"
+ inkscape:window-y="102" />
+ <metadata
+ id="metadata5532">
+ <rdf:RDF>
+ <cc:Work
+ rdf:about="">
+ <dc:format>image/svg+xml</dc:format>
+ <dc:type
+ rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
+ </cc:Work>
+ </rdf:RDF>
+ </metadata>
+ <g
+ id="layer1"
+ inkscape:label="Layer 1"
+ inkscape:groupmode="layer">
+ <path
+ style="opacity:1;fill:url(#radialGradient3749);fill-opacity:1;fill-rule:evenodd;stroke:black;stroke-width:0.89627522;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1"
+ d="M 21.849583,38.506344 C 10.116525,44.372874 16.951215,50.517008 14.506828,54.998384 C 12.062441,59.47976 12.440637,59.862544 17.859203,56.477987 C 21.723353,54.064354 29.952472,57.720543 30.956929,45.88864"
+ id="path3741"
+ sodipodi:nodetypes="cssc" />
+ <path
+ style="opacity:1;fill:url(#linearGradient2836);fill-opacity:1;fill-rule:evenodd;stroke:black;stroke-width:0.89627522;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1"
+ d="M 28.369254,21.695133 C 26.633351,19.549715 18.728813,20.587145 14.186921,21.641404 C 7.4268515,23.210543 0.9685838,27.9663 4.195005,28.660531 C 14.978327,30.980784 17.143691,34.655626 19.228798,33.882115"
+ id="path6432"
+ sodipodi:nodetypes="csss" />
+ <path
+ sodipodi:nodetypes="csss"
+ id="path2842"
+ d="M 46.148705,37.085812 C 48.534715,38.472582 48.720382,46.442744 48.374313,51.092528 C 47.859231,58.01318 44.14891,65.123733 42.968635,62.041737 C 39.023928,51.74111 35.060758,50.164231 35.505734,47.985245"
+ style="opacity:1;fill:url(#linearGradient2844);fill-opacity:1;fill-rule:evenodd;stroke:black;stroke-width:0.89627522;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1" />
+ <path
+ style="opacity:1;fill:url(#linearGradient6428);fill-opacity:1;fill-rule:evenodd;stroke:black;stroke-width:0.84513795;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1"
+ d="M 17.513314,32.870192 C 24.106909,19.600571 52.011194,1.5537027 56.652949,4.8079701 C 61.940039,8.514673 49.455808,39.659755 36.711423,49.397073 C 34.485266,51.097963 16.253117,35.406341 17.513314,32.870192 z "
+ id="path5535"
+ sodipodi:nodetypes="csss" />
+ <path
+ style="opacity:1;fill:url(#radialGradient3763);fill-opacity:1;fill-rule:evenodd;stroke:black;stroke-width:0.62739265;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1"
+ d="M 23.194008,41.318811 C 20.938292,42.233068 18.808505,48.623702 20.22698,49.842836 C 21.522465,50.956263 27.321019,48.537992 28.353862,45.018236"
+ id="path3753"
+ sodipodi:nodetypes="csc" />
+ <path
+ style="opacity:1;fill:url(#linearGradient3739);fill-opacity:1;fill-rule:evenodd;stroke:black;stroke-width:0.89627522;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1"
+ d="M 36.95881,29.117223 C 39.114121,31.058342 35.818096,35.961506 30.929321,41.339158 C 26.040548,46.716809 20.392736,52.822745 19.033305,51.442624 C 18.360955,50.760042 23.596161,41.339158 27.018303,37.428139 C 30.440444,33.51712 34.848808,27.216913 36.95881,29.117223 z "
+ id="path2846"
+ sodipodi:nodetypes="cssss" />
+ <path
+ sodipodi:type="arc"
+ style="opacity:1;fill:url(#linearGradient4658);fill-opacity:1.0;fill-rule:evenodd;stroke:black;stroke-width:0.99614413;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1"
+ id="path3765"
+ sodipodi:cx="45.838795"
+ sodipodi:cy="20.165817"
+ sodipodi:rx="3.8755832"
+ sodipodi:ry="3.7419424"
+ d="M 49.714378 20.165817 A 3.8755832 3.7419424 0 1 1 41.963212,20.165817 A 3.8755832 3.7419424 0 1 1 49.714378 20.165817 z"
+ transform="matrix(1.248315,0,0,1.364326,-11.64974,-7.950761)" />
+ </g>
+</svg>

0 comments on commit 67e1bfc

Please sign in to comment.