breakout.py

Created by zecake

Created on September 27, 2023

6.21 KB


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

default_config=[1,0,0,1]
wc,bl=color(90,90,90),color(0,0,0)
wh=color(255,255,255)
puc=color(240,240,0)
themecols=(((0,1,2,3,4,5),(0,1,2,3,4,5)),((0,1,2,3,4,5),(6,6)),((8,8),(7,7)),((5,9),(5,9)),((0,1,2,2,1),(0,1,2,2,1)),((6,6),(6,6)),((0,0),(0,0)))
colors=((255,0,0),(255,125,0),(255,255,0),(0,255,0),(0,0,255),(255,0,255),(0,4,0),
(200,200,200),(150,150,150),(255,100,255))
def game(score,cg):
  fill_rect(0,0,320,240,bl)
  x,y,r=158,150,0
  cx,cy=158,150
  px,py=158,146
  padx=160
  pup=[160,240]
  ppup=pup
  padxn=padx
  fill_rect(0,0,16,222,wc)
  fill_rect(304,0,16,222,wc)
  fill_rect(0,0,320,16,wc)
  defs=score
  swap=1
  botr=0
  pc=[247,247,247]
  predx=160
  lb=[]
  for yb in range(6):
    for xb in range(13):
      cb=themecols[cg[0]][0]
      cb=colors[cb[yb%len(cb)]]
      inc=themecols[cg[0]][1]
      inc=colors[inc[yb%len(inc)]]
      fill_rect(18+xb*22,50+yb*11,20,9,cb)
      fill_rect(19+xb*22,51+yb*11,18,7,inc)
      lb.append([22+xb*22,55+yb*11])
  print(len(lb))
  botb=lb[0]
  d=0.03
  for i in range(3): draw_string(str(3-i),155,150,wh,bl); sleep(1)
  draw_string(" ",155,150,wh,bl)
  while True:
    mt=monotonic()
    if cg[2]==1 :pc=[240*swap,0,240*((swap+1)%2)]
    fill_rect(ceil(padxn)-16,200,32,4,pc)
    if padxn>padx:fill_rect(floor(padx)-16,200,ceil(padxn-padx),4,bl)
    if padxn<padx:fill_rect(floor(padxn)+17,200,ceil(padx-padxn),4,bl)
    padx=padxn
    if (swap or cg[2]!=1):padxn+=(800*d/3)*(int(keydown(KEY_RIGHT))-int(keydown(KEY_LEFT)))
    if not swap and cg[2]==1:padxn+=(800*d/3)*(int(keydown(KEY_SIX))-int(keydown(KEY_FOUR)))
    #if cg[2]==2:padxn=padx
    rr=radians(r)
    predx=x+tan(rr)*(200-y)
    botr=degrees(atan((padxn-botb[0])/(200-botb[1])))/3
    if abs(botr)>13:
      botr=copysign(8,botr)
    botr=round(botr)
    if cg[2]==2 and cos(rr)>0:padxn+=copysign(sqrt(abs(predx-padx+botr)),predx-padx+botr)
    px,py=cx,cy
    x+=sin(rr)*130*d
    c,pup,rdb=collisions(x,y,pup,pc)
    if c>1:
      x+=-(130*d)*sin(rr)
      r=(360-r)%360+randrange(-15,15)/10
      if c>2: 
        score+=c-2
        lb[rdb]=[0,0]
        rdm=0
        botb=lb[rdm]
        while botb[0]==0 and rdm<77:
          rdm+=1
          botb=lb[rdm]
    if c==1:
      y-=(130*d)
      r=(360-r)%360
      swap=(swap+1)%2
    rr=radians(r)
    y+=cos(rr)*(130*d)
    c,pup,rdb=collisions(x,y,pup,pc)
    if c>1:
      y-=(130*d)*cos(rr)
      r=(180-r)%360+randrange(-20,20)/10
      if c>2: 
        score+=c-2
        lb[rdb]=[0,0]
        rdm=0
        botb=lb[rdm]
        while botb[0]==0 and rdm<77:
          rdm+=1
          botb=lb[rdm]
    if c==1:
      y-=(130*d)*cos(rr)
      r=(180-r)%360
      r=180-((x-padx)*3)
      swap=(swap+1)%2
    cx,cy=ceil(x),ceil(y)
    fill_rect(px,py,4,4,bl)
    fill_rect(cx,cy,4,4,wh)
    d=monotonic()-mt
    if cg[2]==2: d*=cg[3]+1
    if cg[1]==1:
      pup[1]+=70*d
      fill_rect(ceil(pup[0]),ceil(pup[1]-70*d),5,5,bl)
      if 204>pup[1]>196 and padx+16>pup[0]>padx-20:
        pup[1]=300
        
      fill_rect(ceil(pup[0]),ceil(pup[1]),5,5,puc)
    #if d!=0:print(1/d)
    padxn=min(max(padxn,33),287)
    draw_string("Score: "+str(score),0,-2,wh,wc)
    if y>220: return False
    if score>=468+defs: return True

