เกม Pong คล้ายๆของ Steve Jobs โดย VB.net ครับ  

ยังไม่เสร็จสมบูรณ์เป็นโจทย์ให้นร.ไปทำต่อนะครับ






Public Class Form1
Dim a(5, 5) As Double
Dim colors(5, 5) As Color
Dim rand As Random = New Random
Dim ballsize As Integer = 20
Dim x, y, vx, vy As Integer


Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

For i = 0 To a.GetUpperBound(0)

For j = 0 To a.GetUpperBound(0)
a(i, j) = 1
colors(i, j) = Color.FromArgb(rand.Next(255), rand.Next(255), rand.Next(255))
Next
Next
Me.DoubleBuffered = True

x = 300
y = 300
vx = rand.NextDouble * 20 - 10
vy = rand.NextDouble * 20 - 10

Timer1.Start()
Timer1.Interval = 10
End Sub



Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
Dim w, h As Integer
w = Me.Width
h = 80

For i = 0 To a.GetUpperBound(0)

For j = 0 To a.GetUpperBound(0)
If a(i, j) = 1 Then
e.Graphics.FillRectangle(New SolidBrush(colors(i, j)), _
CInt(j * w / a.GetLength(1)), _
CInt(i * h / a.GetLength(0)), _
CInt(w / a.GetLength(1)), _
CInt(h / a.GetLength(0)))
End If
Next
Next

e.Graphics.FillEllipse(New SolidBrush(Color.Red), _
CInt(x - ballsize), _
CInt(y - ballsize), _
CInt(ballsize * 2), _
CInt(ballsize * 2))

End Sub
Private Sub Form1_SizeChanged(sender As Object, e As EventArgs) Handles MyBase.SizeChanged
Refresh()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim w, h As Integer
w = Me.Width
h = 80
x = x + vx
y = y + vy
If x + ballsize > Me.Width Then
x = Me.Width - ballsize
vx = vx * -1
End If

If y + ballsize > Me.Height Then
y = Me.Height - ballsize
vy = vy * -1
End If
If x - ballsize < 0 Then
x = 0 + ballsize
vx = vx * -1
End If

If y - ballsize < 0 Then
y = 0 + ballsize
vy = vy * -1
End If

For i = 0 To a.GetUpperBound(0)

For j = 0 To a.GetUpperBound(0)
If a(i, j) = 1 Then
If x - ballsize < j * w / a.GetLength(1) + w / a.GetLength(1) And _
x - ballsize > j * w / a.GetLength(1) And _
y - ballsize < i * h / a.GetLength(0) + h / a.GetLength(0) And _
y - ballsize > i * h / a.GetLength(0) _
Then
a(i, j) = 0
vy = vy * -1
End If
End If
Next
Next
Refresh()
End Sub
End Class



[ view entry ] ( 2524 views )   |  permalink  |  $star_image$star_image$star_image$star_image$star_image ( 3 / 2215 )

<<First <Back | 1 | 2 | 3 | 4 | 5 | Next> Last>>