This repository was archived by the owner on Apr 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.vb
More file actions
305 lines (258 loc) · 9.67 KB
/
Copy pathForm1.vb
File metadata and controls
305 lines (258 loc) · 9.67 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
Imports System.Threading
Public Class Form1
Public direction As String
'Color Snake
Public rS As Integer = My.Settings.snakeRed
Public gS As Integer = My.Settings.snakeGreen
Public bS As Integer = My.Settings.snakeBlue
'Color manzana
Public rM As Integer = My.Settings.manzRed
Public gM As Integer = My.Settings.manzGreen
Public bM As Integer = My.Settings.manzBlue
'Color campo
Public rC As Integer = My.Settings.campoRed
Public gC As Integer = My.Settings.campoGreen
Public bC As Integer = My.Settings.campoBlue
Public snakeBody, manzana As New List(Of PictureBox)
Public xMzn, yMzn As New List(Of Integer)
Public field As PictureBox
Public gameLoop As New Thread(AddressOf SnakeMainLoop)
Public bodyIndex As Integer = -1
Public gridX As Integer = (My.Settings.gridSizeX * 20) - 20
Public gridY As Integer = (My.Settings.gridSizeY * 20) - 20
Public startingPicPoint As New Point(9999, 9999)
Public snakeSpeed As Integer = My.Settings.PlayerSpeed
Public manzComida As Boolean = True
Public pause, comenzo As Boolean
Public random As New Random
Public puntos As Integer = -1
Public yPos, xPos, posicionesX((gridX + 20) / 20), posicionesY((gridY + 20) / 20), manzPosCounter As Integer
Public cantManzanas = My.Settings.cantManz
'Resize de barra de Info
Private Sub Form1_Resize(sender As Object, e As EventArgs) Handles MyBase.Resize
logGroup.Height = Me.Height - 51
End Sub
'Añade elementos a la List de manzanas, primero se limpia las Lists en caso de que la funcion se llame mientras el juego ya este iniciado
Sub ManzListAdditioner()
xMzn.Clear()
yMzn.Clear()
xMzn.Add(0)
yMzn.Add(0)
manzPosCounter = 0
While manzPosCounter <> cantManzanas
xMzn.Add(0)
yMzn.Add(0)
manzPosCounter += 1
End While
End Sub
'Cargar datos guardados, crear Snake, Manzanas y Field
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If My.Settings.debugMenu Then
UpdateInfo(True)
End If
ManzListAdditioner()
Form2.ApplySavedSettings()
For x As Integer = 0 To 256 Step 1
Dim snakeBodySetup As PictureBox
snakeBodySetup = New PictureBox With {
.BackColor = Color.FromArgb(rS, gS, bS),
.Width = 20,
.Height = 20,
.Location = startingPicPoint
}
snakeBody.Add(snakeBodySetup)
Me.Controls.Add(snakeBody(x))
Next
For z As Integer = 0 To 256 Step 1
Dim manzanaSetup As PictureBox
manzanaSetup = New PictureBox With {
.BackColor = Color.FromArgb(rM, gM, bM),
.Width = 20,
.Height = 20,
.Location = startingPicPoint
}
manzana.Add(manzanaSetup)
Me.Controls.Add(manzana(z))
Next
field = New PictureBox With {
.BackColor = Color.FromArgb(64, 64, 64),
.Width = (My.Settings.gridSizeY * 20),
.Height = (My.Settings.gridSizeX * 20),
.Location = New Point(0, 0)
}
Me.Controls.Add(field)
End Sub
'Termina los threads cuando se cierra el programa
Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
Try
gameLoop.Abort()
Catch ex As Exception
gameLoop.Resume()
gameLoop.Abort()
End Try
End Sub
'Detectar inputs y comienza el thread SnakeMainLoop
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
If e.KeyValue = Keys.W Or Keys.S Then
If e.KeyValue = Keys.W Then
snakeSpeed += 10
ElseIf e.KeyValue = Keys.S AndAlso Not snakeSpeed < 50 Then
snakeSpeed -= 10
End If
My.Settings.PlayerSpeed = snakeSpeed
UpdateInfo(True)
End If
If e.KeyValue = Keys.I Then
cantManzanas += 1
AddedManzanaUpdater()
ElseIf e.KeyValue = Keys.K AndAlso cantManzanas <> 0 Then
cantManzanas -= 1
AddedManzanaUpdater()
End If
If e.KeyValue = Keys.A AndAlso Not Form3.Visible Then
Form2.Show()
End If
If e.KeyValue = Keys.D Then
Form2.Close()
Form3.Show()
End If
If Form2.Visible Then
Form2.SettingSetup()
End If
If comenzo AndAlso e.KeyValue = Keys.P Then
If Not pause Then
pause = True
gameLoop.Suspend()
LabelPause.Visible = True
ElseIf pause Then
pause = False
gameLoop.Resume()
LabelPause.Visible = False
End If
End If
If Not pause Then
If comenzo = False Then
LabelTitle.Visible = False
LabelDesc.Visible = False
LabelAutor.Visible = False
logPuntNormal.Visible = True
direction = "right"
End If
If e.KeyCode = Keys.Down Then
direction = "down"
ElseIf e.KeyCode = Keys.Up Then
direction = "up"
ElseIf e.KeyCode = Keys.Right Then
direction = "right"
ElseIf e.KeyCode = Keys.Left Then
direction = "left"
End If
If Not comenzo Then
Try
gameLoop.Start()
Catch ex As Exception
gameLoop.Resume()
End Try
comenzo = True
field.BackColor = Color.FromArgb(rC, gC, bC)
AddedManzanaUpdater()
End If
End If
End Sub
'La base del juego, Movimiento del jugador, Actualizador de la cola y de la Manzana
Sub SnakeMainLoop()
While True
If direction = "down" Then
xPos += 20
ElseIf direction = "up" Then
xPos -= 20
ElseIf direction = "right" Then
yPos += 20
ElseIf direction = "left" Then
yPos -= 20
End If
If yPos < 0 Then
yPos = gridY
ElseIf xPos < 0 Then
xPos = gridX
ElseIf yPos > gridY Then
yPos = 0
ElseIf xPos > gridX Then
xPos = 0
End If
posicionesY(0) = yPos
posicionesX(0) = xPos
Try
If bodyIndex >= puntos Then
bodyIndex = -1
End If
bodyIndex += 1
snakeBody(bodyIndex).Location = New Point(yPos, xPos)
Catch ex As Exception
End Try
Try
For x As Integer = ((gridX + 20) / 20) * ((gridY + 20) / 20) To 1 Step -1
posicionesX(x) = posicionesX(x - 1)
posicionesY(x) = posicionesY(x - 1)
Next
Catch ex As Exception
End Try
For p As Integer = 0 To cantManzanas Step 1
If xPos & yPos = xMzn(p) & yMzn(p) Then
manzComida = True
End If
Next
Me.Invoke(New Action(Sub()
If manzComida = True AndAlso puntos >= 0 Then
ManzPosUpdater(False)
puntos += 1
ElseIf manzComida = True AndAlso puntos < 0 Then
ManzPosUpdater(True)
puntos += 1
End If
manzComida = False
UpdateInfo(False)
End Sub))
Thread.Sleep(snakeSpeed)
End While
End Sub
'Actualiza la posicion de la manzana consumida o de todas en caso de que "Respawn = True"
Sub ManzPosUpdater(respawnManzanas As Boolean)
For s As Integer = 0 To cantManzanas Step 1
If xPos & yPos = xMzn(s) & yMzn(s) OrElse respawnManzanas Then
xMzn(s) = (random.Next(0, (gridX + 20) / 20) * 20)
yMzn(s) = (random.Next(0, (gridY + 20) / 20) * 20)
Try
manzana(s).Location = New Point(yMzn(s), xMzn(s))
Catch ex As Exception
End Try
End If
Next
UpdateInfo(True)
End Sub
'Devuelve todos los PictureBox de manzanas a su posicion original
Sub AddedManzanaUpdater()
For Each manz In manzana
manz.Location = startingPicPoint
Next
ManzListAdditioner()
ManzPosUpdater(True)
My.Settings.cantManz = cantManzanas
UpdateInfo(True)
End Sub
'Actualiza Posicion del jugador en el menu de informacion
Sub UpdateInfo(showExtra As Boolean)
logPuntNormal.Text = "Puntos: " & puntos
If My.Settings.debugMenu Then
logX.Text = xPos.ToString
logY.Text = yPos.ToString
If showExtra Then
logManzY.Text = (cantManzanas + 1).ToString
logSpeed.Text = snakeSpeed.ToString
logSize.Text = ((gridY + 20) / 20) & "x" & ((gridX + 20) / 20)
logPunt.Text = puntos.ToString
logTheme.Text = My.Settings.ThemeUI
End If
End If
End Sub
End Class