Skip to content

Instantly share code, notes, and snippets.

@SOF3
Last active January 23, 2021 10:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SOF3/6cecff0d72cda4633f8165840b617702 to your computer and use it in GitHub Desktop.
Save SOF3/6cecff0d72cda4633f8165840b617702 to your computer and use it in GitHub Desktop.
# Parameters
set CURSOR_RATIO 2.5
set SCALE 80
set PAD_SIZE 0.1
set X_LEFT 8
set X_RIGHT 168
set BALL_RADIUS 0.05
set INIT_VELOCITY 0.08
set VELOCITY_INCREASE 0.02
set DIFFRACTION_RATIO 0.4
set TRIANGLE_PADDING 10
set TRIANGLE_Y 170
set TRIANGLE_RADIUS 5
set TRIANGLE_STEP 12
set MAX_TRIANGLES 15
# Memory layout
# read playing bank1 0
read ballX bank1 1
read ballY bank1 2
read ballDX bank1 3
read ballDY bank1 4
read leftDeaths bank1 5
read rightDeaths bank1 6
sensor reset switch2 @enabled
op sub reset 1 reset
op mul reset reset 3
op add @counter @counter reset
# {{
write 0 bank1 5
write 0 bank1 6
end
# }}
# Base line
draw clear 255 255 255 0 0 0
draw stroke 2 255 255 0 0 0
draw color 0 0 0 255
draw line 88 0 88 176 0 0
# Fetch reference data
sensor originX display1 @x
sensor originY display1 @y
# Control left pad
sensor leftY wave2 @shootY
op sub leftY leftY originY
op div leftY leftY CURSOR_RATIO
op min leftY leftY 1
op max leftY leftY -1
op sub leftStart leftY PAD_SIZE
op add leftEnd leftY PAD_SIZE
op mul leftStartRender leftStart SCALE
op add leftStartRender leftStartRender 88
op mul leftEndRender leftEnd SCALE
op add leftEndRender leftEndRender 88
draw color 255 0 0 255
draw line X_LEFT leftStartRender X_LEFT leftEndRender 0 0
# Control right pad
sensor rightY wave1 @shootY
op sub rightY rightY originY
op div rightY rightY CURSOR_RATIO
op min rightY rightY 1
op max rightY rightY -1
op sub rightStart rightY PAD_SIZE
op add rightEnd rightY PAD_SIZE
op mul rightStartRender rightStart SCALE
op add rightStartRender rightStartRender 88
op mul rightEndRender rightEnd SCALE
op add rightEndRender rightEndRender 88
draw color 0 0 255 255
draw line X_RIGHT rightStartRender X_RIGHT rightEndRender 0 0
# Draw scoreboard
set redTriangleIndex 0
# {{
op greaterThanEq redCond1 redTriangleIndex MAX_TRIANGLES
op greaterThanEq redCond2 redTriangleIndex rightDeaths
op or redCond redCond1 redCond2
op mul redCond redCond 6
op add @counter @counter redCond
op mul triangleX TRIANGLE_RADIUS redTriangleIndex
op add triangleX triangleX TRIANGLE_PADDING
draw color 255 0 0 255
draw poly triangleX TRIANGLE_Y 3 TRIANGLE_RADIUS 0 0
op add redTriangleIndex redTriangleIndex 1
op sub @counter @counter 11
# }}
set blueTriangleIndex 0
# {{
op greaterThanEq blueCond1 blueTriangleIndex MAX_TRIANGLES
op greaterThanEq blueCond2 blueTriangleIndex leftDeaths
op or blueCond blueCond1 blueCond2
op mul blueCond blueCond 7
op add @counter @counter blueCond
op mul triangleX TRIANGLE_RADIUS blueTriangleIndex
op add triangleX triangleX TRIANGLE_PADDING
op sub triangleX 175 triangleX
draw color 0 0 255 255
draw poly triangleX TRIANGLE_Y 3 TRIANGLE_RADIUS 180 0
op add blueTriangleIndex blueTriangleIndex 1
op sub @counter @counter 12
# }}
sensor playing switch1 @enabled
# if not playing, flush and end
op sub playing_flag 1 playing
op mul playing_flag playing_flag 17
op add @counter @counter playing_flag
# {{
write 0 bank1 1
write 0 bank1 2
write 0 bank1 3
write 0 bank1 4
drawflush display1
print "Designed by [green]SOFe\n"
print "[yellow]Control the waves to "
print "move the pads, then start "
print "the ping-pong game by "
print "clicking on the lower switch.\n"
print "Use the upper switch to reset stats.\n"
print "[red]Red wins: "
print rightDeaths
print "\n[blue]Blue wins: "
print leftDeaths
printflush message1
end
# }}
# Simulate the ball
op mul ballDX2 ballDX ballDX
op mul ballDY2 ballDY ballDY
op add ballVel2 ballDX2 ballDY2
# if ball is not moving...
op greaterThan isMoving ballVel2 0.0001
op mul isMoving isMoving 7
op add @counter @counter isMoving
# {{
op rand startingDirection 2 0
op floor startingDirection startingDirection 0
op mul startingDirection startingDirection 2
op sub startingDirection startingDirection 1
op mul ballDX startingDirection INIT_VELOCITY
set ballX 0
set ballY 0
# }}
# Simulate motion
op add ballX ballX ballDX
op add ballY ballY ballDY
op greaterThan bottomTouch ballY -1
op add @counter @counter bottomTouch
# {{
op mul ballDY ballDY -1
# }}
op lessThan topTouch ballY 1
op add @counter @counter topTouch
# {{
op mul ballDY ballDY -1
# }}
op greaterThan leftTouch ballX -1
op mul leftTouch leftTouch 19
op add @counter @counter leftTouch
# {{
# if NOT (leftStart < ballY < leftEnd) (death)
op lessThan deathCond1 leftStart ballY
op lessThan deathCond2 ballY leftEnd
op and deathCond deathCond1 deathCond2
op mul deathCondSkip deathCond 5
op add @counter @counter deathCondSkip
# {{
op add leftDeaths leftDeaths 1
set ballDX 0
set ballDY 0
set ballX 0
set ballX 0
# }}
op sub bounceCond 1 deathCond
op mul bounceCond bounceCond 6
op add @counter @counter bounceCond
# {{
op sub ballDX ballDX VELOCITY_INCREASE
op mul ballDX ballDX -1
op sub hitPos ballY leftY
op mul diffraction DIFFRACTION_RATIO hitPos
op add ballDY diffraction
set ballX -0.99999
# }}
# }}
op lessThan rightTouch ballX 1
op mul rightTouch rightTouch 19
op add @counter @counter rightTouch
# {{
# if NOT (rightStart < ballY < rightEnd) (death)
op lessThan deathCond1 rightStart ballY
op lessThan deathCond2 ballY rightEnd
op and deathCond deathCond1 deathCond2
op mul deathCondSkip deathCond 5
op add @counter @counter deathCondSkip
# {{
op add rightDeaths rightDeaths 1
set ballDX 0
set ballDY 0
set ballX 0
set ballY 0
# }}
op sub bounceCond 1 deathCond
op mul bounceCond bounceCond 6
op add @counter @counter bounceCond
# {{
op add ballDX ballDX VELOCITY_INCREASE
op mul ballDX ballDX -1
op sub hitPos ballY rightY
op mul diffraction DIFFRACTION_RATIO hitPos
op add ballDY diffraction
set ballX 0.99999
# }}
# }}
# Store new state
write ballX bank1 1
write ballY bank1 2
write ballDX bank1 3
write ballDY bank1 4
write leftDeaths bank1 5
write rightDeaths bank1 6
# Render ball
op mul ballXRender ballX SCALE
op add ballXRender ballXRender 88
op mul ballYRender ballY SCALE
op add ballYRender ballYRender 88
draw color 0 196 0 255
op mul ballRadiusRender BALL_RADIUS SCALE
draw poly ballXRender ballYRender 20 ballRadiusRender 0 0
drawflush display1
print "Designed by [green]SOFe\n"
print "[yellow]Game in progress...\n"
printflush message1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment