Score rendering

This commit is contained in:
Dusk 2023-09-25 15:09:32 +02:00
parent 8fbcc7e915
commit 4f459e5125
3 changed files with 24 additions and 5 deletions

View File

@ -11,6 +11,8 @@ const input = @import("input.zig");
const Renderer = @import("Game/Renderer.zig"); const Renderer = @import("Game/Renderer.zig");
const movement = @import("Game/movement.zig"); const movement = @import("Game/movement.zig");
pub const Score = u64;
const Self = @This(); const Self = @This();
// Codes to access action list // Codes to access action list
@ -36,6 +38,8 @@ held: ?Piece.Type,
piece: Piece, piece: Piece,
shadow: Piece, shadow: Piece,
score: Score = 0,
action_list: ActionList = .{ action_list: ActionList = .{
.right = Action.init(input.game_right, actionRight), // Right .right = Action.init(input.game_right, actionRight), // Right
.left = Action.init(input.game_left, actionLeft), // Left .left = Action.init(input.game_left, actionLeft), // Left
@ -70,7 +74,7 @@ pub fn init(renderer: *Renderer.Renderer) Self {
.timer_left_das = Timer.init(0.2), .timer_left_das = Timer.init(0.2),
.timer_right_arr = Timer.init(0.05), .timer_right_arr = Timer.init(0.05),
.timer_right_das = Timer.init(0.2), .timer_right_das = Timer.init(0.2),
.timer_gravity = Timer.init(0.2), .timer_gravity = Timer.init(0.3),
}; };
// Get a piece from the bag // Get a piece from the bag
@ -205,7 +209,7 @@ pub fn tick(self: *Self) State {
} }
} }
self.grid.clearLines(); self.score += self.grid.clearLines();
self.renderer.render(self.*); self.renderer.render(self.*);

View File

@ -30,7 +30,8 @@ pub fn init() Self {
return self; return self;
} }
pub fn clearLines(self: *Self) void { pub fn clearLines(self: *Self) u8 {
var ncleared: u8 = 0;
for (self.cells, 0..) |_, y| { for (self.cells, 0..) |_, y| {
for (self.cells[y], 0..) |cell_x, x| { for (self.cells[y], 0..) |cell_x, x| {
if (cell_x.free) { if (cell_x.free) {
@ -42,8 +43,10 @@ pub fn clearLines(self: *Self) void {
for (self.cells[1..y], 0..) |_, i| { for (self.cells[1..y], 0..) |_, i| {
self.cells[y - i] = self.cells[y - i - 1]; self.cells[y - i] = self.cells[y - i - 1];
} }
ncleared += 1;
} }
} }
} }
} }
return ncleared;
} }

View File

@ -1,5 +1,4 @@
const SDL = @import("sdl2"); const std = @import("std");
const Game = @import("../Game.zig"); const Game = @import("../Game.zig");
const Bag = @import("Bag.zig"); const Bag = @import("Bag.zig");
const Grid = @import("Grid.zig"); const Grid = @import("Grid.zig");
@ -105,6 +104,7 @@ pub fn render(self: *Self, game: Game) void {
renderPiece(self, game.shadow); renderPiece(self, game.shadow);
renderBag(self, game); renderBag(self, game);
renderHeld(self, game); renderHeld(self, game);
renderScore(self, game.score);
} }
pub fn renderPiece(self: *Self, piece: Piece) void { pub fn renderPiece(self: *Self, piece: Piece) void {
@ -130,6 +130,7 @@ pub fn renderGrid(self: *Self, grid: Grid) void {
const pos_x = self.grid_pos_x; const pos_x = self.grid_pos_x;
const pos_y = self.grid_pos_y; const pos_y = self.grid_pos_y;
var visible = grid.cells[Grid.buffer..]; var visible = grid.cells[Grid.buffer..];
for (visible, 0..) |_, y| { for (visible, 0..) |_, y| {
for (visible[y], 0..) |_, x| { for (visible[y], 0..) |_, x| {
@ -151,3 +152,14 @@ pub fn renderGrid(self: *Self, grid: Grid) void {
} }
} }
} }
pub fn renderScore(self: *Self, score: Game.Score) void {
const s_size = self.renderer.getOutputSize();
var buffer: [32]u8 = undefined;
var score_string = std.fmt.bufPrintZ(&buffer, "Score: {d:0>16}", .{score}) catch "Score: " ++ "9" ** 16;
self.renderer.setColor(.{0,0,0,255});
self.renderer.drawText(score_string, s_size.width - 400, 50, 20);
}