Added end of round detection and scoring
This commit is contained in:
parent
08d7527237
commit
2f76bca8dc
3 changed files with 30 additions and 4 deletions
|
@ -20,7 +20,7 @@ pub const Ball = struct {
|
||||||
self.vx = 0;
|
self.vx = 0;
|
||||||
self.vy = -250;
|
self.vy = -250;
|
||||||
}
|
}
|
||||||
pub fn update(self: *Ball) void {
|
pub fn update(self: *Ball) ?utils.side {
|
||||||
self.vy += utils.gravity;
|
self.vy += utils.gravity;
|
||||||
self.x += self.vx * utils.frequency;
|
self.x += self.vx * utils.frequency;
|
||||||
self.y += self.vy * utils.frequency;
|
self.y += self.vy * utils.frequency;
|
||||||
|
@ -40,6 +40,11 @@ pub const Ball = struct {
|
||||||
if (self.y >= 160 - ball_height) { // floor
|
if (self.y >= 160 - ball_height) { // floor
|
||||||
self.vy = 0;
|
self.vy = 0;
|
||||||
self.y = 160 - ball_height;
|
self.y = 160 - ball_height;
|
||||||
|
if (self.x <= 80) {
|
||||||
|
return .left;
|
||||||
|
} else {
|
||||||
|
return .right;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// Net collision left
|
// Net collision left
|
||||||
var x1: f64 = 78 - ball_width;
|
var x1: f64 = 78 - ball_width;
|
||||||
|
@ -85,6 +90,7 @@ pub const Ball = struct {
|
||||||
self.x = x2;
|
self.x = x2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
13
src/game.zig
13
src/game.zig
|
@ -37,13 +37,22 @@ pub const Game = struct {
|
||||||
self.brothers[0].resetRound();
|
self.brothers[0].resetRound();
|
||||||
self.brothers[1].resetRound();
|
self.brothers[1].resetRound();
|
||||||
}
|
}
|
||||||
pub fn update(self: *Game) void {
|
pub fn update(self: *Game) bool {
|
||||||
self.gamepads[0].update(w4.GAMEPAD1.*);
|
self.gamepads[0].update(w4.GAMEPAD1.*);
|
||||||
self.gamepads[1].update(w4.GAMEPAD2.*);
|
self.gamepads[1].update(w4.GAMEPAD2.*);
|
||||||
self.brothers[0].update(self.gamepads[0]);
|
self.brothers[0].update(self.gamepads[0]);
|
||||||
self.brothers[1].update(self.gamepads[1]);
|
self.brothers[1].update(self.gamepads[1]);
|
||||||
self.ball.update();
|
const finished = self.ball.update();
|
||||||
self.brothers[0].collide(&self.ball);
|
self.brothers[0].collide(&self.ball);
|
||||||
self.brothers[1].collide(&self.ball);
|
self.brothers[1].collide(&self.ball);
|
||||||
|
if (finished) |side| {
|
||||||
|
if (side == .left) {
|
||||||
|
self.brothers[1].score += 1;
|
||||||
|
} else {
|
||||||
|
self.brothers[0].score += 1;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
13
src/main.zig
13
src/main.zig
|
@ -5,12 +5,23 @@ const w4 = @import("wasm4.zig");
|
||||||
|
|
||||||
//----- Globals ---------------------------------------------------------------
|
//----- Globals ---------------------------------------------------------------
|
||||||
var Game: game.Game = undefined;
|
var Game: game.Game = undefined;
|
||||||
|
var wait_before_new_round: u8 = 0;
|
||||||
|
|
||||||
export fn start() void {
|
export fn start() void {
|
||||||
Game.reset();
|
Game.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
export fn update() void {
|
export fn update() void {
|
||||||
Game.update();
|
if (wait_before_new_round == 0) {
|
||||||
|
const finished = Game.update();
|
||||||
|
if (finished) {
|
||||||
|
wait_before_new_round = 60;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
wait_before_new_round -= 1;
|
||||||
|
if (wait_before_new_round == 0) {
|
||||||
|
Game.resetRound();
|
||||||
|
}
|
||||||
|
}
|
||||||
Game.draw();
|
Game.draw();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue