A great pong game, fully working this time!!
This is a fully working pong game, almost identical to the origional one. It has a fairly good (but very basic) computer player, and the ball deflects at different angles depending on how far away from the center of the paddle it is. I just reposted this, so the code is posted instead of a zip file, that way every body can view it.
AI
Podsumowanie AI: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.
Kod źródłowy
Dim vmom As Integer 'vertical momentum Dim hmom As Integer 'horizontal momentum Dim padSpeed As Integer 'the speed of the players paddle Dim origPaddleLoc As Integer Dim origBallLocY As Integer Dim origBallLocX As Integer Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) If KeyCode = 38 Then 'the up key padSpeed = -150 ElseIf KeyCode = 40 Then 'the down key padSpeed = 150 End If End Sub Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer) padSpeed = 0 'stop the paddle from moving End Sub Private Sub Form_Load() hmom = -150 vmom = 0 'record the origional starting locations for everything origPaddleLoc = shpPlayer1.Top origBallLocX = shpBall.Left origBallLocY = shpBall.Top End Sub Private Sub Timer1_Timer() 'move the ball shpBall.Top = shpBall.Top + vmom shpBall.Left = shpBall.Left + hmom 'check to see if the ball's hit a wall If shpBall.Top + shpBall.Height >= shpWallBottom.Top Then shpBall.Top = shpWallBottom.Top - shpBall.Height vmom = -vmom Beep ElseIf shpBall.Top <= shpWallTop.Top + shpWallTop.Height Then shpBall.Top = shpWallTop.Top + shpWallTop.Height vmom = -vmom Beep End If 'move the paddle If padSpeed <> 0 Then shpPlayer1.Top = shpPlayer1.Top + padSpeed End If 'check to see if the paddle's hit a wall If shpPlayer1.Top <= shpWallTop.Top + shpWallTop.Height Then shpPlayer1.Top = shpWallTop.Top + shpWallTop.Height ElseIf shpPlayer1.Top + shpPlayer1.Height >= shpWallBottom.Top Then shpPlayer1.Top = shpWallBottom.Top - shpPlayer1.Height End If If shpPlayer2.Top <= shpWallTop.Top + shpWallTop.Height Then shpPlayer2.Top = shpWallTop.Top + shpWallTop.Height ElseIf shpPlayer2.Top + shpPlayer2.Height >= shpWallBottom.Top Then shpPlayer2.Top = shpWallBottom.Top - shpPlayer2.Height End If 'move the computer player's paddle If shpBall.Top < shpPlayer2.Top Then shpPlayer2.Top = shpPlayer2.Top - 250 ElseIf shpBall.Top > shpPlayer2.Top + shpPlayer2.Height Then shpPlayer2.Top = shpPlayer2.Top + 250 End If 'if the ball has hit player 1's paddle If shpBall.Left <= shpPlayer1.Left + shpPlayer1.Width And shpBall.Left >= shpPlayer1.Left - shpPlayer1.Width Then If shpBall.Top + shpBall.Height >= shpPlayer1.Top And shpBall.Top <= shpPlayer1.Top + shpPlayer1.Height Then 'calculate the angle it's deflecting off at tmp = ((shpPlayer1.Top + (shpPlayer1.Height / 2)) - (shpBall.Top + (shpBall.Height / 2))) * 0.55 vmom = vmom + -tmp Beep shpBall.Left = shpPlayer1.Left + shpPlayer1.Width 'deflect the ball hmom = -hmom End If End If 'if the ball has hit player 2's paddle If shpBall.Left + shpBall.Width >= shpPlayer2.Left And shpBall.Left <= shpPlayer2.Left + shpPlayer2.Width Then If shpBall.Top + shpBall.Height >= shpPlayer2.Top And shpBall.Top <= shpPlayer2.Top + shpPlayer2.Height Then 'calculate the angle it's deflecting off at tmp = ((shpPlayer2.Top + (shpPlayer2.Height / 2)) - (shpBall.Top + (shpBall.Height / 2))) * 0.55 vmom = vmom + -tmp Beep shpBall.Left = shpPlayer2.Left - shpBall.Width 'deflect the ball hmom = -hmom End If End If 'see if someone's won If shpBall.Left + shpBall.Width < 0 Then 'reset the ball and paddles to their origional location shpBall.Left = origBallLocX shpBall.Top = origBallLocY shpPlayer1.Top = origPaddleLoc shpPlayer2.Top = origPaddleLoc hmom = -150 vmom = 0 ElseIf shpBall.Left > Form1.Width Then 'reset the ball and paddles to their origional location shpBall.Left = origBallLocX shpBall.Top = origBallLocY shpPlayer1.Top = origPaddleLoc shpPlayer2.Top = origPaddleLoc hmom = 150 vmom = 0 End If End Sub
Oryginalne komentarze (3)
Odzyskane z Wayback Machine