def collisions(xc0,yc0,cpup,cccp):
  xc0,yc0=ceil(xc0),ceil(yc0)
  hasCollided=False
  db=0
  for t in range(4):
    xc,yc=xc0+(t%2)*4,yc0+(t//2)*4
    cc=collision(xc,yc,cccp)
    if cc>0:
      if cc>2:
        db=int((xc-18)/22)+int((yc-50)/11)*13
        xc=xc-((xc-18)%22)
        yc=yc-((yc-50)%11)
        fill_rect(xc,yc,22,10,bl)
        if randrange(2)==0 and cpup[1]>230:
          cpup=[xc+8,115]
      return cc,cpup,db
  return 0,cpup,db
  
def collision(xc,yc,pcc):
  pixel=get_pixel(xc,yc)
  if pixel==wh:
    return 0
  if pixel==color(pcc):
    return 1
  if pixel==wc:
    return 2
  if pixel==bl:
    return 0
  if pixel==puc:
    return -1
  return 3+2*(int(-(yc-50)/11)+5)

def menu(conf):
  logo(conf)
  m=0
  themes=("Filled (Default)","Outlines","Gray","Pink","Reds","Void","Custom Theme")
  bol=("[ ]","[X]")
  modes=("Solo","Multi","Bot")
  while keydown(4):pass
  while True:
    draw_string("Play",30,80,wh,bl)
    draw_string("Theme:",30,105,wh,bl)
    draw_string("Power-Ups: ",30,130,wh,bl)
    draw_string("Mode: ",30,155,wh,bl)
    if conf[2]==2: 
      draw_string("Speed: ",30,180,wh,bl)
      draw_string("x"+str(conf[3]+1),100,180,wh,bl)
    draw_string(str(themes[conf[0]]),100,105,wh,bl)
    draw_string(bol[conf[1]],140,130,wh,bl)
    draw_string(modes[conf[2]],100,155,wh,bl)
    draw_string(">",15,80+m*25,wh,bl)
    if keydown(0) and (m%2==1 or m==4):
      while keydown(0):pass
      conf[m-1]=max(conf[m-1]-1,0)
      logo(conf)
      fill_rect(100,105,200,20,bl)
    if keydown(3) and (m%2==1 or m==4):
      while keydown(3):pass
      if m==1: 
        conf[0]=min(conf[0]+1,6) 
      if m==3:
        conf[2]=min(conf[2]+1,2)
      if m==4:
        conf[3]=min(conf[3]+1,7)
      logo(conf)
      fill_rect(100,105,200,20,bl)
    if keydown(0) or keydown(3) and m==2:
      if m==2:
        conf[1]=(conf[1]+1)%2
        fill_rect(140,130,200,20,bl)
      while keydown(0) or keydown(3):pass
    if keydown(2):
      while keydown(2):pass
      draw_string(" ",15,80+m*25,wh,bl)
      m=min(m+1,3+int(conf[2]>1))
    if keydown(1):
      while keydown(1):pass
      draw_string(" ",15,80+m*25,wh,bl)
      m=max(m-1,0)
    if keydown(4) and m==0:
      if game(0,conf):
        if game(468,conf):
          draw_string("You Win!",125,150,wh,bl)
          sleep(3)
      logo(conf)

def logo(cfg):
  ll=(
  (1,1,1,0,1,1,1,0,1,1,1,0,1,1,1,0,1,0,1,0,1,1,1,0,1,0,1,0,1,1,1),
  (1,0,1,0,1,0,1,0,1,0,0,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0,1,0),
  (1,1,0,0,1,1,0,0,1,1,0,0,1,1,1,0,1,1,0,0,1,0,1,0,1,0,1,0,0,1,0),
  (1,0,1,0,1,0,1,0,1,0,0,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0,1,0),
  (1,1,1,0,1,0,1,0,1,1,1,0,1,0,1,0,1,0,1,0,1,1,1,0,1,1,1,0,0,1,0))
  fill_rect(0,0,320,222,bl)
  for yl,line in enumerate(ll):
    bcl=themecols[cfg[0]][0]
    bcl=colors[bcl[yl%len(bcl)]]
    icl=themecols[cfg[0]][1]
    icl=colors[icl[yl%len(icl)]]
    for xl,sq in enumerate(line):
      if sq==1:
        fill_rect(21+xl*9,20+yl*7,8,6,bcl)
        fill_rect(22+xl*9,21+yl*7,6,4,icl)

menu(default_config)

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.