d22fa6e1896f6a55b93d985c16e6700035541955

Author
TheEdgeOfRage <git@theedgeofrage.com>
Committer
TheEdgeOfRage <git@theedgeofrage.com>
Date

Message

Initial commit

Diff

This diff is truncated to protect this page.

  1diff --git a/cmd/main.go b/cmd/main.go
  2new file mode 100644
  3index 0000000000000000000000000000000000000000..097c8318bb632c49f5931bef35e3931b0e4be92b
  4--- /dev/null
  5+++ b/cmd/main.go
  6@@ -0,0 +1,21 @@
  7+package main
  8+
  9+import (
 10+	"github.com/gen2brain/raylib-go/raylib"
 11+
 12+	"gitea.theedgeofrage.com/theedgeofrage/yeetris/game"
 13+)
 14+
 15+func main() {
 16+	game := game.Game{}
 17+	game.Init()
 18+
 19+	rl.SetTargetFPS(60)
 20+
 21+	for !rl.WindowShouldClose() {
 22+		game.Update()
 23+		game.Draw()
 24+	}
 25+
 26+	rl.CloseWindow()
 27+}
 28diff --git a/elements/block.go b/elements/block.go
 29new file mode 100644
 30index 0000000000000000000000000000000000000000..f0f30cd4f2224bf14eb716a7e600e017b5f4e728
 31--- /dev/null
 32+++ b/elements/block.go
 33@@ -0,0 +1,18 @@
 34+package elements
 35+
 36+import "github.com/gen2brain/raylib-go/raylib"
 37+
 38+type Block struct {
 39+	Position rl.Vector2
 40+	Color    rl.Color
 41+	LongBoi  bool
 42+}
 43+
 44+func (b *Block) Draw(piecePosition rl.Vector2, scale rl.Vector2, offset rl.Vector2) {
 45+	basePos := rl.Vector2Add(b.Position, piecePosition)
 46+	if basePos.Y < 0 {
 47+		return
 48+	}
 49+	pos := rl.Vector2Add(rl.Vector2Multiply(basePos, scale), offset)
 50+	rl.DrawRectangleV(pos, scale, b.Color)
 51+}
 52diff --git a/elements/colors.go b/elements/colors.go
 53new file mode 100644
 54index 0000000000000000000000000000000000000000..6a628023d394176263595232067abf683a88044f
 55--- /dev/null
 56+++ b/elements/colors.go
 57@@ -0,0 +1,17 @@
 58+package elements
 59+
 60+import "github.com/gen2brain/raylib-go/raylib"
 61+
 62+var (
 63+	Red       = rl.GetColor(0xcc241dff)
 64+	Blue      = rl.GetColor(0x458588ff)
 65+	Green     = rl.GetColor(0x98971aff)
 66+	Yellow    = rl.GetColor(0xfabd2fff)
 67+	Purple    = rl.GetColor(0xb16286ff)
 68+	Orange    = rl.GetColor(0xd65d0eff)
 69+	Cyan      = rl.GetColor(0x689d6aff)
 70+	Black     = rl.GetColor(0x282828ff)
 71+	Gray      = rl.GetColor(0x928374ff)
 72+	LightGray = rl.GetColor(0xa89984ff)
 73+	White     = rl.GetColor(0xebdbb2ff)
 74+)
 75diff --git a/elements/pieces.go b/elements/pieces.go
 76new file mode 100644
 77index 0000000000000000000000000000000000000000..9589e46343b449c571de3c6f1e9bd6da85d99f0e
 78--- /dev/null
 79+++ b/elements/pieces.go
 80@@ -0,0 +1,74 @@
 81+package elements
 82+
 83+import (
 84+	"github.com/gen2brain/raylib-go/raylib"
 85+)
 86+
 87+type Piece struct {
 88+	Blocks []*Block
 89+}
 90+
 91+var Pieces = []*Piece{
 92+	{
 93+		Blocks: []*Block{
 94+			{rl.NewVector2(-1, 0), Cyan, false},
 95+			{rl.NewVector2(0, 0), Cyan, false},
 96+			{rl.NewVector2(1, 0), Cyan, true},
 97+			{rl.NewVector2(2, 0), Cyan, false},
 98+		},
 99+	},
100+	{
101+		Blocks: []*Block{
102+			{rl.NewVector2(-1, 0), Blue, false},
103+			{rl.NewVector2(-1, -1), Blue, false},
104+			{rl.NewVector2(0, 0), Blue, false},
105+			{rl.NewVector2(1, 0), Blue, false},
106+		},
107+	},
108+	{
109+		Blocks: []*Block{
110+			{rl.NewVector2(-1, 0), Orange, false},
111+			{rl.NewVector2(0, 0), Orange, false},
112+			{rl.NewVector2(1, 0), Orange, false},
113+			{rl.NewVector2(1, -1), Orange, false},
114+		},
115+	},
116+	{
117+		Blocks: []*Block{
118+			{rl.NewVector2(-1, 0), Purple, false},
119+			{rl.NewVector2(0, 0), Purple, false},
120+			{rl.NewVector2(1, 0), Purple, false},
121+			{rl.NewVector2(0, -1), Purple, false},
122+		},
123+	},
124+	{
125+		Blocks: []*Block{
126+			{rl.NewVector2(-1, -1), Red, false},
127+			{rl.NewVector2(0, -1), Red, false},
128+			{rl.NewVector2(0, 0), Red, false},
129+			{rl.NewVector2(1, 0), Red, false},
130+		},
131+	},
132+	{
133+		Blocks: []*Block{
134+			{rl.NewVector2(-1, 0), Green, false},
135+			{rl.NewVector2(0, 0), Green, false},
136+			{rl.NewVector2(0, -1), Green, false},
137+			{rl.NewVector2(1, -1), Green, false},
138+		},
139+	},
140+	{
141+		Blocks: []*Block{
142+			{rl.NewVector2(0, -1), Yellow, false},
143+			{rl.NewVector2(1, -1), Yellow, false},
144+			{rl.NewVector2(0, 0), Yellow, false},
145+			{rl.NewVector2(1, 0), Yellow, false},
146+		},
147+	},
148+}
149+
150+func (p *Piece) Draw(pos rl.Vector2, scale rl.Vector2, offset rl.Vector2) {
151+	for _, block := range p.Blocks {
152+		block.Draw(pos, scale, offset)
153+	}
154+}
155diff --git a/game/board.go b/game/board.go
156new file mode 100644
157index 0000000000000000000000000000000000000000..6d4768b9870823babc8c18337684942854c5bd06
158--- /dev/null
159+++ b/game/board.go
160@@ -0,0 +1,262 @@
161+package game
162+
163+import (
164+	"math"
165+
166+	"gitea.theedgeofrage.com/theedgeofrage/yeetris/elements"
167+	"github.com/gen2brain/raylib-go/raylib"
168+)
169+
170+type Board interface {
171+	DescendActivePiece() bool
172+	DropActivePiece() bool
173+	MoveActivePiece(direction int)
174+	RotateActivePiece(clockwise bool)
175+	ClearLines()
176+	Draw(offset, scale rl.Vector2)
177+}
178+
179+type board struct {
180+	Board [10][20]*elements.Block
181+
182+	ActivePiece    *elements.Piece
183+	ActivePiecePos rl.Vector2
184+	NextPiece      *elements.Piece
185+}
186+
187+var _ Board = (*board)(nil)
188+
189+func InitBoard() *board {
190+	board := &board{}
191+	board.newActivePiece()
192+
193+	return board
194+}
195+
196+func (b *board) newActivePiece() bool {
197+	b.ActivePiecePos = rl.NewVector2(5, 0)
198+	b.ActivePiece = elements.Pieces[rl.GetRandomValue(0, int32(len(elements.Pieces)-1))]
199+	for _, block := range b.ActivePiece.Blocks {
200+		newX, newY := b.ActivePiecePos.X+block.Position.X, b.ActivePiecePos.Y+block.Position.Y
201+		if newY >= 0 && b.Board[int(newX)][int(newY)] != nil {
202+			return true
203+		}
204+	}
205+	return false
206+}
207+
208+func (b *board) setBlocksFromActivePiece() {
209+	for _, block := range b.ActivePiece.Blocks {
210+		newX, newY := b.ActivePiecePos.X+block.Position.X, b.ActivePiecePos.Y+block.Position.Y
211+		b.Board[int(newX)][int(newY)] = &elements.Block{
212+			Position: rl.NewVector2(newX, newY),
213+			Color:    b.ActivePiece.Blocks[0].Color,
214+		}
215+	}
216+}
217+
218+func (b *board) checkHorizontalCollision(left bool) bool {
219+	for _, activeBlock := range b.ActivePiece.Blocks {
220+		xPos := int(b.ActivePiecePos.X + activeBlock.Position.X)
221+		yPos := int(b.ActivePiecePos.Y + activeBlock.Position.Y)
222+		if left {
223+			if xPos == 0 {
224+				return true
225+			}
226+			if yPos >= 0 && b.Board[xPos-1][yPos] != nil {
227+				return true
228+			}
229+		} else {
230+			if xPos == 9 {
231+				return true
232+			}
233+			if yPos >= 0 && b.Board[xPos+1][yPos] != nil {
234+				return true
235+			}
236+		}
237+	}
238+	return false
239+}
240+
241+func (b *board) descendActivePiece() bool {
242+	b.ActivePiecePos.Y += 1
243+	for _, activeBlock := range b.ActivePiece.Blocks {
244+		if b.ActivePiecePos.Y+activeBlock.Position.Y == 20 {
245+			b.ActivePiecePos.Y -= 1
246+			b.setBlocksFromActivePiece()
247+			return true
248+		}
249+
250+		if b.Board[int(b.ActivePiecePos.X+activeBlock.Position.X)][int(b.ActivePiecePos.Y+activeBlock.Position.Y)] != nil {
251+			b.ActivePiecePos.Y -= 1
252+			b.setBlocksFromActivePiece()
253+			return true
254+		}
255+	}
256+	return false
257+}
258+
259+// DescendActivePiece moves the piece down by one block. If the piece cannot move down, it will be placed on the board.
260diff --git a/game/game.go b/game/game.go
261new file mode 100644
262index 0000000000000000000000000000000000000000..ece776ed607f0b9589b38958ccf9a85b64497784
263--- /dev/null
264+++ b/game/game.go
265@@ -0,0 +1,92 @@
266+package game
267+
268+import (
269+	"gitea.theedgeofrage.com/theedgeofrage/yeetris/elements"
270+	"github.com/gen2brain/raylib-go/raylib"
271+)
272+
273+const DEBUG = false
274+
275+// Game type
276+type Game struct {
277+	FramesCounter int32
278+	TickRate      int32
279+	GameOver      bool
280+	Pause         bool
281+
282+	Scale rl.Vector2
283+
284+	Board Board
285+	UI    *UI
286+}
287+
288+// Init - Initialize game
289+func (g *Game) Init() {
290+	g.Scale = rl.NewVector2(40, 40)
291+
292+	g.FramesCounter = 0
293+	g.TickRate = 30
294+	g.GameOver = false
295+	g.Pause = false
296+	g.Board = InitBoard()
297+	g.UI = InitUI(g.Scale)
298+}
299+
300+func (g *Game) Update() {
301+	defer g.Board.ClearLines()
302+	if g.GameOver {
303+		if rl.IsKeyPressed(rl.KeyEnter) {
304+			g.Init()
305+			g.GameOver = false
306+		}
307+		return
308+	}
309+	if rl.IsKeyPressed(rl.KeyP) {
310+		g.Pause = !g.Pause
311+	}
312+
313+	if g.Pause {
314+		return
315+	}
316+
317+	if rl.IsKeyPressed(rl.KeyI) {
318+		g.Board.MoveActivePiece(1)
319+	}
320+	if rl.IsKeyPressed(rl.KeyH) {
321+		g.Board.MoveActivePiece(-1)
322+	}
323+	if rl.IsKeyPressed(rl.KeyE) {
324+		g.Board.RotateActivePiece(true)
325+	}
326+	if rl.IsKeyPressed(rl.KeyN) {
327+		g.Board.DescendActivePiece()
328+	}
329+	if rl.IsKeyPressed(rl.KeySpace) {
330+		g.Board.DropActivePiece()
331+	}
332+	if g.FramesCounter%g.TickRate == 0 && !DEBUG {
333+		g.Board.DescendActivePiece()
334+	}
335+	g.FramesCounter++
336+}
337+
338+func (g *Game) Draw() {
339+	rl.BeginDrawing()
340+	rl.ClearBackground(elements.Black)
341+
342+	g.UI.Draw(g)
343+
344+	if !g.GameOver {
345+		g.Board.Draw(g.UI.BoardOffset, g.Scale)
346+	} else {
347+		rl.DrawText(
348+			"PRESS [ENTER] TO PLAY AGAIN",
349+			int32(rl.GetScreenWidth())/2-rl.MeasureText("PRESS [ENTER] TO PLAY AGAIN", 20)/2,
350+			int32(rl.GetScreenHeight())/2-50,
351+			20,
352+			elements.LightGray,
353+		)
354+	}
355+
356+	rl.EndDrawing()
357+}
358diff --git a/game/ui.go b/game/ui.go
359new file mode 100644
360index 0000000000000000000000000000000000000000..f8add3ad072872089e7c5ffc49037fe6842c9f23
361--- /dev/null
362+++ b/game/ui.go
363@@ -0,0 +1,56 @@
364+package game
365+
366+import (
367+	// "fmt"
368+
369+	"gitea.theedgeofrage.com/theedgeofrage/yeetris/elements"
370+	"github.com/gen2brain/raylib-go/raylib"
371+)
372+
373+type UI struct {
374+	ScreenWidth   int32
375+	ScreenHeight  int32
376+	BoardWidth    int32
377+	BoardHeight   int32
378+	BoardOffset   rl.Vector2
379+	UIOffsetWidth int32
380+
381+	Score int
382+}
383+
384+func InitUI(scale rl.Vector2) *UI {
385+	boardWidth := 10 * int32(scale.X)
386+	boardHeight := 20 * int32(scale.Y)
387+	screenWidth := boardWidth + 40 + 300
388+	screenHeight := boardHeight + 40
389+	rl.InitWindow(screenWidth, screenHeight, "Yeetris")
390+
391+	return &UI{
392+		ScreenWidth:   screenWidth,
393+		ScreenHeight:  screenHeight,
394+		BoardWidth:    boardWidth,
395+		BoardHeight:   boardHeight,
396+		BoardOffset:   rl.NewVector2(20, 20),
397+		UIOffsetWidth: boardWidth + 40 + 20,
398+	}
399+}
400+
401+func drawRectangleBorder(x, y, width, height, border int32, color rl.Color) {
402+	rl.DrawRectangle(x-border, y-border, width+2*border, height+2*border, color)
403+	rl.DrawRectangle(x, y, width, height, elements.Black)
404+}
405+
406+func (u *UI) Draw(g *Game) {
407+	drawRectangleBorder(int32(u.BoardOffset.X), int32(u.BoardOffset.Y), u.BoardWidth, u.BoardHeight, 20, elements.Gray)
408+	rl.DrawText("Yeetris", u.UIOffsetWidth, 20, 40, rl.White)
409+
410+	if g.Pause {
411+		rl.DrawText(
412+			"GAME PAUSED",
413+			u.ScreenWidth/2-rl.MeasureText("GAME PAUSED", 40)/2,
414+			u.ScreenHeight/2-40,
415+			40,
416+			elements.LightGray,
417+		)
418+	}
419+}
420diff --git a/go.mod b/go.mod
421new file mode 100644
422index 0000000000000000000000000000000000000000..129be15cbc78e51740365b972d09172ccc291ad6
423--- /dev/null
424+++ b/go.mod
425@@ -0,0 +1,10 @@
426+module gitea.theedgeofrage.com/theedgeofrage/yeetris
427+
428+go 1.21.6
429+
430+require github.com/gen2brain/raylib-go/raylib v0.0.0-20240125111008-83d871a38f28
431+
432+require (
433+	github.com/ebitengine/purego v0.6.0-alpha.1.0.20231122024802-192c5e846faa // indirect
434+	golang.org/x/sys v0.14.0 // indirect
435+)
436diff --git a/go.sum b/go.sum
437new file mode 100644
438index 0000000000000000000000000000000000000000..b957b9cbe34e2b9dd34445e2976fa3727b9a981b
439--- /dev/null
440+++ b/go.sum
441@@ -0,0 +1,6 @@
442+github.com/ebitengine/purego v0.6.0-alpha.1.0.20231122024802-192c5e846faa h1:Ik7QikRgeH+bFOfAcMpttCbs6XxWXxCLXMm4awxtOXk=
443+github.com/ebitengine/purego v0.6.0-alpha.1.0.20231122024802-192c5e846faa/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ=
444+github.com/gen2brain/raylib-go/raylib v0.0.0-20240125111008-83d871a38f28 h1:e0HOaKlvsdC0ZtfWbEaaWAxYTSxUZoCAFS36jLfsVRA=
445+github.com/gen2brain/raylib-go/raylib v0.0.0-20240125111008-83d871a38f28/go.mod h1:P/hDjVwz/9fhR0ww3+umzDpDA7Bf7Tce4xNChHIEFqE=
446+golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
447+golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=