Pong Math

BlitzMax Forums/BlitzMax Programming/Pong Math

DaY(Posted 2016) [#1]
Ok so been working on a pong clone while trying to relearn to code after a stroke.


so far i have collision, basic ai(jitters) ball movement BUT it is not smooth
and the ball goes crazy at times any idears on were its going pair shaped?

i created the project in blide so i think it will work standalone but not tested


Framework brl.glmax2d
Import brl.glmax2d
Import brl.bmploader
Import brl.timer
Import brl.system
Import pub.libpng
Import brl.math
Import brl.random

Include "Includes\TActor.bmx"

'This BMX file was edited with BLIde ( http://www.blide.org )
Graphics 800, 600,, 60
SeedRnd(MilliSecs())
AutoMidHandle True
AutoImageFlags True

Const PLAYER_SPEED:Int = 7
Const COMPUTER_SPEED:Int = 6
Const XRANDOM_FACTOR:Int = 2
Const YRANDOM_FACTOR:Int = 1
Const START_XV_MIN:Int = 3
Const START_XV_MAX:Int = 6
Const START_YV_MIN:Int = -8
Const START_YV_MAX:Int = 8

' Create Player Using TActor
Global Player:TActor = New TActor.Create()
Player.m_Actor = LoadImage ("Media/player1.bmp")
Player.m_Posx = 10
Player.m_Posy = 250
Player.m_Alpha = 1
Player.m_Blend = 1
Player.m_Score = 0

' Create AI Using TActor
Global AI:TActor = New TActor.Create()
AI.m_Actor = LoadImage ("Media/player2.bmp")
AI.m_Posx = 770
AI.m_Posy = 250
AI.m_Alpha = 1
AI.m_Blend = 1
AI.m_Score = 0

' Create Ball Using TActor
Global Ball:TActor = New TActor.Create()
Ball.m_Actor = LoadImage ("Media/ball.bmp")
Ball.m_Posx = 400
Ball.m_Posy = 300
Ball.m_Alpha = 1
Ball.m_Blend = 1

' Initial Ball Movement
Ball.m_xv = Rand(START_XV_MIN, START_XV_MAX)
Ball.m_yv = Rand(START_YV_MIN, START_YV_MAX)

Repeat
	  Cls
		DrawText "Player 1: " + Player.m_Score, 0, 0
		DrawText "Player 2: " + AI.m_Score, 0, 20
		BallPhysics()
		Player.Actor()
		AI.Actor()
		Ball.Actor()		
	    UserInput()
	    AIMovement()
        Flip
Until KeyDown(KEY_ESCAPE)
EndGraphics

End

Function UserInput()
         If KeyDown(KEY_D) = 1 Then DebugStop
		 If KeyDown(KEY_UP)
		 	 Player.m_Posy = Player.m_Posy - PLAYER_SPEED
		 End If
		 If KeyDown(KEY_DOWN)
		 	 Player.m_Posy = Player.m_Posy + PLAYER_SPEED
		 End If
EndFunction

Function AIMovement()
	    If Ball.m_Posy > AI.m_Posy
		 	 AI.m_Posy = AI.m_Posy + COMPUTER_SPEED
		ElseIf Ball.m_Posy < AI.m_Posy
		     AI.m_Posy = AI.m_Posy - COMPUTER_SPEED
		End If
End Function

Function BallPhysics()
	    If ImagesCollide(Ball.m_Actor, Ball.m_Posx, Ball.m_Posy, 0, Player.m_Actor, 20, Player.m_Posy, 0)
		     Ball.m_xv = -Ball.m_xv + Rand(-XRANDOM_FACTOR, XRANDOM_FACTOR)
			 Ball.m_yv = Ball.m_yv + Rand(-YRANDOM_FACTOR, YRANDOM_FACTOR)
		ElseIf ImagesCollide(Ball.m_Actor, Ball.m_Posx, Ball.m_Posy, 0, AI.m_Actor, 760, AI.m_Posy, 0)
		     Ball.m_xv = -Ball.m_xv + Rand(-XRANDOM_FACTOR, XRANDOM_FACTOR)
			 Ball.m_yv = Ball.m_yv + Rand(-YRANDOM_FACTOR, YRANDOM_FACTOR)
		ElseIf Ball.m_Posy <= 0
		     Ball.m_xv = Ball.m_xv + Rand(-XRANDOM_FACTOR, XRANDOM_FACTOR)
			 Ball.m_yv = -Ball.m_yv + Rand(-YRANDOM_FACTOR, YRANDOM_FACTOR)
		ElseIf Ball.m_Posy >= 600
		     Ball.m_xv = Ball.m_xv + Rand(-XRANDOM_FACTOR, XRANDOM_FACTOR)
			 Ball.m_yv = -Ball.m_yv + Rand(-YRANDOM_FACTOR, YRANDOM_FACTOR)
		ElseIf Ball.m_Posx <= 0
		     AI.m_Score = AI.m_Score + 1
			 Cls
			 Ball.m_Posx = 400
			 Ball.m_Posy = 300
		ElseIf Ball.m_Posx >= 800
		     Player.m_Score = Player.m_Score + 1
			 Cls
			 Ball.m_Posx = 400
			 Ball.m_Posy = 300
		End If
		Ball.m_Posx = Ball.m_Posx + Ball.m_xv
		Ball.m_Posy = Ball.m_Posy + Ball.m_yv
End Function


Type TActor
     Field m_Actor:TImage
	 Field m_Sizex:Float, m_Sizey:Float
	 Field m_Alpha:Int, m_Blend:Int
	 Field m_Posx:Int, m_Posy:Int
	 Field m_Frame:Int, m_Flag:String
     Field m_Score:Int
     Field m_xv:Int
     Field m_yv:Int
	 
	 Method Actor()
			DrawImage m_Actor, m_Posx, m_Posy
			SetAlpha m_Alpha
			SetBlend m_Blend			
	 End Method
	 
	 Function Create:TActor()
	        Return New TActor
	 EndFunction
End Type



RustyKristi(Posted 2016) [#2]
Nice. maybe post your images or at least placeholders so we can try it directly?


Kryzon(Posted 2016) [#3]
Hi, some things I would change:
	Method Actor()

		'The SetAlpha and SetBlend should come before the DrawImage, they change the settings that the DrawImage will use.

		SetAlpha m_Alpha
		SetBlend m_Blend

		DrawImage m_Actor, m_Posx, m_Posy
	
	 End Method

- Rather than do 'Player.m_Blend = 1' use the constant for that, it's easier to read: 'Player.m_Blend = SOLIDBLEND' (you can find the constants in this page.)

If ImagesCollide(Ball.m_Actor, Ball.m_Posx, Ball.m_Posy, 0, Player.m_Actor, 20, Player.m_Posy, 0)
When the ball collides you do something with the ball direction. It's possible that the next frames the ball is still colliding and so changes direction every frame, causing it to go crazy.
Instead of imagescollide I would use simple rectangle intersection (if the ball.x > rectangle.x and ball.x+ball.width < rectangle.x+rectangle.width then...).

If you think things are too complex you should start again from scratch. You can follow this BlitzCoder tutorial on a simple pong clone (you need to adapt it to BlitzMax):
http://iadbungler.free.fr/bcoder/cgi-bin/articles/Wce60db5a3b5af.htm


DaY(Posted 2016) [#4]
ive added it to github for the images etc

https://github.com/WeyrSDev/Pongy-Code

im thinking of changeing it so Physlite handles the ball gotta figure it out lol

@Kryzon thx for the link thats handy, the ball is doing the collision correct its the way the ball reacts after the collision that it seem to go strange, I think my constants may be off and need tweaking or better time keeping.


i can only do so much on this laptop its like 7-8 years old and over heats and dies if you look at it wrong so i wasnt sure if its my end thats the problem.


RustyKristi(Posted 2016) [#5]
It looks good. Btw, if you could work your way and make it NG compatible you can do an android build. You're using compatible android modules anyway (except and not sure about Physlite) just use sdl as driver.


DaY(Posted 2016) [#6]
I cant compile Bmx-NG it cuts the pc out every time due to overheating and i cant get a new one lol

All i can do is use webrowser and BMX not great but its that or nothing!


RustyKristi(Posted 2016) [#7]
got it. btw, what's the .bls file for? some kind of tile editor file?


DaY(Posted 2016) [#8]
Thats for the blide IDE


RustyKristi(Posted 2016) [#9]
ah ok :) keep it up. you got something going there, maybe add basic multiplayer using raknet, bnet or bnetex.