Created
December 9, 2023 06:34
-
-
Save xord/92beb77eab25cd07f48c58a43eadb29d to your computer and use it in GitHub Desktop.
スイカゲーム風サンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubysketch' | |
using RubySketch | |
STEP = 20 | |
SIZE_RANGE = 20..100 | |
def nextSize() | |
SIZE_RANGE.step(STEP).to_a.sample | |
end | |
def createBall(x, y, size) | |
ball = createSprite x, y, shape: Circle.new(0, 0, size) | |
ball.restitution = 0.5 | |
ball.friction = 1 | |
ball.draw do |&draw| | |
draw.call | |
push do | |
fill 0 | |
textSize size / 2 | |
textAlign CENTER, CENTER | |
text size, 0, 0, size, size | |
end | |
end | |
ball.contact do |other| | |
if other.width == size && $balls.include?(other) | |
remove ball, other | |
pos = (ball.pos + other.pos) / 2 | |
createBall(pos.x, pos.y, size + STEP).dynamic = true | |
end | |
end | |
$balls << ball | |
ball | |
end | |
def remove(*balls) | |
balls.each do |ball| | |
removeSprite ball | |
$balls.delete ball | |
end | |
end | |
noStroke | |
gravity 0, 1000 | |
walls = [ | |
createSprite(0, height - 10, width, 10), | |
createSprite(0, 0, 10, height), | |
createSprite(width - 10, 0, 10, height) | |
] | |
walls.each {_1.friction = 1} | |
$balls = [] | |
$nextBall = createBall 0, 0, nextSize | |
draw do | |
$nextBall&.center = [mouseX, 20] | |
background 100 | |
sprite *$balls, *walls | |
end | |
mousePressed do | |
$nextBall&.dynamic = true | |
$nextBall = nil | |
setTimeout 1 do | |
$nextBall = createBall 0, 0, nextSize | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment