-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw.go
146 lines (119 loc) · 5.16 KB
/
draw.go
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/text"
"github.com/hajimehoshi/ebiten/v2/vector"
"image/color"
"strconv"
)
// Displays graphics on the screen based on the current state of the game.
func (g *game) Draw(screen *ebiten.Image) {
screen.Fill(globalBackgroundColor)
switch g.gameState {
case titleState:
g.titleDraw(screen)
case colorSelectState:
g.colorSelectDraw(screen)
case displayColorState:
g.displayColorDraw(screen)
case playState:
g.playDraw(screen)
case resultState:
g.resultDraw(screen)
}
}
// Displays graphics for the title screen.
func (g game) titleDraw(screen *ebiten.Image) {
text.Draw(screen, "Puissance 4 en réseau", largeFont, 90, 150, globalTextColor)
text.Draw(screen, "Projet de programmation système", smallFont, 105, 190, globalTextColor)
text.Draw(screen, "Année 2023-2024", smallFont, 210, 230, globalTextColor)
if g.stateFrame >= globalBlinkDuration/3 {
text.Draw(screen, "Appuyez sur entrée", smallFont, 210, 500, globalTextColor)
}
if g.isReady() {
text.Draw(screen, "Votre adversaire est prêt.", smallFont, 160, 650, globalSuccessColor)
} else {
text.Draw(screen, "Opposant non connecté.", smallFont, 185, 650, globalErrorColor)
}
}
// Displays graphics for the player color selection screen.
func (g game) colorSelectDraw(screen *ebiten.Image) {
text.Draw(screen, "Quelle couleur pour vos pions ?", smallFont, 110, 80, globalTextColor)
line := 0
col := 0
for numColor := 0; numColor < globalNumColor; numColor++ {
xPos := (globalNumTilesX-globalNumColorCol)/2 + col
yPos := (globalNumTilesY-globalNumColorLine)/2 + line
if numColor == g.p1Color {
vector.DrawFilledCircle(screen, float32(globalTileSize/2+xPos*globalTileSize), float32(globalTileSize+globalTileSize/2+yPos*globalTileSize), globalTileSize/2, globalSelectColor, true)
}
if numColor == g.p2Color {
vector.DrawFilledCircle(screen, float32(globalTileSize/2+xPos*globalTileSize), float32(globalTileSize+globalTileSize/2+yPos*globalTileSize), globalTileSize/2, globalTokenOpacityColors[numColor], true)
vector.DrawFilledCircle(screen, float32(globalTileSize/2+xPos*globalTileSize), float32(globalTileSize+globalTileSize/2+yPos*globalTileSize), globalTileSize/2-globalCircleMargin, globalTokenOpacityColors[numColor], true)
} else {
vector.DrawFilledCircle(screen, float32(globalTileSize/2+xPos*globalTileSize), float32(globalTileSize+globalTileSize/2+yPos*globalTileSize), globalTileSize/2-globalCircleMargin, globalTokenColors[numColor], true)
}
col++
if col >= globalNumColorCol {
col = 0
line++
}
}
}
// Displays graphics for the screen showing the colors of both players.
func (g game) displayColorDraw(screen *ebiten.Image) {
text.Draw(screen, "Vos choix de couleurs", smallFont, 200, 80, globalTextColor)
y := 250
radius := globalTileSize
vector.DrawFilledCircle(screen, float32(100+radius), float32(y+radius), float32(radius-globalCircleMargin), globalTokenColors[g.p1Color], true)
text.Draw(screen, "Vous", smallFont, 165, 500, globalTextColor)
vector.DrawFilledCircle(screen, float32(400+radius), float32(y+radius), float32(radius-globalCircleMargin), globalTokenColors[g.p2Color], true)
text.Draw(screen, "Adversaire", smallFont, 425, 500, globalTextColor)
if g.startTimer > 0 {
remainingSeconds := ((startCountdown * 60) - g.startTimer) / 60
if remainingSeconds > 0 {
text.Draw(screen, "Lancement dans "+strconv.Itoa(remainingSeconds)+" secondes", smallFont, 150, 650, globalSuccessColor)
} else {
text.Draw(screen, "Lancement imminent", smallFont, 210, 650, globalSuccessColor)
}
} else {
text.Draw(screen, "En attente de votre adversaire.", smallFont, 135, 650, globalErrorColor)
}
}
// Displays graphics during the game.
func (g game) playDraw(screen *ebiten.Image) {
g.drawGrid(screen)
vector.DrawFilledCircle(screen, float32(globalTileSize/2+g.tokenPosition*globalTileSize), float32(globalTileSize/2), globalTileSize/2-globalCircleMargin, globalTokenColors[g.p1Color], true)
}
// Displays graphics on the results screen.
func (g game) resultDraw(screen *ebiten.Image) {
g.drawGrid(offScreenImage)
options := &ebiten.DrawImageOptions{}
options.ColorScale.ScaleAlpha(0.2)
screen.DrawImage(offScreenImage, options)
message := "Égalité"
if g.result == p1wins {
message = "Gagné !"
} else if g.result == p2wins {
message = "Perdu…"
}
text.Draw(screen, message, smallFont, 300, 400, globalTextColor)
}
// Displays the Connect 4 grid, including the already played tokens.
func (g game) drawGrid(screen *ebiten.Image) {
vector.DrawFilledRect(screen, 0, globalTileSize, globalTileSize*globalNumTilesX, globalTileSize*globalNumTilesY, globalGridColor, true)
for x := 0; x < globalNumTilesX; x++ {
for y := 0; y < globalNumTilesY; y++ {
var tileColor color.Color
switch g.grid[x][y] {
case p1Token:
tileColor = globalTokenColors[g.p1Color]
case p2Token:
tileColor = globalTokenColors[g.getP2Color()]
default:
tileColor = globalBackgroundColor
}
vector.DrawFilledCircle(screen, float32(globalTileSize/2+x*globalTileSize), float32(globalTileSize+globalTileSize/2+y*globalTileSize), globalTileSize/2-globalCircleMargin, tileColor, true)
}
}
}