-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
109 lines (88 loc) · 3.05 KB
/
main.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
-- Define tables for computer and human players
local players = {
computer = {},
human = {}
}
-- Define window dimensions
local window = {
width = 777,
height = 472
}
-- Variable to track game start
local start = false
-- Load font
local font
function love.load()
-- Set window title and mode
love.window.setTitle('Dice')
love.window.setMode(window.width, window.height, {resizable = false, vsync = false})
-- Set background color
love.graphics.setBackgroundColor(0, 0, 0)
-- Seed random number generator (moved to love.load)
math.randomseed(os.time())
-- Load images for human and computer
players.human.img = love.graphics.newImage('img/d1.png')
players.computer.img = love.graphics.newImage('img/d1_inverted.png')
-- Set player names
players.human.name = "Human"
players.computer.name = "Computer"
-- Set players dice position
players.human.position = 0
-- Load font
font = love.graphics.setNewFont("font/Sniglet-webfont.ttf", 72)
end
function love.draw()
-- Check if game has started
if start == false then
-- Display game result
local winnerText = players.human.win and "Human wins!" or "Computer wins!"
love.graphics.printf(winnerText, 0, window.height - 76, window.width, 'center')
else
-- Display instructions to start game
love.graphics.printf("Click to roll", 0, window.height - 76, window.width, 'center')
end
-- Draw human and computer dice images
local humanX, computerX = 33, window.width * 0.5
if players.human.position == 1 then
humanX, computerX = computerX, humanX
end
love.graphics.draw(players.human.img, humanX, 30, 0, 0.2, 0.2)
love.graphics.draw(players.computer.img, computerX, 30, 0, 0.2, 0.2)
end
function love.mousepressed(x, y, button)
if start then return end -- Do nothing if the game has already started
players.human.position = x < window.width/2 and 0 or 1
startGame(button)
end
function startGame(button)
-- Reset game state
start = false
-- Roll dice and handle cheats
rollDices()
if button == 2 then
while players.human.roll > players.computer.roll do
rollDices()
end
elseif button == 3 then
while players.human.roll < players.computer.roll do
rollDices()
end
end
-- Load dice images based on roll results
players.human.img = love.graphics.newImage('img/d' .. players.human.roll .. '.png')
players.computer.img = love.graphics.newImage('img/d' .. players.computer.roll .. '_inverted.png')
-- Determine winner
players.human.win = players.human.roll > players.computer.roll
end
function rollDices()
-- Roll dice for computer and human
local computerRoll, humanRoll
-- Ensure the rolls are different
repeat
computerRoll = math.random(1, 6)
humanRoll = math.random(1, 6)
until computerRoll ~= humanRoll
-- Assign rolls to players
players.computer.roll = computerRoll
players.human.roll = humanRoll
end