-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
80 lines (68 loc) · 1.81 KB
/
main.go
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
package main
import (
"github.com/veandco/go-sdl2/sdl"
)
func main() {
if err := sdl.Init(sdl.INIT_EVERYTHING); err != nil {
panic(err)
}
defer sdl.Quit()
window, err := sdl.CreateWindow("Bouncing Square", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, 800, 600, sdl.WINDOW_SHOWN)
if err != nil {
panic(err)
}
defer func(window *sdl.Window) {
err := window.Destroy()
if err != nil {
}
}(window)
surface, err := window.GetSurface()
if err != nil {
panic(err)
}
// Initial position, size, and velocity of the square
square := sdl.Rect{X: 300, Y: 200, W: 50, H: 50}
velocityX, velocityY := int32(5), int32(5)
running := true
for running {
// Handle events
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
switch event.(type) {
case *sdl.QuitEvent:
println("Quit")
running = false
}
}
// Update square position
square.X += velocityX
square.Y += velocityY
// Check for collisions with window edges
if square.X <= 0 || square.X+square.W >= 800 {
velocityX = -velocityX // Reverse horizontal direction
}
if square.Y <= 0 || square.Y+square.H >= 600 {
velocityY = -velocityY // Reverse vertical direction
}
// Clear the surface by filling it with black
err = surface.FillRect(nil, 0)
if err != nil {
panic(err)
}
// Draw the square with a color (purple)
color := sdl.Color{R: 255, G: 0, B: 255, A: 255} // purple
pixel := sdl.MapRGBA(surface.Format, color.R, color.G, color.B, color.A)
err = surface.FillRect(&square, pixel)
if err != nil {
panic(err)
}
// Update the window surface to show changes
err = window.UpdateSurface()
if err != nil {
panic(err)
}
// Delay to control frame rate
framerate := (1.0 / 60.0) * 1000.0
println(uint32(framerate))
sdl.Delay(uint32(framerate)) // roughly 60 FPS
}
}