space_war_v2.py

Created by wperez274

Created on August 01, 2022

8.97 KB

game.

//////////////////[ keys ]/////////////////////

keys [arrow] = move around key [ok] = shoot key [backspace] = pause/ resume ——————————————————– destroy enemies before they pass you. avoid crashing. grab fuel to regain some energy.

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,120)

F(0,0,322,222,bg)
F(0,47,322,3,"white")
F(0,172,322,3,"white")

while not K(KEY_OK):

  STR("SPACE WAR",110,80,"orange",bg)
  STR("( PRESS [OK] BUTTON TO START)",20,140,"cyan",bg)


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)

bg=(R(0,70),R(0,50),R(0,90))
area=1
game=True
psc=0
pl=3
pe=30
px=10
py=100
pw=9
ph=5
pc=(R(80,255),R(80,255),R(80,255))

foodx=R(300,500)
foody=R(38,165)
foodw=foodh=R(4,10)
foodc="cyan"
#----------------------

random_rect_x=R(400,800)
random_rect_y=R(40,170)
random_rect_w=R(40,150)
random_rect_h=R(40,150)
random_rect_c=(R(0,255),R(0,255),R(0,255))

#----------------------

#weapons
laser_fire=True
triple_laser_fire=False
wave_fire=False
random_char_fire=False
missle_fire=False


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

enemies_destroyed=0
et=0
efast=False
#enemy energy
ee=R(2,5)
ex=280
ey=100
ew=R(5,12)
eh=R(4,16)
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,20,(0,0,0))
F(0,183,322,40,(R(0,255),R(0,255),R(0,255)))


while game:
  
#----------------------
#rects
  mx-=R(0,3)
#enemy
  ex-=1
  ec2=(R(0,H),0,0)

  foodx-=2
#----------------------
  STR("Area: "+str(area),140,0,"white",BLACK)
  STR("Player[",5,0,pc,BLACK)
  STR("Enemy[",240,0,ec,BLACK)
#----------------------
  F(mx,my,mw,mh,mc)  
  F(mx+mw,my,2,mh,bg)  
  
  F(mx,my-2,mw,2,bg)  
  F(mx,my+mh,mw,2,bg)  
  
  F(mx,my,2,mh,choice([WHITE,CYAN]))
  F(mx+mw-2,my,2,mh,choice([WHITE,CYAN]))
  F(mx,my,mw,2,choice([WHITE,CYAN]))
  F(mx,my+mh-2,mw,2,choice([WHITE,CYAN]))
#----------------------
  F(ex,ey,ew,eh,ec)
  F(ex-2,ey,2,eh,ec2)
  
  F(ex+ew,ey,2,eh,bg)
  
  F(ex,ey+10,5,3,ec)

#erase spacecraft trail  
#??
  F(ex+6,ey,5,3,bg)
  F(ex+1,ey+5,5,3,bg)

  
  F(mx+mw+1,my,1,mh,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*5),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,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+ew<-R(20,50):
    ee=R(2,5)
    F(px,py,pw,ph,RED)
    F(0,0,322,36,RED)
    sleep(0.01)
    F(0,0,322,36,BLACK)
    F(258,4,round(ee)+10,11,BLACK)
    ex=R(350,500)
    ey=R(40,168)
    ew=R(4,20)
    ec=(R(0,H),R(0,H),R(0,H))
    pe-=5
    #[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")
    if laser_fire:
      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-=3
    F(px+pw,py,3,ph,bg)  
  
  if K(KEY_RIGHT):
    px+=3
    F(px-3,py,3,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([-1,0,1])
    
#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
    psc+=R(50,100)

  if enemies_destroyed>R(5,12):
    area+=1
    enemies_destroyed=0
    ee=R(2,6)
    ex=R(350,500)
    ey=R(37,168)
    ew=R(5,12)
    eh=R(4,16)

    
    ec=(R(0,H),R(0,H),R(0,H))
    bg=(R(0,255),R(0,255),R(0,255))


    F(0,0,322,222,bg)
    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,1,322,4,"red")
    F(0,218,322,4,"red")
    F(0,0,4,222,"red")
    F(317,0,4,222,"red")
    sleep(0.01)
    F(0,0,322,222,bg)
    
