🎉 Initial Commit of Basic things like movement of player

This commit is contained in:
LittleSheep 2024-07-18 12:23:16 +08:00
commit 4b7358a1d0
8 changed files with 152 additions and 0 deletions

8
.idea/.gitignore generated vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

9
.idea/CodingLand.iml generated Normal file
View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

8
.idea/modules.xml generated Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/CodingLand.iml" filepath="$PROJECT_DIR$/.idea/CodingLand.iml" />
</modules>
</component>
</project>

5
go.mod Normal file
View File

@ -0,0 +1,5 @@
module git.solsynth.dev/highland/codingland
go 1.22.5
require github.com/veandco/go-sdl2 v0.4.40

2
go.sum Normal file
View File

@ -0,0 +1,2 @@
github.com/veandco/go-sdl2 v0.4.40 h1:fZv6wC3zz1Xt167P09gazawnpa0KY5LM7JAvKpX9d/U=
github.com/veandco/go-sdl2 v0.4.40/go.mod h1:OROqMhHD43nT4/i9crJukyVecjPNYYuCofep6SNiAjY=

View File

@ -0,0 +1,5 @@
package common
type Vector2D struct {
X, Y float64
}

View File

@ -0,0 +1,38 @@
package entities
import (
"git.solsynth.dev/highland/codingland/pkg/internal/common"
"github.com/veandco/go-sdl2/sdl"
)
const (
Acceleration = 0.5
Friction = 0.9
)
type Player struct {
Position common.Vector2D
Size common.Vector2D
Velocity common.Vector2D
}
func (p *Player) Move(deltaX, deltaY float64) {
p.Velocity.X += deltaX * Acceleration
p.Velocity.Y += deltaY * Acceleration
p.Position.X += p.Velocity.X
p.Position.Y += p.Velocity.Y
p.Velocity.X *= Friction
p.Velocity.Y *= Friction
}
func (p *Player) Draw(pen *sdl.Renderer) {
pen.SetDrawColor(255, 255, 255, 255)
pen.FillRect(&sdl.Rect{
X: int32(p.Position.X),
Y: int32(p.Position.Y),
W: int32(p.Size.X),
H: int32(p.Size.Y),
})
}

77
pkg/main.go Normal file
View File

@ -0,0 +1,77 @@
package main
import (
"git.solsynth.dev/highland/codingland/pkg/internal/common"
"git.solsynth.dev/highland/codingland/pkg/internal/entities"
"log"
"runtime"
"github.com/veandco/go-sdl2/sdl"
)
const (
windowWidth = 640
windowHeight = 480
playerSize = 50
)
func init() {
runtime.LockOSThread()
}
func main() {
if err := sdl.Init(sdl.INIT_EVERYTHING); err != nil {
log.Fatalf("Failed to initialize SDL: %s", err)
}
defer sdl.Quit()
window, err := sdl.CreateWindow("CodingLand", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, windowWidth, windowHeight, sdl.WINDOW_SHOWN)
if err != nil {
log.Fatalf("Failed to create window: %s", err)
}
defer window.Destroy()
renderer, err := sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED|sdl.RENDERER_PRESENTVSYNC)
if err != nil {
log.Fatalf("Failed to create renderer: %s", err)
}
defer renderer.Destroy()
player := &entities.Player{
Position: common.Vector2D{X: windowWidth / 2, Y: windowHeight / 2},
Size: common.Vector2D{X: playerSize, Y: playerSize},
}
running := true
for running {
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
switch event.(type) {
case *sdl.QuitEvent:
running = false
}
}
keys := sdl.GetKeyboardState()
if keys[sdl.SCANCODE_UP] == 1 || keys[sdl.SCANCODE_W] == 1 {
player.Move(0, -1)
}
if keys[sdl.SCANCODE_DOWN] == 1 || keys[sdl.SCANCODE_S] == 1 {
player.Move(0, 1)
}
if keys[sdl.SCANCODE_LEFT] == 1 || keys[sdl.SCANCODE_A] == 1 {
player.Move(-1, 0)
}
if keys[sdl.SCANCODE_RIGHT] == 1 || keys[sdl.SCANCODE_D] == 1 {
player.Move(1, 0)
}
player.Move(0, 0)
renderer.SetDrawColor(0, 0, 0, 255)
renderer.Clear()
player.Draw(renderer)
renderer.Present()
}
}