Skip to content

Instantly share code, notes, and snippets.

@vgel
Created July 20, 2012 19:55
Show Gist options
  • Save vgel/3152857 to your computer and use it in GitHub Desktop.
Save vgel/3152857 to your computer and use it in GitHub Desktop.
simple platformer. run python a.py mapfile
import pygame as pg,sys
pg.init()
w=pg.display.set_mode((100,)*2)
o=list(open(sys.argv[1]).read().replace("\n",""))
px=py=50
j=0
k=pg.key.get_pressed
t=lambda x,y:o[x/5+y/5*20]
z=lambda i:min(95,max(0,i))
while True:
f=pg.time.get_ticks()
for i,e in enumerate(o):pg.draw.rect(w,(255,)*3 if e=='#' else (0,)*3,pg.Rect(i%20*5,i/20*5,5,5))
pg.event.pump()
if k()[pg.K_w]and j==0 and t(px,py+6)=='#':j=20
if k()[pg.K_a]and(px%5>0 or(t(px-5,py)!='#'and t(px-5,py+5)!='#')):px=z(px-1)
if k()[pg.K_d]and(px%5>0 or(t(px+5,py)!='#'and t(px+5,py+5)!='#')):px=z(px+1)
if j>0 and (py%5>0 or (t(px,py-5)!='#'and t(px+4,py-5)!='#')):
py=z(py-1)
j-=1
else:
j=0
if py%5<4 or(t(px,py+6)!='#'and t(px+4,py+6)!='#'):py=z(py+1)
pg.draw.rect(w,(200,)*3,pg.Rect(px,py,5,6))
pg.display.flip()
d=pg.time.get_ticks()-f
pg.time.delay(16-d)
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
...##...............
...#................
#..#....#....#...#..
##.###.#####.###.###
####################
####################
####################
####################
####################
####################
####################
@2bt
Copy link

2bt commented Jul 22, 2012

Hey nice work. You should come to http://golf.shinh.org/ and golf with us there!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment