5
\$\begingroup\$

Your challenge is to create a basic version of Space Invaders in Python

Rules

  • Program must be not written in Py3K
  • All levels must be randomly generated
  • There must be a HUD showing the players score

The winner would be the answer which uses the least amount of code

Some Questions

Q: How detailed do the aliens and other elements have to be?

A: The aliens should be Block ASCII

Q: Can the bunkers be simplified to rectangles?

A: Yes, as long as they are filled

Q: How many enemies/bunkers do there have to be?

A: Number of Enemy's are chosen by random. 4 bunkers

Q: Do we need to implement the "mystery ship" that goes across the top of the screen?

A: No

Q:Do the symbols/characters that make up the laser cannon and the aliens need to be different?

A: Yes

Q: Do they need different shapes?

A: Yes

Q: Does it matter how fast the bullets/lasers travel?

A: No

\$\endgroup\$
  • 1
    \$\begingroup\$ I think this question is missing some basic requirements: objective winning criterion and a clear specification of the task (read the faq). Please try to clarify your question. \$\endgroup\$ – Howard Dec 27 '11 at 10:05
  • \$\begingroup\$ @Howard Ive improve'd it a little bit.. What else is it missing? \$\endgroup\$ – Amith KK Dec 27 '11 at 10:32
  • 10
    \$\begingroup\$ -0. These requirements are too broad for meaningful code golf. What counts as Space Invaders? What are the rules of the game? Also, why limit this to Python? \$\endgroup\$ – Steven Rumbalski Dec 27 '11 at 16:30
  • \$\begingroup\$ @StevenRumbalski 1. The classic game 2. See:Wikipedia 3. Because it is a challenge :? \$\endgroup\$ – Amith KK Dec 27 '11 at 16:49
  • 1
    \$\begingroup\$ How simplified is the final version allowed to be? The answer to this question can ultimately decide the winner because certain simplifications can make it much easier to do in less code. How detailed do the aliens and other elements have to be? Can the bunkers be simplified to rectangles? How many enemies/bunkers do there have to be? Do we need to implement the "mystery ship" that goes across the top of the screen? Do the symbols/characters that make up the laser cannon and the aliens need to be different? Do they need different shapes? Does it matter how fast the bullets/lasers travel? \$\endgroup\$ – PhiNotPi Dec 28 '11 at 0:28
17
\$\begingroup\$

3060 3031 characters

OKAY, this was fun. But I very highly doubt that my code is short enough to compete, or if it's basic enough. I'm new to code golf but regardless, I had fun and that's what matters. I'm too used to object-oriented programming so a lot of my code is probably just too much objects... Game runs solely in the console, and I shortened most of the code with the exec''''''.replace() method. Complete with input of your name, random enemy firing, walls, aliens are randomly generated, worth 50 points each. each time you fire you lose one point so that you lose points if you just hold space in one spot to beat the level. Enemy speed increases each level, capped at every 0.2 seconds. Prints the level number, and game over at the bottom, your name,lives, and score at the top left. you have 3 lives. Ungolfed version is ~9500 characters, but I'll leave that out of here for now

instructions: fire with space, move with a and d, exit with x

let me know what think, if I can improve on anything

edit: forgot that msvcrt is Windows-Only. perhaps I can make a unix version too...

