planet_z_v4.py

Created by wperez274

Created on February 16, 2022

7.12 KB


# GAME BY: WILSON
from math import *
from random import *
from kandinsky import *
from ion import *
from time import *
from kandinsky import fill_rect as F
from random import randint as R
from ion import keydown as K

H=255

CYAN=(0,H,H)
RED=(H,0,0)
DARKRED=(135,0,0)
DARKGREEN=(0,135,0)
WHITE=(H,)*3
BLACK=(0,)*3
GREEN=(0,H,0)
BLUE=(0,0,H)
#screen width and height
SCRW=322
SCRH=222
SCRA=SCRW*SCRH
#3 different platforms
GX=0
GY=R(22,190)
GW=R(20,140)
GH=300
GC=(R(0,H),R(0,H),R(0,H))

GX2=R(80,180)
GY2=R(22,190)
GW2=R(20,140)
GH2=300
GC2=(R(0,H),R(0,H),R(0,H))

GX3=R(200,290)
GY3=R(40,190)
GW3=R(20,140)
GH3=300
GC3=(R(0,H),R(0,H),R(0,H))

area=1
world=1
game=True
#back
bg=(R(0,70),R(0,20),R(0,70))

#player
psc=0
pe=9
plife=3
px=GX+10
py=GY-R(16,25)
pw=10
ph=15
pc=BLUE

edash=False
edash_timer=0
eattacks=False
eruns=False
edefeated=False
dooropen=False
etimer=0
echase=False
edir=0
#enemys energy
ex=R(180,280)
ey=R(40,100)
ew=R(9,28)
eh=R(8,25)
ee=0.5*ew*eh
ec=(R(0,H),R(0,H),R(0,H))

fballdir=R(3,4)
fballx=70
fbally=120
fballw=10
fballh=10
fballc=RED

#player faces left or right
facingleft=False
facingright=True

doorx=-10
doory=-10
doorw=16
doorh=30
doorc=BLACK

vel=1
xVel=0
jumping=False
jumpCount=0
maxJump=22
falling=True

def pause():
  draw_string("(PAUSED)",110,100,(randint(0,255),randint(0,255),randint(0,255)),bg)
  while K(KEY_OK):
    pass
  while not K(KEY_OK):
    pass
  while K(KEY_OK):
    draw_string("        ",110,100,bg,bg)
    pass

#main
F(0,22,319,221,bg)
F(0,0,319,20,BLACK)
F(0,210,322,50,(R(0,255),0,0))
while game:
  etimer+=0.1
  draw_string("Level:"+str(world),100,2,"yellow",BLACK)
  draw_string(":["+str(plife),16,2,WHITE,BLACK)
  draw_string(":[",240,2,GREEN,BLACK)
  F(259,6,round(ee/4),10,RED)
  F(2,3,pw-2,ph-2,pc)
  F(4,5,5,5,CYAN)
  F(226,3,ew-3,eh-4,ec)
  F(227,6,5,4,WHITE)

  if etimer>R(6,10):
    etimer=0
    edir=R(0,4)
    echase=choice([False,True])
#enemy chases player
  if echase:
    if ex<px:
      for i in range(3):
        edir=2
    if ex>px:
      edir=1
    if ey<py:
      edir=4
    if ey>py:
      edir=3
        
  if dooropen:
    F(doorx,doory,doorw,doorh,(randint(0,255),randint(0,255),randint(0,255)))
    F(doorx,doory+doorh,doorw,2,WHITE)
# 3 platforms
  F(GX,GY,GW,GH,GC)
  F(GX2,GY2,GW2,GH2,GC2)
  F(GX3,GY3,GW3,GH3,GC3)
#draw player & enemy  
  F(px,py,pw,ph,pc)
  F(px+2,py+2,7,5,CYAN)
  F(ex,ey,ew,eh,ec)
  F(ex+2,ey+4,8,4,choice([BLACK,WHITE,DARKGREEN]))
  
  if K(KEY_LEFT):
    px += -vel
    F(px+pw,py,1,ph,bg)
    facingleft=True
    facingright=False

  if K(KEY_RIGHT):
    px += vel
    F(px-1,py,1,ph,bg)
    facingright=True
    facingleft=False
    
  if K(KEY_BACKSPACE) and jumping==False and falling==False:
    jumping=True
    jumpCount=1
  
  if jumping:
    jumpCount+=1
    py-=3
    F(px,py+ph+1,pw,3,bg)
#leave this in jumping scope    
    if jumpCount==maxJump:
      jumping=False
      falling=True
    elif falling:
      if py+ph<=GY and py+ph>=GY:
        falling=False
      else:
        falling=True
#careful with code here!
  if px+pw>=GX and px<=GX+GW and py+ph>=GY: 
    falling=False
  else:
    falling=True
  if py+ph>=GY+GH:
    falling=True
    
  if px+pw>=GX2 and px<=GX2+GW2 and py+ph>=GY2:
    falling=False

  if py+ph>=GY2+GH2:
    falling=True

  if px+pw>=GX3 and px<=GX3+GW3 and py+ph>=GY3:
    falling=False

  if py+ph>=GY3+GH3:
    falling=True

  if falling:
    sleep(0.0001)
    py+=vel
    F(px,py-1,pw,1,bg)

