-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathpaintout.nim
161 lines (132 loc) · 2.92 KB
/
paintout.nim
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import nico
import math
# frame counter
var frame = 0
# color
var t = 8.0f
# paddle
var px = 64.0f
var py = 120.0f
var pxv = 0.0f
# ball
var bx = 64.0f
var by = 64.0f
var bxv = 0.0f
var byv = 0.0f
var text: string
var splat = false
var clear = false
const textOptions = [
"HELLO WORLD",
"WELCOME TO NICO",
"MEOW",
"RIBBIT",
"NIM IS FUN",
]
const
sfxHitPaddle = 0
sfxHitWall = 1
sfxDrop = 2
proc gameInit() =
# load all our assets
loadFont(0, "font.png")
setFont(0)
loadMusic(0, "exampleMusic.ogg")
loadSfx(sfxHitPaddle, "hitPaddle.ogg")
loadSfx(sfxHitWall, "hitWall.ogg")
loadSfx(sfxDrop, "drop.ogg")
# start playing music
music(15, 0)
text = "HELLO WORLD"
bxv = 0.5f
byv = 0.75f
proc gameUpdate(dt: Pfloat) =
frame += 1
t += 0.1f
if t >= 16f:
t = 8f
# move paddle
if btn(pcLeft):
pxv -= 0.1f
if btn(pcRight):
pxv += 0.1f
px += pxv
pxv *= 0.95f
let tw = textWidth(text).float32
if px - tw / 2f < 0:
px = tw / 2f
if px > screenWidth.float32 - tw / 2f:
px = (screenWidth.float32 - tw / 2f)
# move ball
bx += bxv
by += byv
byv += 0.1f
# hit the sides
if bx > screenWidth - 4 or bx < 4:
bxv = -bxv
splat = true
sfx(-1, sfxHitWall)
# hit paddle
if by > screenHeight - 8 and bx > px - tw div 2 - 4 and bx < px + tw div 2 + 4:
byv = -byv * 0.8 - 1.0
bxv += pxv
splat = true
sfx(-1, sfxHitPaddle)
# restart
if btnp(pcA) or by > screenHeight:
bx = 64
by = 64
byv = -2.0
bxv = rnd(2.0)-1.0
clear = true
sfx(-1, sfxDrop)
if btnp(pcB):
# switch text
text = rnd(textOptions)
proc gameDraw() =
# clear the at start and when we restart
if frame == 1 or clear or btnp(pcA):
setColor(1)
rectfill(0,0,128,128)
clear = false
# draw the paddle
setColor(t.int)
printc(text, px, py)
# drip effect
for i in 0..<1000:
let x = rnd(screenWidth)
let y = rnd(screenHeight)
let c = pget(x,y)
if c != 1 or rnd(10) == 0:
pset(x,y+1,c)
# draw ball
let d = sqrt(pow(bxv,2.0) + pow(byv,2.0))
let ballSize = if d > 2.0: 2 elif d > 1.0: 3 elif d > 0.5: 4 else: 5
setColor(t.int)
circfill(bx,by, ballSize)
# some random dark splats
if rnd(30) == 0:
setColor(1)
let x = rnd(128)
let y = rnd(128)
circfill(x,y,rnd(5)+3)
for i in 0..rnd(30):
circfill(x + rnd(30)-15, y + rnd(30)-15, rnd(2)+1)
# splats at ball location during impact
if splat:
setColor(t.int)
let x = bx + rnd(10)-5
let y = by + rnd(10)-5
circfill(x,y,rnd(10)+5)
for i in 0..rnd(30):
circfill(x + rnd(30)-15, y + rnd(30)-15, rnd(2)+1)
splat = false
# initialization
nico.init("nico", "test")
# we want a fixed sized screen with perfect square pixels
fixedSize(true)
integerScale(true)
# create the window
nico.createWindow("nico",128,128,4)
# start, say which functions to use for init, update and draw
nico.run(gameInit, gameUpdate, gameDraw)