summaryrefslogtreecommitdiff
path: root/football/main.go
blob: a6cd954d4e2034b1c34185776f4a7e80494d7efa (plain)
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
package main

import (
	"citrons.xyz/talk/tui"
	"math/rand"
	"log"
	"strings"
	"time"
)

type ball struct {
	x int
	y int
	vx int
	vy int
	inGoal bool
}

func main() {
	tui.Start()

	s := tui.Size()
	var balls [20]ball
	for i := 0; i < len(balls); i++ {
		balls[i].x = rand.Int() % s.Width - 1
		balls[i].y = rand.Int() % s.Height
		if rand.Int() % 2 == 1 {
			balls[i].vx = 1
		} else {
			balls[i].vx = -1
		}
		if rand.Int() % 2 == 1 {
			balls[i].vy = 1
		} else {
			balls[i].vy = -1
		}
	}

	var sb strings.Builder

	for {
		tui.Clear()
		s := tui.Size()
		for y := 0; y < s.Height; y++ {
			for x := 0; x < s.Width; x++ {
				if (x + 66) % 10 == 1 && (y + 2) % 5 == 1 {
					tui.Write(x, y, "*", tui.Style {Fg: tui.Green})
				}
			}
		}
		tui.Write(2, 1, "normal", tui.Style {Fg: tui.Red})
		tui.Write(2, 2, "bold", tui.Style {Fg: tui.Yellow, Bold: true})
		tui.Write(2, 3, "italic", tui.Style {Fg: tui.Green, Italic: true})
		tui.Write(2, 4, "underline", tui.Style {Fg: tui.Blue, Underline: true})
		tui.Write(2, 5, "strikethrough", tui.Style {Fg: tui.Magenta, Strikethrough: true})
		tui.Write(2, 6, "hooray!", tui.Style {Fg: tui.White, Bold: true, Italic: true, Underline: true, Strikethrough: true})
		gx := s.Width / 2 - 1
		for i := 0; i < len(balls); i++ {
			if balls[i].inGoal {
				continue
			}
			balls[i].x += balls[i].vx
			balls[i].y += balls[i].vy
			if balls[i].x < 0 {
				balls[i].x = 0
				balls[i].vx = -balls[i].vx
			} else if balls[i].x >= s.Width - 1 {
				balls[i].x = s.Width - 2
				balls[i].vx = -balls[i].vx
			}
			if balls[i].y < 0 {
				balls[i].y = 0
				balls[i].vy = -balls[i].vy
			} else if balls[i].y >= s.Height {
				balls[i].y = s.Height - 1
				balls[i].vy = -balls[i].vy
			}
			if balls[i].x >= gx - 1 && balls[i].x <= gx + 1 && balls[i].y == 0 {
				balls[i].inGoal = true
				continue
			}
			tui.Write(balls[i].x, balls[i].y, "⚽", tui.DefaultStyle)
		}
		tui.Write(gx, 0, "🥅", tui.DefaultStyle)

		w := tui.Write(2, 8, sb.String(), tui.DefaultStyle)
		tui.MoveCursor(2 + w, 8)
		err := tui.Present()
		if err != nil {
			log.Fatal(err)
		}

		select {
		case ev := <-tui.Events():
			switch ev := ev.(type) {
			case tui.TextInput:
				sb.WriteRune(rune(ev))
			case error:
				log.Fatal(ev)
			}
		case <-time.After(time.Second / 16):
		}
	}
}