space_war.py

Created by wperez274

Created on April 06, 2023

6.57 KB

Cool game for Numworks calculator. The goal is to destroy oncoming enemy spaceships and prevent them from reaching the left side of the screen (your Base).. Avoid the weird rectangles that keep getting in your way, and avoid getting destroyed by them. Gain energy by destroying spaceships and catching the random little squares that show up.

**[ KEYS ]**

Key [Arrows] = Move, fly. Key [OK] = Shoot Key [Backspace] = Pause/ Resume game. ——————————————

Good luck!

Sincerely,

Wilson


from math import *
from random import *
from kandinsky import *
from ion import *
from time import *
from kandinsky import fill_rect as F
from kandinsky import draw_string as STR
from ion import keydown as K
from random import randint as R
from random import choice as CH

H=255
SW=322
SH=222
Mc=200

bg=(0,0,0)
F(0,0,322,222,bg)

F(0,0,322,50,(0,40,0))
F(0,170,322,70,(0,40,0))
F(0,48,322,2,(0,255,0))
F(0,170,322,2,(0,255,0))


while not K(KEY_EXE):


  STR("SPACE WAR",110,80,"cyan",bg)
  STR("( PRESS [OK] BUTTON TO START)",20,140,"white",bg)
  bg=(R(0,35),R(0,50),R(0,25))

startimer=0
#amount of stars
nb=R(10,20)

st = [0]*nb
pr = [0]*nb
#background maybe eraser
NR="black"

def point():
  return [randrange(322),35,randrange(1,10)]

for i in range(nb): st[i] = point()
F(0,0,H,SH,NR)

#color definitions via RGB Decimal
BLACK=(0,)*3
RED=(H,0,0)
DARK_RED=(125,0,0)
WHITE=(H,)*3
BLUE=(0,0,H)
YELLOW=(H,H,0)
MAGENTA=(H,0,H)
GREEN=(0,H,0)
BLACK=(0,)*3
CYAN=(0,H,H)

world=1
game=True
psc=0
pl=3
pe=30
px=10
py=100
pw=9
ph=5
pc=WHITE

foodx=R(300,500)
foody=R(38,165)
foodw=foodh=R(6,15)
foodc=CYAN

#dark matter
mx=R(350,500)
my=R(30,100)
mw=R(20,30)
mh=R(20,50)
mc=(R(0,255),R(0,255),R(0,255))

enemies_destroyed=0
et=0
efast=False
#enemy energy
ee=5
ex=280
ey=100
ew=15
eh=15
ec=(R(0,H),R(0,H),R(0,H))

F(0,0,SW,SH,bg)

F(0,0,SW,22,(0,0,0)) 
#main loop

F(0,0,322,36,(R(0,255),R(0,255),R(0,255)))
F(0,183,322,40,(R(0,255),R(0,255),R(0,255)))

while game:
  
  
  foodc=(R(0,150),R(0,150),R(0,150))
#rects
  mx-=R(0,3)

#enemy
  ex-=1
  foodx-=2


  STR(" World:"+str(world),128,2,YELLOW,BLACK)
  STR("Player[",5,0,CYAN,BLACK)
  STR("Enemy[",240,0,"green",BLACK)

  F(mx,my,mw,mh,mc)  

  F(mx,my,2,mh,choice([DARK_RED,RED]))
  F(mx+mw-2,my,2,mh,choice([DARK_RED,RED]))
  F(mx,my,mw,2,choice([DARK_RED,RED]))
  F(mx,my+mh-2,mw,2,choice([DARK_RED,RED]))
  
  F(ex,ey,ew,eh,ec)
  F(ex+ew,ey,2,eh,bg)
  
  F(ex-5,ey+5,5,3,ec)
  F(ex,ey+10,5,3,ec)
  F(mx+mw+1,my,1,mh,bg) 
#erase spacecraft trail  
  F(ex+6,ey,5,3,bg)
  F(ex+1,ey+5,5,3,bg)

  
  F(73,4,round(pe/3),11,"cyan")

  F(px,py,pw,ph,pc)
  F(px+4,py+1,4,2,CYAN)
  F(298,4,round(ee),11,RED)
  F(ex+6,ey+10,5,3,bg)
  F(ex-5,ey+5,5,3,"red")

  F(foodx,foody,foodw,foodh,foodc)
  F(foodx+foodw+1,foody,2,foodh,bg)
  
#enemy random speedup
  et+=0.01
  if et>R(2,5):
    et=0
    efast=R(0,1)

  
  
  if py>=ey-3 and py<=ey+6:
    ey+=choice([R(-3,0),R(0,3)])
   
  if ey<42:ey=42
  if ey+eh>172:ey=172-eh
  
    
  
#enemy reaches the gates
  if ex<-R(10,30):
    ee=R(3,8)
    F(258,4,round(ee)+10,11,BLACK)
    ex=R(250,400)
    ey=R(40,168)
    
    ew=R(6,30)
    
    ec=(R(0,H),R(0,H),R(0,H))
    pe-=3
    F(73+int(pe/3),4,2,11,BLACK)
    F(px,py,pw,ph,RED)
#[0,0,1] is a CH list of True or False
    efast=CH([0,0,1])
    et=0
#player shoots              
  if K(KEY_OK):
    F(px+pw-2,py,2,ph,"red")
    F(px+pw,py+1,SW-px,3,(R(0,H),R(0,H),R(0,H)))
    sleep(0.001)
    F(px+pw,py+1,SW-px,3,bg)
    
  if K(KEY_LEFT):
    px-=2
    F(px+pw,py,2,ph,bg)  
  
  if K(KEY_RIGHT):
    px+=2
    F(px-2,py,2,ph,bg)  
  
  
  if K(KEY_UP):
    py-=2
    F(px,py+ph,pw,2,bg)  
    
  if K(KEY_DOWN):
    py+=2
    F(px,py-2,pw,2,bg)  
