ayin/colorgame.ayin
2025-12-24 09:39:20 +02:00

88 lines
2.6 KiB
Text

let setup = fn() {
return {
.color: {
.r: random_u8(),
.g: random_u8(),
.b: random_u8(),
},
.rect: {
.speed: 100,
.dimensions: {
.x: 50,
.y: 50,
.w: 300 - 50,
.h: 300 - 50,
},
.color: {
.r: random_u8(),
.g: random_u8(),
.b: random_u8(),
.speed: 150,
},
},
}
}
let migrate = fn(state) {
state
}
let update = fn(state, input) {
let delta = get_frame_time()
let dpad_x = if input.gamepad1.dpad.right { 1 } else { 0 } + if input.gamepad1.dpad.left { -1 } else { 0 }
let dpad_y = if input.gamepad1.dpad.down { 1 } else { 0 } + if input.gamepad1.dpad.up { -1 } else { 0 }
# let movement = {
# .x: input.gamepad1.sticks.left.x + dpad_x,
# .y: input.gamepad1.sticks.left.y + dpad_y,
# }
let movement = {
.x: dpad_x,
.y: dpad_y,
}
state.rect.dimensions.x =
state.rect.dimensions.x + (delta * state.rect.speed * movement.x)
state.rect.dimensions.y =
state.rect.dimensions.y + (delta * state.rect.speed * movement.y)
let color = {
.r: state.rect.color.r + (input.gamepad1.sticks.right.x * delta * state.rect.color.speed),
.g: state.rect.color.g + ((0 - input.gamepad1.sticks.right.y) * delta * state.rect.color.speed),
.b: state.rect.color.b + ((0 - input.gamepad1.sticks.left.y) * delta * state.rect.color.speed),
}
state.rect.color.r = if ((0 <= color.r) && (color.r <= 255)) { color.r } else { if color.r < 0 { 0 } else { 255 } }
state.rect.color.g = if ((0 <= color.g) && (color.g <= 255)) { color.g } else { if color.g < 0 { 0 } else { 255 } }
state.rect.color.b = if ((0 <= color.b) && (color.b <= 255)) { color.b } else { if color.b < 0 { 0 } else { 255 } }
return state
}
let draw = fn(state) {
frame_clear(state.color.r, state.color.g, state.color.b)
draw_rectangle(state.rect.dimensions, state.rect.color)
let mut count = 0
let r_diff = state.color.r - state.rect.color.r
let g_diff = state.color.g - state.rect.color.g
let b_diff = state.color.b - state.rect.color.b
let epsilon = 2
if ((0 - epsilon) < r_diff) && (r_diff < epsilon) { count = count + 1 }
if ((0 - epsilon) < g_diff) && (g_diff < epsilon) { count = count + 1 }
if ((0 - epsilon) < b_diff) && (b_diff < epsilon) { count = count + 1 }
if count == 0 {
draw_text("Match the colors", 70, 80, 30, { .r: 255, .g: 255, .b: 255 })
}
if count == 1 {
draw_text("Close", 200, 200, 50, { .r: 255, .g: 255, .b: 255 })
}
if count == 2 {
draw_text("Closer!!", 200, 200, 50, { .r: 255, .g: 255, .b: 255 })
}
if count == 3 {
draw_text("Great Job!", 200, 200, 50, { .r: 255, .g: 255, .b: 255 })
}
}