gameq2.py

Created by wperez274

Created on January 12, 2022

2.09 KB

simple game. key [Backspace] = Pause/ Resume Warning: The more you move left and right, the more points you get, but the more energy is used or loss. The energy loss is low but it’s there. Reason why I put that is because, in reality, the more you move, the more energy (Carbs & Calories / Heat) your body uses. So move wisely :-) Only move when you have to. Good luck!


from math import *
from random import *
from kandinsky import *
from ion import *
from time import *

detected=False

bg=(0,0,0)
game=True

ex=randint(0,300)
ey=randint(0,20)
ew=randint(50,200)
eh=8
#speed factor used
#in a for loop
es=1
ec="red"


penergy=30
pscore=0
px=110
py=210
pw=15
ph=5
ps=10
pc="cyan"


def move_left():
  global px,py,pw,ph
  for i in range(8):
    px-=1
    fill_rect(px+pw,py,3,ph,bg)

  if px<1:
    px=1
# player moves right
# to go faster, just
# increase int in range()
def move_right():
  global px,py,pw,ph
  for i in range(8):
    px+=1
    fill_rect(px-2,py,2,ph,bg)
  if px+pw>321:
    px=321-pw

  
fill_rect(0,0,322,222,bg)

while penergy>0:

  draw_string("SCORE:"+str(int(pscore)),5,2,"white",bg)
  draw_string("Energy[",140,2,"white",bg)
  fill_rect(210,6,int(penergy),12,"cyan")
  
  # enemy speed
  for i in range(es):
    sleep(0.005)
    ey+=1
  
  fill_rect(ex,ey,ew,eh,ec)
  fill_rect(ex,ey-2,ew,2,bg)
  fill_rect(px,py,pw,ph,pc)

  
  if keydown(KEY_LEFT):
    pscore+=randint(0,1)
    penergy-=0.01
    move_left()
  if keydown(KEY_RIGHT):
    penergy-=0.01
    pscore+=randint(0,1)
    move_right()
  
# enemy missed player    
  if ey>222+eh+randint(10,20):
    ey=randint(0,30)
    ex=randint(0,310)
    ew=randint(50,230)
    penergy+=.5
    es+=0.1
    detected=choice([0,1])
    fill_rect(0,0,322,222,"black")
    pscore+=5

    
  if keydown(KEY_BACKSPACE):
    draw_string("(PAUSED)",110,100,"white",bg)
    while keydown(KEY_BACKSPACE):
      pass
    while not keydown(KEY_BACKSPACE):
      pass
    while keydown(KEY_BACKSPACE):
      draw_string("        ",110,100,bg,bg)
      pass
      
   # player is detected 
  if detected:
    if ex<px:
      for i in range(es):
        sleep(0.0001)
        ex+=1
    if ex>px:
      for i in range(es):
        sleep(0.0001)
        ex-=1
    
        
  if px+pw>=ex and px<=ex+ew and py<=ey+eh:
    penergy-=.25
    fill_rect(px,py,pw,ph,"red")
    
fill_rect(0,0,322,222,"black")
draw_string("GAME OVER",100,100,"green","black")      
draw_string("SCORE:"+str(pscore),100,140,"white","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.