exec'''import threading as t
import msvcrt,Queue,time,random,os
u=raw_input('name:')
r=range
@Z:
$~(S):S.w=78;S.h=30;S.X();S.l='';S.Q=[];S.q=[];S.m=''
$J(S,x,y,c):S.q.append([x,y,c])
$X(S):S.s=[[' '^y in r(78)]^x in r(30)]
$V(S):
?s='';S.X()
?^x,y,z in S.q:S.s[y][x]=z
?S.q=[]
?^x in S.s:
? ^y in x:s+=y
? s+='\\n'
?s+=S.m.center(S.w)
?if s!=S.l:os.system('cls');S.l=s;print s
@E(Exception):pass
@C:
$~(S,g,c,p):S.g=g;S.c=c;S.p=p;S.s=[]
$M(S,a,d):
?if (a<0 and S.p[d]==0) or (a>0 and S.p[d]==(S.g.z.h if d else S.g.z.w)-1):raise E()
?S.p[d]+=a
$X(S):S.s.remove(S)
@A(C):
$~(S,g):C.~(S,g,'A',[39,28])
$M(S,a,d=0):
?try:C.M(S,a,d)
?except E:pass
@Y(A):
$~(S,g,p):C.~(S,g,'Y',p);S.s=g.a
@B(C):
$~(S,g,p,n):C.~(S,g,'|'if n<0 else'*',[p[0],p[1]+n]);S.s=g.b;S.d=n;S.a=0 if n<0 else 1
$M(S):
?try:C.M(S,S.d,1)
?except E:S.X()
$b(S):S.g.z.J(S.p[0],S.p[1],'X');S.g.z.V();S.X()
$co(S):
?^w in S.g.w:
? if S.p==w.p:S.b();w.X()
?if S.a:
? if S.p==S.g.p.p:S.b();S.g.d()
?else:
? ^a in S.g.a:
??if S.p==a.p:S.b();S.g.P(50);a.X()
@W(C):
$~(S,g,p):C.~(S,g,'#',p);S.s=g.w
@I(t.Thread):
$~(S,g):t.Thread.~(S);S.f=1;S.c={'a':g._g4,'d':g._g3,'x':g._X,' ':g._fi};S.start()
$run(S):
?while S.f:
? c=msvcrt.getch()
? if c in S.c:S.c[c]()
$X(S):S.f=0
@G:
$~(S):S.z=Z();S.x=3.0;S.c=0;S.b=[];S.Q=Queue.Queue();S.I=I(S);S.g2=0;S.g1=0;S.do=0;S.f=1;S.v=0;S.l=3;S.ti=[];S.G(1);S.O()
$O(S):
?while S.f:
? time.sleep(0.02)
? if not S.a:S.G(1)
? S.ch();S.B();S.u()
$u(S):
?S.z.X();S.P(0)
?s=u+' '+`S.l`
?^i in r(len(s)):S.z.J(i,0,s[i])
?^i in S.a+S.b+[S.p]+S.w:S.z.J(i.p[0],i.p[1],i.c)
?S.z.V()
$ch(S):
?try:
? while not S.Q.empty():S.Q.get_nowait()();S.Q.task_done()
?except Queue.Empty:pass
$cl(S):
?try:
? while not S.Q.empty():S.Q.get_nowait();S.Q.task_done()
?except Queue.Empty:pass
$_g4(S):
?if not S.g2:S.g2=1;S.Q.put(S.g4)
$g4(S):S.p.M(-1);S.g2=0
$_g3(S):
?if not S.g1:S.g1=1;S.Q.put(S.g3)
$g3(S):S.p.M(1);S.g1=0
$_X(S):S.I.X();S.Q.put(S.X)
$X(S):S.f=0;S.I.X();S.st()
$_fi(S):
?if not S.do:S.do=1;S.Q.put(S.fi)
$fi(S):S.b.append(B(S,S.p.p[:],-1));S.do=0;S.P(-1)
$_af(S):S.Q.put(S.af)
$af(S):c=random.choice(S.a);S.b.append(B(S,c.p[:],1));S.nt(random.uniform(0.1,2),S._af)
$nt(S,n,f):x=t.Timer(n,f);S.ti.append(x);x.start()
$st(S):
?^i in S.ti:i.cancel()
$G(S,n=0):
?S.st();S.cl();S.p=A(S);S.a=[];S.w=[];S.b=[]
?^x in[[i,o]^i in r(30)^o in r(3,10)]:
? if random.randint(0,99)%4==0:S.a.append(Y(S,x))
?S._m();S._af();f=[]
?^x in [6,27,46,65]:f+=[[z,y]^z in r(x,x+6)^y in r(23,26)]
?^i in f:S.w.append(W(S,i))
?S.v+=n;S.z.m='Level %s'%S.v;S.u()
?if S.x>0.2:S.x=1.6 - S.v*0.1
?S.m1=0;S.m2=0;S.k=1;time.sleep(3)
$_m(S):S.Q.put(S.m)
$m(S):
?x=0
?if S.m2:S.m2=0;S.m1=0;S.k=-S.k;x=1
?^i in S.a:i.M(*(abs(S.k),1)if x else(S.k,0))
?if S.m1==47:S.m2=1
?else:S.m1+=1
?S.nt(S.x,S._m)
$B(S):
?^i in S.b:i.M();i.co()
$d(S):
?S.l-=1
?if S.l==0:S.z.m='Game Over!';S.X();return
?S.G(0)
$P(S,n):
?S.c+=n if S.c+n>0 else 0;x=`S.c`
?^n in r(len(x)):S.z.J(n,1,x[n])
G()'''.replace('@','class ').replace('?','  ').replace('$',' def ').replace('^','for ').replace('~','__init__')

what the screen looks like:

Blazer 3
488            |

      YY  Y         YY  Y Y Y     Y
      YY  Y   YY  Y  Y           Y
      Y   Y    Y      Y    YY
                           Y   Y  Y
       YY Y   Y   Y Y             Y
           Y  YY YX Y Y
       Y       Y |                 Y




                   |


         *

                    |



      ####           |     ######             ######             ######
      ######               ######             ######             ######
      ######               ######             ######             ######


                     A

                                   Level 1

edit: managed to remove something I accidentally left in I wasn't using anymore

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.