#enemy is hit.
  if K(KEY_OK) and py+1 >= ey and py+1 <= ey+eh and px+pw<=ex+ew:
    F(298,2,25,15,BLACK)
    for i in range(R(2,10)):
      for j in range(R(2,10)):
        i=ex+R(0,7)
        j=ey+R(0,7)
        F(i,j,R(2,5),R(2,5),CH([CYAN,BLACK,bg,ec]))
    
    F(258,4,round(ee),11,BLACK)
#enemy is hit
    ee-=0.06
    psc+=R(0,2)
    pe+=0.02
    ey+=choice([R(-2,0),R(0,2)])
    
#enemy destroyed    
  if ee<1:
    ee=R(3,8)
    ex=R(250,400)
    ey=R(37,168)
    ew=R(6,30)
    ec=(R(0,H),R(0,H),R(0,H))
    enemies_destroyed+=1
    pe+=3
    pc=(R(50,Mc),R(50,Mc),R(50,Mc))
    psc+=R(10,20)

  if enemies_destroyed>R(3,8):
    world+=1
    enemies_destroyed=0
    ee=R(3,7)
    ex=R(250,400)
    ey=R(37,168)
    ew=R(6,30)
    ec=(R(0,H),R(0,H),R(0,H))
    bg=(R(0,35),R(0,50),R(0,35))
    F(0,23,SW,SH,bg)
   
    F(0,0,322,36,(R(0,255),R(0,255),R(0,255)))
    F(0,183,322,40,(R(0,255),R(0,255),R(0,255)))

    pe+=1
    pc=(R(50,Mc),R(50,Mc),R(50,Mc))
    psc+=R(10,20)
#player crashes into ceiling    
  if py<=36:
    py=36
    pe-=0.2
    F(px,py,pw,ph,RED)
    F(0,34,322,2,CYAN)

#player crashes into ground    
  if py+ph>180:
    py=182-ph
    pe-=0.2
    F(px,py,pw,ph,RED)
    F(0,182,322,2,CYAN)
  if px<1:
    px=1
  if px+pw>SW:
    px=SW-pw
  if pe<1:
    game=False
#pause game    
  if K(KEY_BACKSPACE):
    STR("(PAUSED)",110,100,(R(0,H),R(0,H),R(0,H)),bg)
    while K(KEY_BACKSPACE):
      pass
    while not K(KEY_BACKSPACE):
      pass
    while K(KEY_BACKSPACE):
      STR("        ",110,100,bg,bg)
      pass

#player low on energy
  if pe<10:
    F(px,py+ph,1,1,CH([(120,0,0),(H,0,0),bg,BLACK]))
    F(px,py-1,pw,1,bg)
    py+=R(0,1)

  if efast:
    for i in range(R(1,2)):
      ex-=1
      F(ex+ew,ey,2,eh,bg)

  startimer+=0.1
  if startimer>R(3,7):
    startimer=0
  
    for i in range(nb):
      st[i][1] += randrange(1,int(50/st[i][2]))
      z = st[i][2]
      Sx = st[i][0]
      Sy = min(240,st[i][1])
      if Sy >= 170: st[i] = point()
      if pr[i] != 0: set_pixel(pr[i][0],pr[i][1],bg)
      pr[i] = [int(Sx),int(Sy)]
    
      r=R(0,Mc)
      g=R(0,Mc)
      b=R(0,Mc)
      set_pixel(pr[i][0],pr[i][1],color(r,g,b))
    
  
  if mx+mw<0-R(10,20):
    mx=R(500,1000)
    mw=R(20,70)
    mh=R(20,70)
    my=R(40,180-mh)
    mc=(R(0,255),R(0,255),R(0,255))

    F(0,36,322,144,bg)

    
  
  
  if px+pw>=mx and px<=mx and py+ph>=my and py<=my+mh:
    px-=1
    pe-=0.5
    F(0,0,SW,20,bg)
    F(px,py,pw,ph,"red")    
  
  my+=R(-2,2)
  
  
  
  
  
  if my<36:my=36
  if my+mh>180:my=180-mh
  
  
  
  if px>mx and px+pw<=mx+1 and py+ph>=my and py<=my+mh:
    px-=1
    pe-=0.3
  if px+pw>=mx and px<=mx+mw and py+ph>=my and py<=my+mh:
    F(px,py,pw,ph,RED)
    pe-=0.1
    
    
  mw+=choice([0,0,1,-1])
  mh+=choice([0,0,1,-1])
  
  
  if foodx+foodw<-R(5,25):
    foodx=R(1000,2000)
    foody=R(38,165)
    foodw=foodh=R(6,15)

    
  if foodx+foodw>=px and foodx<=px+pw and foody+foodh>=py and foody<=py+ph:
    F(px,py,pw,ph,GREEN)
    F(foodx,foody,foodw,foodh,bg)
    foodx=R(1000,2000)
    foody=R(38,165)
    foodw=foodh=R(6,15)
    pe+=1+round(foodw/5)

    F(px-10,py+1,30,2,(R(0,255),R(0,255),R(0,255)))
    F(px+3,py-10,2,25,(R(0,255),R(0,255),R(0,255)))
    
    
    sleep(0.1)
    F(px-10,py+1,30,2,bg)
    F(px+3,py-10,2,25,bg)
    
    

#game over

sleep(0.5)
F(0,0,322,222,(0,0,0))
STR("GAME OVER",100,100,RED,BLACK)      
sleep(1.4)
STR("SCORE:",100,140,(R(150,255),R(150,255),R(150,255)),BLACK)
sleep(1.5)
STR(str(psc),170,140,"cyan",BLACK)

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.