simple_game_v2.py

Created by wperez274

Created on August 08, 2022

7.69 KB

Game. Fun. Play.

Bye.


from math import *
from random import *
from kandinsky import *
from kandinsky import fill_rect as F
from ion import *
from time import *

sleep(0.4)
bg="black"
F(0,0,322,222,bg)
while not keydown(KEY_OK) and not keydown(KEY_EXE):
  draw_string("SIMPLE GAME",100,10,"cyan",bg)
  draw_string("KEYs [Arrow] = 8 Directions",30,60,"yellow",bg)
  draw_string("KEY [OK] = Shoot",65,80,"white",bg)
  draw_string("KEY [Backspace] = Pause/Resume",10,100,"orange",bg)
  draw_string("(Press [OK] or [EXE] to Start)",15,160,"gray",bg)


laser_fire=True

ammo_x=randint(300,500)
ammo_y=randint(30,200)
ammo_w=ammo_h=randint(20,100)
ammo_c="black"

bg=choice([(randint(0,255),randint(0,255),randint(0,255)),
"white","black","pink","yellow","blue","cyan","gray"])

pe=20
px=7
py=100
pw=12
ph=7
pc="blue"
pscore=0

fuel_x=randint(500,800)
fuel_y=randint(30,170)
fuel_w=fuel_h=9
fuel_c="green"

meteor_x=randint(200,300)
meteor_y=randint(0,20)
meteor_w=meteor_h=randint(7,45)
meteor_c=(randint(20,65),randint(0,35),randint(0,35))

rect_x=randint(250,400)
rect_y=randint(22,200)
rect_w=randint(8,100)
rect_h=randint(7,150)
rect_c=(randint(0,255),randint(0,255),randint(0,255))
F(0,0,322,222,bg)
F(0,0,322,24,"black")
# main loop  
while pe>0:
  
  ammo_x-=choice([0,1,1,2])
  
  fuel_x-=1
  meteor_c=(randint(50,255),0,0)
  rect_x-=choice([2,3,4,4,5])

  meteor_x-=choice([0,1,2,4])
  meteor_y+=choice([0,2])

  draw_string("Score:"+str(pscore),7,3)
  draw_string("ENERGY",130,2,"cyan","black")
  F(200,5,round(pe),9,"red")


  F(0,24,322,3,(randint(0,255),randint(0,255),randint(0,255)))
  F(0,219,322,3,(randint(0,255),randint(0,255),randint(0,255)))
  F(0,0,3,222,"white")
  F(317,0,3,222,"white")
  F(px,py,pw,ph,pc)
  
  F(px,py,1,ph,"cyan")
  F(px+pw-1,py,1,ph,"cyan")
  F(px,py,pw,1,"cyan")
  F(px,py+ph-1,pw,1,"cyan")
  
  F(fuel_x,fuel_y,fuel_w,fuel_h,fuel_c)
  F(fuel_x+fuel_w,fuel_y,1,fuel_h,bg)
  
  F(fuel_x,fuel_y,2,fuel_h,"white")
  F(fuel_x+fuel_w-2,fuel_y,2,fuel_h,"white")
  F(fuel_x,fuel_y,fuel_w,2,"white")
  F(fuel_x,fuel_y+fuel_h-2,fuel_w,2,"white")
  
  F(rect_x,rect_y,rect_w,rect_h,rect_c)
  F(rect_x+rect_w,rect_y,5,rect_h,bg)

  F(rect_x,rect_y,3,rect_h,"white")
  F(rect_x+rect_w-3,rect_y,3,rect_h,"white")
  F(rect_x,rect_y,rect_w,3,"white")
  F(rect_x,rect_y+rect_h-3,rect_w,3,"white")
  
  F(meteor_x,meteor_y,meteor_w,meteor_h,meteor_c)
  F(meteor_x+meteor_w,meteor_y,4,meteor_h,bg)
  F(meteor_x,meteor_y-2,meteor_w,2,bg)
  
  F(meteor_x,meteor_y,2,meteor_h,"blue")
  F(meteor_x+meteor_w-2,meteor_y,2,meteor_h,"blue")
  F(meteor_x,meteor_y,meteor_w,2,"blue")
  F(meteor_x,meteor_y+meteor_h-2,meteor_w,2,"blue")
  
  #ammo box  
  F(ammo_x,ammo_y,ammo_w,ammo_h,ammo_c)
  F(ammo_x+ammo_w,ammo_y,2,ammo_h,bg)

#outline just for looks  
  F(ammo_x,ammo_y,1,ammo_h,"blue")  
  F(ammo_x+ammo_w-1,ammo_y,1,ammo_h,"blue")  
  F(ammo_x,ammo_y,ammo_w,1,"blue")  
  F(ammo_x,ammo_y+ammo_h-1,ammo_w,1,"blue")
  
  
  F(200,3,105,2,"white")
  F(200,15,105,2,"white")
  F(199,3,2,14,"white")
  F(303,3,2,14,"white")
  
  if rect_x+rect_w<0:
    rect_x=randint(250,400)
    rect_y=randint(-10,170)
    rect_w=randint(8,100)
    rect_h=randint(10,180)
    rect_c=(randint(0,255),randint(0,255),randint(0,255))
    pscore+=randint(5,20)
    pe+=0.3
    bg=choice([(randint(0,255),randint(0,255),randint(0,255)),
    "white","black","pink","yellow","blue","cyan","gray"])

    F(3,26,317,219,bg)

  if meteor_x+meteor_w<0-randint(5,20):
    meteor_x=randint(500,800)
    meteor_y=randint(-50,50)
    meteor_w=meteor_h=randint(9,70)
    
  if meteor_y+meteor_h>222+randint(20,50):
    meteor_x=randint(200,800)
    meteor_y=randint(-50,50)
    meteor_w=meteor_h=randint(9,70)

  if keydown(KEY_OK):
    if laser_fire:
      F(px+pw,py+1,322-px,3,(randint(0,255),randint(0,255),randint(0,255)))
      sleep(0.001)
      F(px+pw,py+1,322-px,3,bg)


#player shoots meteor
  if keydown(KEY_OK) and meteor_y+meteor_h>=py+1 and meteor_y<=py+4:
    F(meteor_x,meteor_y,meteor_w,meteor_h,"black")
    sleep(0.04)
    F(meteor_x,meteor_y,meteor_w,meteor_h,bg)
    meteor_x=randint(300,900)
    meteor_y=randint(-200,-50)
    meteor_w=meteor_h=randint(9,65)
    pe+=2

#limit energy
  if pe>100:pe=100


#pause/resume game    
  if keydown(KEY_BACKSPACE):
    draw_string("(PAUSED)",110,50,(randint(0,255),randint(0,255),randint(0,255)),bg)
    while keydown(KEY_BACKSPACE):
      pass
    while not keydown(KEY_BACKSPACE):
      pass
    while keydown(KEY_BACKSPACE):
      draw_string("        ",110,50,bg,bg)    
#player flies
  if keydown(KEY_LEFT):
    px-=3
    F(px+pw,py,3,ph,bg)
  if keydown(KEY_RIGHT):
    px+=3
    F(px-3,py,3,ph,bg)
  if keydown(KEY_UP):
    py-=3
    F(px,py+ph,pw,3,bg)
  if keydown(KEY_DOWN):
    py+=3
    F(px,py-3,pw,3,bg)
#"The Spaceship crashing into 
# ceiling or ground...not good."
# -Elon Musk, 2022

  if py<26:
    py=26
    F(px,py,pw,ph,"red")
    F(0,0,322,22,"black")
    pe-=0.2
  if py+ph>219:
    py=219-ph
    F(px,py,pw,ph,"red")
    F(0,0,322,22,"black")
    pe-=0.2
  if px<3:px=3
  if px+pw>318:
    px=318-pw
  if meteor_y>=py:
    meteor_y-=1

# collision, not good
  if px+pw>=rect_x and px<=rect_x+rect_w and py+ph>=rect_y and py<=rect_y+rect_h:
    pe-=0.3
    F(px,py,pw,ph,"red")
    F(rect_x,rect_y,rect_w,rect_h,pc)
    F(0,0,322,22,"black")
  
  if px+pw>=meteor_x and px<=meteor_x+meteor_w and py+ph>=meteor_y and py<=meteor_y+meteor_h:
    pe-=0.7
    F(px,py,pw,ph,"red")
    F(0,0,322,22,"black")
    
    F(0,25,322,222,"red")
    sleep(0.05)
    F(0,0,322,222,bg)
    
    
  if fuel_x+fuel_w<0-randint(2,10):
    fuel_x=randint(600,1200)
    fuel_y=randint(30,200)

  if ammo_x+ammo_w<0-randint(10,20):
    ammo_x=randint(500,1000)
    ammo_y=randint(20,200)
    ammo_w=randint(20,100)
    ammo_y=randint(20,200)
    
    
    
  
  if px+pw>=ammo_x and px<=ammo_x+ammo_w and py+ph>=ammo_y and py<=ammo_y+ammo_h:
    F(0,0,322,22,"black")
  
    F(px,py,pw,ph,"red")
    F(px-2,py,2,ph,"blue")
    F(px+pw,py,2,ph,"blue")
    F(px,py-2,pw,2,"blue")
    F(px,py+ph,pw,2,"blue")
    sleep(0.02)
    F(px-2,py,2,ph,bg)
    F(px+pw,py,2,ph,bg)
    F(px,py-2,pw,2,bg)
    F(px,py+ph,pw,2,bg)
    pe-=2    
          



#player grabs random fuel capsule
  if px+pw>=fuel_x and px<=fuel_x+fuel_w and py+ph>=fuel_y and py<=fuel_y+fuel_h:
    F(px,py,pw,ph,"cyan")
    
    F(px-2,py,2,ph,"pink")
    F(px+pw,py,2,ph,"pink")
    F(px,py-2,pw,2,"pink")
    F(px,py+ph,pw,2,"pink")
    sleep(0.3)
    F(px-2,py,2,ph,bg)
    F(px+pw,py,2,ph,bg)
    F(px,py-2,pw,2,bg)
    F(px,py+ph,pw,2,bg)
    
    pe+=5
    F(fuel_x,fuel_y,fuel_w,fuel_h,bg)
    fuel_x=randint(500,1000)
    fuel_y=randint(30,200)
  
  if pe<10:
    F(px,py,pw,ph,choice(["red","white",bg]))
    F(px,py+ph,2,2,"red")
    F(px,py-1,pw,1,bg)
    py+=choice([0,0,1])
    

# simple Pause/Resume code
  if keydown(KEY_EXE):
    while not keydown(KEY_ANS):
      draw_string("(PRESS [ANS] KEY TO RESUME)",15,195)  
    
    draw_string("(PRESS [ANS] KEY TO RESUME)",15,195,bg,bg)  
    
        
  
  for i in range(rect_y,rect_y+rect_h,randint(4,9)):
    i+=randint(1,3)
    F(rect_x+rect_w+randint(1,3),i,randint(1,4),randint(1,2),(randint(0,255),randint(0,255),randint(0,255)))

  for i in range(rect_y,rect_y+rect_h,randint(4,9)):
    i+=randint(1,3)
    F(rect_x+rect_w,i,randint(1,322),randint(1,2),(randint(0,255),randint(0,255),randint(0,255)))

#game over    
sx=450
sleep(1.2)
F(0,0,322,222,(0,0,0))
draw_string("Written by: Wilson",11,7,"white","black")
while pe<1 and not keydown(KEY_OK):
  
  F(0,0,2,222,"orange")
  F(318,0,2,222,"orange")
  
  F(0,0,322,2,"orange")
  F(0,219,322,2,"orange")
  
  
  sleep(0.05)
  sx-=8
  if sx<170:
    sx=170
  draw_string("GAME OVER",100,80,"orange","black")
  draw_string("SCORE:",100,120,"white","black")
  draw_string(str(pscore)+" ",sx,120,"cyan","black")
  F(0,180,322,40,"orange")

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.