sds2.py

Created by elnix91

Created on June 01, 2024

4.88 KB

pas final version


from kandinsky import fill_rect as f , draw_string as d,set_pixel as s
from ion import keydown as k
from time import monotonic as m

# CONSTANTS
COL = {
"fond"      : (200,)*3,
"player0"   : (255,0,0),
"player1"   : (0,0,255),
"sol"       : (0,)*3,
"power"     : (255,)*3,
"power_max" : (255,255,0),
"hit"       : (0,)*3}
GROUNDS = [[0,90],[60,140],[120,90],[160,90],[140,185],[140,175],[220,140],[280,90]]
XKEYS   = [0,3,44,46]
YKEYS   = [1,39]
ATKEYS  = [12,18,52,51]

def draw_ground():
  for i in range(len(GROUNDS)):
    f(GROUNDS[i][0],GROUNDS[i][1],40,10,COL["sol"])

def draw_player(aff):
  for i in range(2):
    if aff == 0:
      if i == 0:
        col = COL["player0"]
      else:
        col = COL["player1"]
    elif aff == 1:
      col = COL["fond"]
    else:
      col = COL["hit"]
    f(PLAYER[i][0],PLAYER[i][1],10,20,col)
    if PLAYER[i][2] == 0:
      f(PLAYER[i][0],PLAYER[i][1]+5,-5,5,col)
    else:
      f(PLAYER[i][0]+10,PLAYER[i][1]+5,5,5,col)

def draw_heart(x,y,col):
  for i in range(5):
    for j in range(5):
      if "0101011111111110111000100"[i*5+j] == "1":
        f(x+j*2,y+i*2,2,2,col)

def draw_health():
  col = COL["player0"]
  for i in range(10):
    if i>=HEALTH[0]:col=COL["sol"]
    draw_heart(2+15*i,210,col)
  col = COL["player1"]
  for i in range(10):
    if i>=HEALTH[1]:col=COL["sol"]
    draw_heart(308-15*i,210,col)

def draw_power():
  f(2,197,316,5,COL["sol"])
  for i in range(2):
    if POWER[i]==99:col=COL["power_max"]
    else:col=COL["power"]
    f(2+326*i,197,POWER[i]-2*i*POWER[i],5,col)

def draw_atk(player,aff):
    if aff==0:
      if player==0:
        col=COL["player0"]
      else:
        col=COL["player1"]
    else:col=COL["fond"]
    if PLAYER[player][2]==0:
      f(PLAYER[player][0],PLAYER[player][1]+5,-15,5,col)
    else:
      f(PLAYER[player][0]+10,PLAYER[player][1]+5,15,5,col)
 
def draw_bullet(aff):
  if aff==0:
    col=COL["player0"]
  else:
    col=COL["fond"]
  for i in range(2):
    for j in range(len(BULLET[i])):
      f(BULLET[i][j][0],BULLET[i][j][1],5,5,col)
    if aff==0:
      col=COL["player1"]

def iscolision(p,mod,g=0,a=1):
  for i in range(len(GROUNDS)):
    if(mod==0)&(PLAYER[p][1]>(GROUNDS[i][1]-20))&(PLAYER[p][1]<(GROUNDS[i][1]+10))&(PLAYER[p][0]>(GROUNDS[i][0]-10))&(PLAYER[p][0]<(GROUNDS[i][0]+45)):return 0
    elif(mod==3)&(PLAYER[p][1]>(GROUNDS[i][1]-20))&(PLAYER[p][1]<(GROUNDS[i][1]+10))&(PLAYER[p][0]>(GROUNDS[i][0]-15))&(PLAYER[p][0]<(GROUNDS[i][0]+40)):return 0
    elif (mod==1)&(PLAYER[p][0]>(GROUNDS[i][0]-10))&(PLAYER[p][0]<(GROUNDS[i][0]+40))&(PLAYER[p][1]>(GROUNDS[i][1]-20))&(PLAYER[p][1]<(GROUNDS[i][1]+10+g)):
      if a:return 0
      else:return PLAYER[p][1]-GROUNDS[i][1]-10
    elif(PLAYER[p][0]>(GROUNDS[i][0]-10))&(PLAYER[p][0]<(GROUNDS[i][0]+40))&(PLAYER[p][1]>(GROUNDS[i][1]-20+g))&(PLAYER[p][1]<(GROUNDS[i][1]+10)):
      if a:return 0
      else:return PLAYER[p][1]-GROUNDS[i][1]+20
  return 1
# NEW GAME LOOP
while 1:
# Initial map drawing
  f(0,0,320,195,COL["fond"])
# Initialize main variables
  PLAYER   = [[50,175,0],[260,175 ,1]]
  HEALTH   = [10,10]
  POWER    = [0,0]
  BULLET   = [[],[]]
  ATK      = [0,0]
  G        = [0,0]
  jump     = [0,0]
  c_health = 0
  c_power  = 0
  atk      = [0,0]
  c_bullet = 0
# GAME LOOP
  while 1:
#   First part of frame system
    T=m()
#   Erasing sprites system
    draw_player(1)
    draw_atk(0,1)
    draw_atk(1,1)
    if c_bullet:draw_bullet(1)
#   Reset tests things moved
    c_atk    = 0
    c_bullet = 0
    c_health = 0

#   X axis move system
    for i in range(2):
      if k(XKEYS[i+1*i])&(PLAYER[i][0]>1)&iscolision(i,0):
        PLAYER[i][0]-=5
        PLAYER[i][2]=0
      elif k(XKEYS[i+1+i*1])&(PLAYER[i][0]<310)&iscolision(i,3):
        PLAYER[i][0]+=5
        PLAYER[i][2]=1

#   Y axis move system
    for i in range(2):
      if k(YKEYS[i])&(jump[i]==0):
        G[i]=15
        jump[i]=1
      if G[i]>0:
        if iscolision(i,1,G[i]):
          PLAYER[i][1]-=G[i]
          G[i]-=2
        else:
          PLAYER[i][1]-=iscolision(i,1,G[i],0)
          G[i]=-G[i]
      elif G[i]<0:
        if iscolision(i,2,G[i]):
          PLAYER[i][1]-=G[i]
          G[i]-=2
        else:
          PLAYER[i][1]-=iscolision(i,2,G[i],0)
          G[i]=0
          jump[i]=0
      else:
        if iscolision(i,2,-2):
          G[i]-=2
          jump[i]=1
      if PLAYER[i][1]>175:
        G[i]=0
        PLAYER[i][1]=175
        jump[i]=0

#   Attack system
    for i in range(2):
      if k(ATKEYS[0+i*1])&(ATK[i]==0)&atk[i]:
        ATK[i]=1
        atk[i]=0
      if (ATK[i]>0)&(ATK[i]<=5):
        ATK[i]+=1
      if ATK[i]==5:
        ATK[i]=0
      if not k(ATKEYS[0+i*1]):atk[i]=1
#   Drawing sprites system
    d(str(ATK[0])+"  "+str(ATK[1]),0,0)
    draw_ground()
    if c_health:draw_health()
    draw_player(0)
    draw_power()
    if ATK[0]>0:draw_atk(0,0)
    if ATK[1]>0:draw_atk(1,0)
    if c_bullet:draw_bullet(0)

#   Second part of frame system
    while m()-T<0.07:1

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.