This script is heavily inspired by the android game circle pong, by manguildev (source) and was made as part of a challenge to code an entire game WITHOUT USING A SINGLE “if” statement (turns out boolean arithmetic is pretty fun actually!).
REQUIRES util.py to be downloaded onto your calculator.
Use the ALPHA and TOOLBOX keys to rotate the pad clockwise or counterclockwise (respectively) to protect the ball from touching the red zone.
I’ll add a score counter soon…
from math import * from util import * def cartesian(r,th):return[r*cos(th),r*sin(th)] def line(x,y,u,v,t): yield from((x+u*i,y+v*i)for i in range(ceil(t))) x+=u*t;y+=v*t return x,y,atan2(y,x) class Pad: def __init__(s,r,spd,phi): s.pos=[cartesian(r,-phi/2),cartesian(r,phi/2)] s.dx,s.dy=cartesian(1.,spd) def __call__(s,d): dx,dy=s.dx**(d&1),s.dy*d p=s.pos (sx,sy),(ex,ey)=s,e=p[d==1],p[d!=1] # MWAHAHAAA. Begone, if statements! s[0]=sx*dx-sy*dy s[1]=sy*dx+sx*dy e[0]=ex*dx-ey*dy e[1]=ey*dx+ex*dy return sx,sy,ex,ey def __contains__(s,w,offset=pi/45): (sx,sy),(ex,ey)=s.pos th,phi=wrap(atan2(sy,sx)-offset,-pi,pi),wrap(atan2(ey,ex),-pi,pi) # Values returned by arc trig functions are always in [-pi, pi] return(w>=th)+(w<=phi)+(phi<th)==2 # Ahh yeess.. def ball_pos(pad,t,spd): x=y=w=v=0;u=spd;t/=spd while w in pad: x,y,w=yield from line(x,y,u,v,t) u,v=cartesian(-spd,w+uniform(-pi/5,pi/5)) t=-2*(u*x+v*y)/(u*u+v*v) @game_func def run(col,bg,spd): fill_screen(bg) f,d,t,k=fill_rect,disc,int,keydown dzc=(255,0,0) bx,by=160,111 pad=Pad(106,pi/120,pi/3) def rotate_pad(d): sx,sy,ex,ey=pad(d) f(157+t(sx),108-t(sy),7,7,col) f(157+t(ex),108-t(ey),7,7,dzc) for _ in range(240):rotate_pad(1) for i in range(3,0,-1): draw_string(str(i),155,102,col,bg) sleep(.5) for x,y in ball_pos(pad,94,spd): d(bx,by,8,bg) bx,by=160+t(x),111-t(y) d(bx,by,8,col) rotate_pad(k(13)-k(16)) # Wait, there's more? ARGS={"col":CYAN,"bg":DARK_PURPLE,"spd":.75} run(**ARGS)