color setting, drawing

This commit is contained in:
Gnawmon 2024-04-23 13:59:57 +03:00
parent 30a4bafcf3
commit 113de54345
7 changed files with 79 additions and 38 deletions

View File

@ -1,16 +1,16 @@
# ballsoffline
# Balls Offline
a game inspired by balls online
A game inspired by [balls online](https://balls-online-plus.onrender.com/)
## user/account settings
## Setting up Balls Offline
you need to make user/account on account.json, you can look example file [account.def.json](./account.def.json)
To play first create an accounts.json, you can look example file [account.def.json](./account.def.json)
if you're on linux, you can do:
If you're on linux, you can do:
```bash
echo '{"username":"'$USER'"}' > account.json
echo '{"username":"'$USER'","color":"000000"}' > account.json
```
## credits
## Credits
[guessy](./guessy4.ttf) is made by dottych

View File

@ -1,3 +1,4 @@
{
"username": "USERNAME HERE"
"username": "USERNAME HERE",
"color": "000000"
}

82
bf.go
View File

@ -16,6 +16,7 @@ const (
var (
ballpos = rl.Vector2{X: 400, Y: 225}
ballColor = rl.Black
speed = 3
squareSize = 100
squares = []rl.Vector2{{X: 640 - float32(squareSize), Y: 315}, {X: 640, Y: 315}, {X: 640 + float32(squareSize), Y: 315}, {X: 640, Y: 315 - float32(squareSize)}, {X: 640, Y: 315 - float32(squareSize)*2}}
@ -23,8 +24,9 @@ var (
)
func main() {
userdata := loadJson("account.json")
checkUserdataValid(userdata)
userData := loadJson("account.json")
checkUserDataValid(userData)
ballColor = convertHex(userData["color"].(string))
rl.InitWindow(screenWidth, screenHeight, "balls offline")
guessy4 := rl.LoadFont("guessy4.ttf")
@ -35,27 +37,13 @@ func main() {
rl.BeginDrawing()
rl.ClearBackground(rl.NewColor(128, 128, 128, 255))
for index, element := range squares {
if debug {
fmt.Println(index, element.X)
}
rl.DrawRectangleV(element, rl.Vector2{X: float32(squareSize), Y: float32(squareSize)}, rl.LightGray)
}
if rl.IsKeyDown(rl.KeyLeftShift) {
draw()
}
drawSquares()
drawPaint()
drawPosition(ballpos, rl.Vector2{X: 10, Y: screenHeight - 20}, guessy4)
drawBall(ballpos, rl.Black, rl.White)
drawBall(ballpos, ballColor, rl.White)
moveBall()
drawTextShadow(userdata["username"].(string), rl.Vector2{X: ballpos.X - ballRadius - 24, Y: ballpos.Y - ballRadius - 40}, guessy4)
for paint := range paints {
rl.DrawRectangleV(paint.location, ballRadius, rl.Black)
}
drawTextShadow("your name", rl.Vector2{X: ballpos.X - ballRadius - 24, Y: ballpos.Y - ballRadius - 40}, guessy4)
drawTextShadow(userData["username"].(string), rl.Vector2{X: ballpos.X - ballRadius - 24, Y: ballpos.Y - ballRadius - 40}, guessy4)
rl.EndDrawing()
@ -64,11 +52,41 @@ func main() {
rl.CloseAudioDevice()
rl.CloseWindow()
}
func drawPaint() {
if debug {
t := strconv.Itoa(len(paints))
rl.DrawText(t, 10, 10, 40, rl.Black)
}
for index, paintI := range paints {
if paintI.time > 1 {
paints[index] = paint{time: paintI.time - 1, location: paintI.location}
rl.DrawRectangleV(paintI.location, rl.Vector2{X: ballRadius - 2, Y: ballRadius - 2}, rl.NewColor(ballColor.R, ballColor.G, ballColor.R, uint8(paintI.time)))
} else {
removePaint(paints, index)
}
}
}
func drawSquares() {
for index, element := range squares {
if debug {
fmt.Println(index, element.X)
}
rl.DrawRectangleV(element, rl.Vector2{X: float32(squareSize), Y: float32(squareSize)}, rl.LightGray)
}
}
func draw() {
rl.DrawRectangle(int32(ballpos.X), int32(ballpos.Y), ballRadius, ballRadius, rl.Black)
paintedPaint := paint{255, ballpos}
paints = append(paints, paintedPaint)
}
func moveBall() {
if rl.IsKeyDown(rl.KeyLeftShift) {
draw()
}
if rl.IsKeyDown(rl.KeyS) {
speed += 2
}
@ -127,7 +145,7 @@ func checkCollision(newPos rl.Vector2) bool {
fmt.Println(index, square.X, rl.Vector2Distance(newPos, square))
rl.DrawLineV(newPos, square, rl.Purple)
}
if rl.CheckCollisionPointRec(newPos, rl.Rectangle{square.X, square.Y, float32(squareSize), float32(squareSize)}) {
if rl.CheckCollisionPointRec(newPos, rl.Rectangle{X: square.X, Y: square.Y, Width: float32(squareSize), Height: float32(squareSize)}) {
if debug {
rl.DrawText("colliding with obstacle", 10, 10, 40, rl.Black)
}
@ -154,3 +172,23 @@ type paint struct {
time float32
location rl.Vector2
}
func removePaint(s []paint, i int) []paint {
s[i] = s[len(s)-1]
return s[:len(s)-1]
}
func convertHex(hex string) rl.Color {
hex1 := hex[0:2]
hex2 := hex[2:4]
hex3 := hex[4:6]
decimal, err := strconv.ParseInt(hex1, 16, 64)
decimal2, err := strconv.ParseInt(hex2, 16, 64)
decimal3, err := strconv.ParseInt(hex3, 16, 64)
if err != nil {
fmt.Println(err)
}
return rl.NewColor(uint8(decimal), uint8(decimal2), uint8(decimal3), 255)
}

View File

@ -3,18 +3,22 @@ package main
import "fmt"
func check(Error error) {
if Error != nil { panic(Error) }
if Error != nil {
panic(Error)
}
}
func checkValid(something any) bool {
if something == nil { return false }
if something == nil {
return false
}
return true
}
func checkUserdataValid(userdata map[string]interface{}) {
func checkUserDataValid(userdata map[string]interface{}) {
for _, data := range []string{"username"} {
if !checkValid(userdata[data]) {
panic(fmt.Sprint("\"", data,"\" isn't defined in userdata:\n",userdata))
panic(fmt.Sprint("\"", data, "\" isn't defined in userdata:\n", userdata))
}
}
}

4
go.mod
View File

@ -2,9 +2,9 @@ module ballsoffline
go 1.22.2
require github.com/gen2brain/raylib-go/raylib v0.0.0-20240418150228-9548fadb54e6
require (
github.com/ebitengine/purego v0.7.1 // indirect
github.com/gen2brain/raylib-go/raygui v0.0.0-20240418150228-9548fadb54e6 // indirect
github.com/gen2brain/raylib-go/raylib v0.0.0-20240418150228-9548fadb54e6
golang.org/x/sys v0.19.0 // indirect
)

2
go.sum
View File

@ -2,7 +2,5 @@ github.com/ebitengine/purego v0.7.1 h1:6/55d26lG3o9VCZX8lping+bZcmShseiqlh2bnUDi
github.com/ebitengine/purego v0.7.1/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ=
github.com/gen2brain/raylib-go/raylib v0.0.0-20240418150228-9548fadb54e6 h1:mNKFgLZIU0eEHKHjb7Uk9ZuSy65DdgmEf2xxum0Tof4=
github.com/gen2brain/raylib-go/raylib v0.0.0-20240418150228-9548fadb54e6/go.mod h1:P/hDjVwz/9fhR0ww3+umzDpDA7Bf7Tce4xNChHIEFqE=
github.com/gen2brain/raylib-go/raylib v0.0.0-20240421191056-278df68f40bb h1:2CdDr/LfDc9uLOW/+3ffSM5Ia7xZZMgjC6UmP7KTjRw=
github.com/gen2brain/raylib-go/raylib v0.0.0-20240421191056-278df68f40bb/go.mod h1:P/hDjVwz/9fhR0ww3+umzDpDA7Bf7Tce4xNChHIEFqE=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=