|
-
May 17th, 2013, 09:10 PM
#1
Thread Starter
New Member
Creating 2 player pong, not working!
So I'm trying to create a 2 player pong game, however I have 2 problems.
1. After adding a moving ball, the player1's paddle will now not move. The arrow keys will not make it move, however it was all working before adding the ball
2. I haven't done this in this version yet, but in another version I added 2 players, and only one player could move at a time. As soon as I used the arrow keys (player 1) player 2 could not move using WASD.
Thanks for any help! I'm using VB 2008 Express, Code below:
Code:
Public Class Pong
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyCode
Case Keys.Left
Me.Player1.Left -= 1
Case Keys.Up
Me.Player1.Top -= 1
Case Keys.Right
Me.Player1.Left += 1
Case Keys.Down
Me.Player1.Top += 1
End Select
End Sub
Private Sub Ball_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles Ball.PreviewKeyDown
Select Case e.KeyCode
Case Keys.Left, Keys.Up, Keys.Right, Keys.Down
e.IsInputKey = True
End Select
End Sub
Private Sub Pong_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
'Set up ball speeed and movement
Dim speed As Single = 10
'Create a random instance to keep the game exciting
Dim rndInst As New Random()
'Set the minimum and maximum angle of randomisation
Dim xVel As Single = Math.Cos(rndInst.Next(8, 12)) * speed
Dim yVel As Single = Math.Sin(rndInst.Next(4, 14)) * speed
Private Sub Gametimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Gametimer.Tick
'Move the ball
Ball.Location = New Point(Ball.Location.X + xVel, Ball.Location.Y + yVel)
'Now we must make sure the ball never leaves the game area
If Ball.Location.Y < 0 Then
Ball.Location = New Point(Ball.Location.X, 0)
'Set the velocity to the opposite of the velocity when hitting the ball
yVel = -yVel
End If
'Check if the ball hits the bottom of the playing feild
If Ball.Location.Y > Me.Height - Ball.Size.Height - 45 Then
Ball.Location = New Point(Ball.Location.X, Me.Height - Ball.Size.Height - 45)
yVel = -yVel
End If
'Check if the ball hits a players paddle
If Ball.Bounds.IntersectsWith(Player1.Bounds) Then
Ball.Location = New Point(Player1.Location.X - Ball.Size.Width, Ball.Location.Y)
xVel = -xVel
End If
'Check if the ball hits a players paddle
If Ball.Bounds.IntersectsWith(player2.Bounds) Then
Ball.Location = New Point(player2.Location.X + Ball.Size.Width, Ball.Location.Y)
xVel = -xVel
End If
End Sub
Private Sub Ball_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Ball.Click
End Sub
Private Sub play_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles play.Click
Gametimer.Enabled = True
End Sub
End Class
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|