-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.swift
58 lines (44 loc) · 1.15 KB
/
App.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//
// File.swift
//
//
// Created by Christophe Bronner on 2021-12-26.
//
import RaylibKit
@main struct BouncingBall: Applet {
let BALL_RADIUS = 20
var position: Vector2
var speed: Vector2
var paused = false
var blink = Timeline()
init() {
Window.create(800, by: 450, title: "Example - Shapes - Bouncing Ball")
Application.target(fps: 30)
position = Window.size / 2
speed = Vector2(5, 4)
}
mutating func update() {
if Keyboard.space.isPressed {
paused.toggle()
}
guard !paused else {
blink.update()
return
}
position += speed
if position.x.toInt >= Window.width - BALL_RADIUS || position.x.toInt <= BALL_RADIUS {
speed.x.negate()
}
if position.y.toInt >= Window.height - BALL_RADIUS || position.y.toInt <= BALL_RADIUS {
speed.y.negate()
}
}
func draw() {
Renderer2D.circle(at: position, radius: BALL_RADIUS.toFloat, color: .maroon)
Renderer2D.text("PRESS SPACE to PAUSE BALL MOVEMENT", at: 10, Window.height - 25, color: .lightGray)
if paused, blink.intervals(seconds: 0.5) {
Renderer2D.text("PAUSED", at: 350, 200, size: 40, color: .gray)
}
Renderer2D.fps(at: 10, 10)
}
}