Pong Help 2

Blitz3D Forums/Blitz3D Programming/Pong Help 2

mintybreath(Posted 2007) [#1]
Hi, i was bored so decided to make a pong variation game. Figured it would be fun.

I ran into a problem though. I made an input screen in the beginning of the game. Where it asks you how many players, 2 or 1. When you input 1, i tried to make it use the previosly created initializelevel() function i created. For some reason i cannot see, it doesnt work. It starts the game as normal, but with everything at the top of the screen on the y axis, and the ball behind player 1. Obiously this does not work.

If someone could please point out my mistake, or offer some help of any kind, that would really help me. Im just using an input for now, i may go to something bigger and better later, but for now just input help please.

Here is the code if anyone needs it.

Graphics3D 800,600


;seed the random generator
SeedRnd(MilliSecs())

SetBuffer BackBuffer()
AutoMidHandle True


;welcome options
two$=("2")
one$=("1")

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

welcome()

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

;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

;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\xv = ball\xv + 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

;___________________________________________
;_______________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()



Flip

Delay 20

Wend

End



b32(Posted 2007) [#2]
I think you didn't call initializelevel at the game start ?
Also, in this part:
;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\xv = ball\xv + Rand(-4,4)
There is two times \xv
And adding those Random values ? It could mean that a ball returns in the direction where it came from, or goes through your bat?


mintybreath(Posted 2007) [#3]
Oh ok, thanks for pointing that out. Ill check that out, and if i need more help, ill just post here.


mintybreath(Posted 2007) [#4]
One thing though, I did use Initializelevel() in the beginning. Look under the welcome() function, i said if the player$ value is equal to the one$ value, or a numerical 1, then initializelevel()

Do you see anything wrong with it that would make it not initializelevel like it should?


b32(Posted 2007) [#5]
Yes, one$ and two$ should be Global, however Const might be better, because they don't change.
const one$ = "1"
Else, they are empty in the welcome() Function.
Then, the welcome function should be placed after creating the ball and the two players (type instances)


Techlord(Posted 2007) [#6]
The 24 Hour Pong Making Contest!


mintybreath(Posted 2007) [#7]
There we go! that fixed my problem great. Thanks alot! :)