#player crashes into ground    
  if py+ph>180:
    py=182-ph
    pe-=0.2
    F(px,py,pw,ph,RED)
    F(0,1,322,4,"red")
    F(0,218,322,4,"red")
    F(0,0,4,222,"red")
    F(317,0,4,222,"red")
    sleep(0.01)
    F(0,0,322,222,bg)
    
  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.2
    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.2
  if px+pw>=mx and px<=mx+mw and py+ph>=my and py<=my+mh:
    F(px,py,pw,ph,RED)
    pe-=0.2
    
  if foodx+foodw<-R(5,25):
    foodx=R(1000,2000)
    foody=R(38,165)
    foodw=foodh=R(5,12)
    foodc=choice(['red','green','yellow','cyan','white','pink','orange','purple','gray'])


    
  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(4,10)
    foodc=choice(['red','green','yellow','cyan','white','pink','orange','purple','gray'])

    pe+=1+round(foodw/3)

    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)
  
  if random_rect_x+random_rect_w < -R(20,60):
    random_rect_x=R(400,800)
    random_rect_y=R(40,170)
    random_rect_w=R(40,150)
    random_rect_h=R(40,150)
    random_rect_c=(R(0,255),R(0,255),R(0,255))  
  
  
#top and bottom  
  F(0,33,322,3,choice([BLACK,WHITE,BLUE,CYAN,MAGENTA,RED,DARK_RED]))
  F(0,182,322,3,choice([BLACK,WHITE,BLUE,CYAN,MAGENTA,RED,DARK_RED]))

    
  random_rect_x-=1


  F(random_rect_x,random_rect_y,random_rect_w,2,random_rect_c)
  F(random_rect_x,random_rect_y+random_rect_h-2,random_rect_w,2,random_rect_c)
  F(random_rect_x,random_rect_y,2,random_rect_h,random_rect_c)
  F(random_rect_x+random_rect_w-2,random_rect_y,2,random_rect_h,random_rect_c)
  
  
  
  F(random_rect_x+random_rect_w,random_rect_y,1,random_rect_h,bg)

  if K(KEY_OK) and py+1 >= my and py+1 <= my+mh and px+pw<=mx+mw:
    F(mx,my,mw,mh,"red")

    mw-=choice([0,0,0,0,0,1])
    mh-=choice([0,0,0,0,0,1])
  
  if mw<20 or mh<20:
    F(mx,my,mw,mh,bg)
    mx=R(350,500)
    my=R(30,100)
    mw=R(30,80)
    mh=R(30,80)
    mc=(R(0,255),R(0,255),R(0,255))
    pe+=5
  

  if px+pw>=random_rect_x and px<=random_rect_x+random_rect_w and py+ph>=random_rect_y and py<=random_rect_y+random_rect_h:
    F(px,py,pw,ph,"red")
    F(0,1,322,4,"red")
    F(0,218,322,4,"red")
    F(0,0,4,222,"red")
    F(317,0,4,222,"red")
    sleep(0.01)
    F(0,0,322,222,bg)
    pe-=1  
  
  foodc=(R(0,255),R(0,255),R(0,255))    
   
#game over

sleep(0.5)
F(0,0,322,222,(0,0,0))

F(0,1,322,4,"red")
F(0,218,322,4,"red")
F(0,0,4,222,"red")
F(317,0,4,222,"red")

sx=450


sleep(0.2)


draw_string("Written by: Wilson",11,7,"gray",BLACK)
while pe<1 and not keydown(KEY_OK):
  F(0,1,322,5,"red")
  F(0,217,322,5,"red")
  F(0,0,5,222,"red")
  F(316,0,5,222,"red")

  sleep(0.05)
  sx-=8
  if sx<170:sx=170
  STR("GAME OVER",100,80,(R(0,255),0,R(0,55)),BLACK)
#  sleep(1.4)
  STR("SCORE:",100,120,"orange",BLACK)
#  sleep(1.5)


  STR(str(psc)+" ",sx,120,"cyan",BLACK)
  
  F(0,180,322,40,"red")

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.