#player teleports to new world
  if px>doorx and px+pw<doorx+doorw and py+ph>=doory and py<=doory+doorh and edefeated==True:
    px=1
    bg=(R(0,80),R(0,20),R(0,80))
    ex=R(180,280)
    ey=R(40,100)
    ew=R(10,24)
    eh=R(8,20)
    ee=0.5*ew*eh
    fballw=R(5,15)
    fballh=fballw
    
    world+=1
    edefeated=False
    ec=(R(0,H),R(0,H),R(0,H))
    dooropen=False
    pe+=0.4
    psc+=R(5,10)
    F(0,22,322,222,bg)
    F(0,210,322,50,(H,0,0))
    py=20
    GX=R(0,10)
    GY=R(20,190)
    GW=R(20,140)
    GH=300
    GC=(R(0,55),R(0,55),R(0,55))
    GX2=R(80,180)
    GY2=R(20,190)
    GW2=R(20,140)
    GH2=300
    GC2=(R(0,25),R(0,25),R(0,25))
    GX3=R(200,280)
    GY3=R(40,190)
    GW3=R(20,140)
    GH3=300
    GC3=(R(0,10),R(0,10),R(0,10))
  if px+pw>321: 
    px=321-pw
    

  if K(KEY_OK):
    pause()
      
  if K(KEY_TOOLBOX) and facingleft:
    F(0,py+4,px,3,(R(0,H),R(0,H),R(0,H)))
    sleep(0.0015)
    F(0,py+4,px,3,bg)

  if K(KEY_TOOLBOX) and facingright:
    F(px+pw,py+4,322-px,3,(randint(0,255),randint(0,255),randint(0,255)))
    sleep(0.0015)
    F(px+pw,py+4,322-px,3,bg)
    
  if facingleft:
    F(px+1,py+7,1,3,WHITE)
    facingright=False

  if facingright:
    F(px+pw-3,py+7,1,3,WHITE)
    facingleft=False
#enemy is hit
  if K(KEY_TOOLBOX) and facingleft and ex<=px and py+4>=ey and py+4<=ey+eh:
    F(ex,ey,ew,eh,RED)
    F(259,6,60,10,BLACK)
    ee-=0.2
    psc+=1
    pe+=0.01

  if K(KEY_TOOLBOX) and facingright and ex>=px and py+4>=ey and py+4<=ey+eh:
    F(ex,ey,ew,eh,RED)
    F(259,6,60,10,BLACK)
    ee-=0.2
    psc+=1
    pe+=0.05

#enemy destroyed  
  if ee<1:
    edefeated=True
    ee=0
    doorx=GX3+4
    
    doory=GY3-doorh
    dooropen=True
    
    for i in range(5):
      for j in range(5):
        i=R(ex,ex+ew)
        j=R(ey,ey+eh)
        F(i,j,R(2,5),R(2,5),choice([BLACK,DARKRED,RED]))
    
    dooropen=True

  if dooropen:
    F(doorx,doory,doorw,doorh,(randint(0,255),randint(0,255),randint(0,255)))
    eattacks=False
    echase=False
    etimer=0
    edir=0
    
  if ex<1:
    ex=1
    edir=choice([2,3,4])     
  if ex+ew>322:
    ex=322-ew
    edir=choice([1,3,4])     
  if ey<=24:
    ey=24
  if ey+eh>222:
    ey=222-eh
    edir=choice([1,2,3])   
#enemy moves      
  if edir==1:
    ex-=1
    F(ex+ew,ey,1,eh,bg)    
  if edir==2:
    ex+=1
    F(ex-1,ey,1,eh,bg)
  if edir==3:
    ey-=1
    F(ex,ey+eh,ew,1,bg)    
  if edir==4:
    ey+=1
    F(ex,ey-1,ew,1,bg)
  if ey+eh>210:
    ey=210-eh
    ee-=0.01
    F(ex,ey,ew,eh,RED)
#boundaries
  if px<0:
    px=0   
  if py<20:
    py=20
  if py+ph>210:
    py=210-ph
    F(px,py,pw,ph,RED)
    pe-=0.2
#player energy loss    
  if px+pw>=ex and px<=ex+ew and py+ph>=ey and py<=ey+eh and edefeated==False:
    F(px,py,pw,ph,RED)
    sleep(0.001)
    pe-=0.001*ew*eh
    ee+=0.1
    edir=R(1,4)
  if plife<2:
    F(px,py,pw,ph,bg)
  if ee<50:
    F(ex,ey,ew,eh,"gray")
  if pe<1:
    pe=9
    plife-=1
  if pe>9:
    pe=1
    plife+=1

  if px+pw>ex and px<ex+ew and py+ph>ey and py+ph<ey+eh:
    pe-=0.1
    ee-=0.2    
    py-=2
  if fbally<22:
    fbally=22
    fballdir=4
  if fbally+fballh>220:
    fbally=220-fballh
    fballdir=3
    F(fballx,fbally,fballw,fballh,bg)
    fballx=R(20,300)

  F(fballx,fbally,fballw,fballh,(R(50,255),0,R(0,55)))

  if fballdir==3:
    fbally-=1
    F(fballx,fbally+fballh,fballw,1,bg)

  if fballdir==4:
    fbally+=1
    F(fballx,fbally-1,fballw,1,bg)

  if px+pw>=fballx and px<=fballx+fballw and py+ph>=fbally and py<=fbally+fballh:
    F(px,py,pw,ph,RED)
    pe-=0.2
  if ex+ew>=fballx and ex<=fballx+fballw and ey+eh>=fbally and ey<=fbally+fballh:
    F(ex,ey,ew,eh,RED)
    ee-=0.2

#game is over when plife < 1
  if plife<1:
    game=False

draw_string("GAME OVER",100,100,WHITE,bg)      
draw_string("SCORE:"+str(psc),100,140,CYAN,bg)

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.