MORE PONG HELP?! yes please. :)

Blitz3D Forums/Blitz3D Programming/MORE PONG HELP?! yes please. :)

mintybreath(Posted 2007) [#1]
Ok, i bet everyone is getting kind of annoyed by my continuous pong help question, and are probably thinking, "Pong is so easy, i could code that in 10 minutes."

Well i am not very good at coding yet. So if someone could please help me again, i would really appreciate it. :)

I am making an input that opens in the beginning of the program, and gives you the option to enter how many players, 1 or 2. Well that seems to work, you enter a number and the program starts.

My problem comes when i am trying to make the number entered affect something. I am trying to make the player 2 AI only work when 1 player is picked, not 2. I have tried a vast majority of things to get this to work, and all of them have just made the second player not move, when any number is picked. I have tried while..wend statements, i have tried If..Then...else and if..then.

None of them seem to work. I may be using them wrong, but i really dont know, sorry if i am. :)

Could someone please tell me a good idea or suggestion to make the number 1 give the player 2 an AI, and make it so the player 2 does not.

I have tried everything i can think of, and it is not working, so if someone could please just help me with this?

here is my code if any of you need it.

Graphics3D 800,600


;seed the random generator
SeedRnd(MilliSecs())

SetBuffer BackBuffer()
AutoMidHandle True


;welcome options

Function welcome()
		player$=Input$("How many players? 1 or 2? ")
		If player$=two$ Then
		initializelevel()
		ElseIf player$=one$ Then
		initializelevel()
		Else 
	EndIf
End Function



AppTitle "Boredbb Pong V 1.0"

;consts
;the following are key constants
Const upkey = 200   ;up
Const downkey = 208  ;down
Const pausekey = 25  ; P button

Const humanspeed = 7 ;the humans maximum speed
Const computerspeed = 6 ;the computers maximum speed

Const one$="1"
Const two$="2"


;types
;the player type ;Human/Opponent
Type player
	Field y,score      ;y position the score
End Type

;the ball type ;for the ball
Type ball
	Field x,y,xv,yv    ;x/y cordinate and velocity
End Type



;images
;human player image
Global player1image = LoadImage("Red team.jpg")

;computer player image
Global player2image = LoadImage("Blue team.jpg")

;the picture of the ball
Global ballimage = LoadImage("ball.jpg") ;loads the ball image



;Type initialization
Function initializelevel()
;put ball in center of the screen
ball\x = 400
ball\y = 300

;make the ball move in a random direction
ball\xv = Rand(2,6)
ball\yv = Rand(-8,8)

;place the players in thier correct positions
player2\y = 300
player1\y = 300
End Function

;drawscore()
Function drawscore()
;write the human score
Text 700,0,"Player 1: "+player1\score
;write the computers score
Text 700,30,"Player 2: "+player2\score
End Function

;testkeyboard()
;moves the player up and down based on the keyboard inputs
Function testkeyboard()

;if player hits up, move up
If KeyDown(upkey)
	player1\y = player1\y - humanspeed
EndIf

;If player presses down, move him down
If KeyDown(downkey)
	player1\y = player1\y+ humanspeed
EndIf

;if player presses pause, then pause the game
If KeyHit(pausekey)
	;make screen blank
	Cls
	Text 400,300,"Press the P button to unpause the game."
	Flip
	;wait for player unpause
	While Not KeyDown(pausekey)
	Wend
EndIf
End Function

;testAI()
;update ball and score and enemy


Function TestAI()

;if ball is above the computer. move computer up
If ball\y>player2\y
	player2\y = player2\y + computerspeed
	
;if ball is lower than computer, move computer down
	ElseIf ball\y<player2\y
		player2\y = player2\y - computerspeed
		EndIf

End Function

Function BallAI()
;if ball hits human player, reflect it away.
If ImagesOverlap(ballimage,ball\x,ball\y,player1image,60,player1\y)
	ball\xv = -ball\xv + Rand(-4,4)
	ball\yv = ball\yv + Rand(-4,4)
	
;if ball hits computer , reflect it away
	ElseIf ImagesOverlap(ballimage,ball\x,ball\y,player2image,740,player2\y)
		ball\xv = -ball\xv + Rand(-4,4)
		ball\yv = ball\yv + Rand(-4,4)
		
;if ball hits top wall, reflect
	ElseIf ball\y <= 0
		ball\yv = -ball\yv + Rand(-1,1)
		ball\xv = ball\xv + Rand(-1,1)
		
;if ball hits the bottom wall, reflect it upward
	ElseIf ball\y >= 600
		ball\yv = -ball\yv + Rand(-1,1)
		ball\xv = ball\xv + Rand(-1,1)

;if ball hits the left wall, player 2 point
ElseIf ball\x <= 0
	player2\score = player2\score + 1   ;computer scores!
	Text 400,300,"Player 2 Scores!!!!"
	Flip
	;wait 2 seconds
	Delay(2000)
	
	;reset the level
	initializelevel()
	
;if ball hits right wall, human scored so he gets a point
ElseIf ball\x >= 800
	player1\score = player1\score + 1   ;human scores
	Text 400,300,"Player 1 Scores!!!!"
	Flip
	;wait 2 seconds
	Delay(2000)
	;reset level
	initializelevel()


EndIf

;if player hits the top, he stops
If player1\y <= 0 
	player1\y=player1\y + humanspeed
EndIf	

If player1\y >=600
	player1\y=player1\y - humanspeed
EndIf

If player2\y <= 0
	player2\y=player2\y + humanspeed
EndIf

If player2\y >= 600
	player2\y=player2\y - humanspeed
EndIf

;update ball's postition on screen
ball\x = ball\x + ball\xv
ball\y = ball\y + ball\yv



End Function



;create the ball
Global ball.ball = New ball
;create the human
Global player1.player = New player
;create the computer 
Global player2.player = New player



;set initial scores
player1\score = 0
player2\score = 0

welcome()

While player$=one$
testAI()
Wend



;___________________________________________
;_______________Main Loop___________________
;___________________________________________
While Not KeyDown(1)
;clears screen
Cls

;draw the ball
DrawImage (ballimage,ball\x,ball\y)
;draw the human
DrawImage (player1image,60,player1\y)
;draw the computer
DrawImage (player2image,740,player2\y)


;masking the green
MaskImage ballimage,11,255,20

;test what user has pressed
testkeyboard()
;what should AI do?
testAI()

;draw the hud
drawscore()
;Enemy AI
ballAI()

Flip

Delay 20

Wend

End


any real help is welcome.


mintybreath(Posted 2007) [#2]
Please, any suggestions or advise for making a difficulty level would really help. :)


b32(Posted 2007) [#3]
At the beginning of the game, store the number of players in a Global variable.
Btw, one$ and two$ don't have to be strings. You can also do this instead:
		If player$=2 Then
		initializelevel()
		ElseIf player$=1 Then
		initializelevel()

You could store 'player$' into this global variable, and during the game, respond to this variable.
If numberofplayers = 1 Then
   TestAI()
Else
   TestKeyBoard_Player2()
End If


In the original Pong, I heard, at some point the bats became smaller. I think the speed increases too. Other than that, it might be nice if the players could somehow control the ball a bit. Then you could introduce bonus points that are falling through the playing field.
However, it is difficult to really expand on Pong, since it quickly becomes Breakout instead. In the Breakout variant I've played, at some point, your bat became a gun with which you could fire at the bonus points.


mintybreath(Posted 2007) [#4]
Thanks b32 that really helps alot. Ill try that out. :)


mintybreath(Posted 2007) [#5]
Should i keep testAI() in the loop though? Cause what you just told me keeps the AI, but i can control the enemy as well when in multi...

and in single i can still control him... Ill test around with tweeking different things though until someone writes back.

Sorry about any irritation with all this pong stuff. :)

EDIT: I think the problem is that the functions are in the loop, which they seem to have to be, because when i take them out, it doesnt work. Since they are in the loop and there is nothing designating them to only work when player 1 is picked, they just work automatically. Does anyone know a way to fix this problem?


b32(Posted 2007) [#6]
Basically, in one player mode, you want to enable the AI, and in two player mode, the controls.
In order to do that, do this:
1) Add a Global variable 'numberofplayers'
2) In this part, add 'numberofplayers = 1' and 'numberofplayers = 2'
		If player$=two$ Then
		initializelevel()
		;<-here
		ElseIf player$=one$ Then
		initializelevel()
		;<-and here

3) After 'what should AI do?', add
If numberofplayers = 1 Then
   TestAI()
Else
   testkeyboardplayer2()
End If

4) Copy the function testkeyboard(), and name the copy testkeyboardplayer2()
5) Change the keys in that function to any other keys, and change all the 'player1's into 'player2's
6) Remove the pausekey from this second function


mintybreath(Posted 2007) [#7]
Ok ill try that out. Lol, trying to make it as simple as possible. :D

Anyway thanks, i am pretty sure i can in no way mess this up. :)


Edit:
Yes it worked! :D

Thanks alot b32, that really helped.