How do I set various difficulty settings?

Blitz3D Forums/Blitz3D Programming/How do I set various difficulty settings?

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

This code hardly does anything at all to the enemy AI, as a matter of fact, he doesn't even have a "brain" at all, he's just a free-floating sprite without having any commands to sense and crash into you at all. This wasn't the type of code I was expecting, how should this really be done? Here's the updated code:

; Plasmorg.bb - This is the source code for the game project, "Plasmorg:  Science Chaos Theory"
; (c)2006, Robert A. Morin, RAM Digital Media
; -------------------------------------------

; First, set the graphics resolution to 800x600
Graphics 800,600



; Below is the copyright information, the logo screen, and the game title logo sequence...

; First, make sure that the screen is clear before continuing
Cls

; First, load the images for the copyright, the title, and the game logo
Global copyrightimage = LoadImage("copyright.bmp")
Global logoimage = LoadImage("logo.bmp")
Global titleimage = LoadImage("title.bmp")
Global epilogueimage = LoadImage("epilogue.bmp")
Global menuimage = LoadImage("mainmenu.bmp")
Global difficultyimage = LoadImage("difficulty.bmp")


; This first image is the red/purple/magenta smoke in the background
Global redimage = LoadImage("redsmoke.bmp")

; The front image is the yellow/cyan/periwinkle image of smoke
Global yellowimage = LoadImage("yellowsmoke.bmp")

; Now, load in the spooky background sound in the background
Global backsound = LoadSound("background.wav")
Global bulletsound = LoadSound("zapper.wav")
Global magicsound = LoadSound("magic.wav")
Global explosionsound = LoadSound("explode.wav")
Global ambientsound = LoadSound("ambience.wav")
Global logosound = LoadSound("whoosh.wav")


; Now, take a variable to track the scrolling of these images
scrolly = 0



; The following are the constants used for this game

Const PLAYERSPEED = 10
Const ENEMYSPEEDEASY = 5, ENEMYSPEEDMEDIUM = 10, ENEMYSPEEDHARD = 20, ENEMYSPEEDCHAOS = 30

Const NORMALBULLET = 1, MISSILE = 2




; Below, load the type used to move the player's object around the screen
Type player
	Field x, y 	; These define the coordinates that the player moves on
	Field frame	; This defines the frames that are drawn from the animation
	Field collision ; This defines the collision done between you and your enemy
	Field hits	; These are the hits that you knock down your enemy with
	Field image ; This defines the image for the player
End Type

Type enemy
	Field x, y	; These define the coordinates that the enemy moves on
	Field xv, yv	; These define the velocity that the enemy AI uses
	Field image	; This defines the image that the enemy uses
	Field frame	; This will define the frames used in the rotation of the enemy when he moves
End Type

Type bullet
	Field x, y	; These define the coordinates that the bullet takes
	Field image	; This is the image definition for the bullet
	Field bullettype	; This defines whether you have a short spike or a magic brick block
	Field frame	; This defines the frame for the animations when the bullet or missile is fired
End Type

; Now, the following will create the player for the game

; Provide the starting values for the player and the enemy

Global player.player = New player
player\x = 400
player\y = 400
player\frame = 0
player\collision = 0
player\image = LoadAnimImage("player.bmp",64,64,0,8)

Global enemy.enemy = New enemy
enemy\x = 400
enemy\y = 200
enemy\xv = Rand(-5,5)
enemy\yv = Rand(-5,5)
enemy\image = LoadAnimImage("enemy.bmp",64,64,0,8)
enemy\frame = 0

; Display the copyright/credits screen first

; Make sure the screen is clear
Cls
	DrawImage copyrightimage,0,0
	Delay 5000
	
; Now, clear the screen and display the RAM Digital Media logo
Cls
	DrawImage logoimage,0,0
	PlaySound logosound
	Delay 5000
	
; Then, load the game title screen 
Cls
	DrawImage titleimage,0,0
	; Play and loop the spooky ambience for this game
	LoopSound ambientsound
	PlaySound ambientsound
	; Start playing the sound while the background loads
	LoopSound backsound
	PlaySound backsound
	Delay 5000

; Next, load the epilogue, and wait for the player to press a key to start
Cls
	DrawImage epilogueimage,0,0
	start=WaitKey()

; Go to the main menu screen
Cls
	DrawImage menuimage,0,0

	resume=WaitKey()
; Once player chooses to start, bring up the different difficulty levels for the game
If resume = KeyHit(3)
	End
EndIf

; If the start game was hit, continue, if the quit game was hit, quit
	
Cls
	DrawImage difficultyimage,0,0
	key=WaitKey()
	
	

; Initialize the default settings for AutoMidHandle and BackBuffer
AutoMidHandle True
SetBuffer BackBuffer()





; Below starts the main loop for this part of the program
While Not KeyDown(1)

; Next, make sure that the screen is clear before setting the smoke
Cls



; Now, tile the red smoke below, and the brighter smoke on the second layer
TileImage redimage,0,scrolly
TileImage yellowimage,0,scrolly*2

; Increase the value of the scrolly variable
scrolly = scrolly + 1

; Once the tracking variable exceeds the maximum value, reset it to 0
If scrolly >= ImageHeight(yellowimage)
	scrolly = 0
EndIf

; Display the image's coordinates for his movements
Text 0,0, "X Coord:\>  " + player\x
Text 0,12, "Y Coord:\>  " + player\y
Text 0,500, "PLAYER SCORE:\>  " + player\hits
Text 0,512, "ENEMY SCORE :\>  " + player\collision

TestEnemyCollisions()

CollisionCrash()




If KeyDown(57)
	bullets.bullet = New bullet
	bullets\x = player\x
	bullets\y = player\y
	bullets\image = LoadAnimImage("bullet.bmp",12,12,0,8)
	bullets\bullettype = NORMALBULLET
	bullets\frame = 0
	PlaySound bulletsound
EndIf

If KeyDown(14)
	bullets.bullet = New bullet
	bullets\x = player\x
	bullets\y = player\y
	bullets\image = LoadAnimImage("block.bmp",32,32,0,8)
	bullets\bullettype = MISSILE
	bullets\frame = 0
	PlaySound magicsound
EndIf

UpDateBullets()

; Below is the enemy's image specifications
enemy\image = LoadAnimImage("enemy.bmp",64,64,0,8)



; Move the enemy in the same direction as the player does, thus, trying to crash into him

If key = KeyHit(2)
	If player\y > enemy\y
		enemy\y = enemy\y + ENEMYSPEEDEASY	
		enemy\frame = enemy\frame - 1
	EndIf

	If player\x < enemy\x			
		enemy\x = enemy\x - ENEMYSPEEDEASY
		enemy\frame = enemy\frame - 1
	EndIf

	If player\y < enemy\y
		enemy\y = enemy\y - ENEMYSPEEDEASY
		enemy\frame = enemy\frame + 1
	EndIf

	If player\x > enemy\x
		enemy\x = enemy\x + ENEMYSPEEDEASY
		enemy\frame = enemy\frame + 1
	EndIf
EndIf

If key = KeyHit(3)
	If player\y > enemy\y
		enemy\y = enemy\y + ENEMYSPEEDMEDIUM	
		enemy\frame = enemy\frame - 1
	EndIf

	If player\x < enemy\x			
		enemy\x = enemy\x - ENEMYSPEEDMEDIUM
		enemy\frame = enemy\frame - 1
	EndIf

	If player\y < enemy\y
		enemy\y = enemy\y - ENEMYSPEEDMEDIUM
		enemy\frame = enemy\frame + 1
	EndIf

	If player\x > enemy\x
		enemy\x = enemy\x + ENEMYSPEEDMEDIUM
		enemy\frame = enemy\frame + 1
	EndIf
EndIf

If key = KeyHit(4)
	If player\y > enemy\y
		enemy\y = enemy\y + ENEMYSPEEDHARD	
		enemy\frame = enemy\frame - 1
	EndIf

	If player\x < enemy\x			
		enemy\x = enemy\x - ENEMYSPEEDHARD
		enemy\frame = enemy\frame - 1
	EndIf

	If player\y < enemy\y
		enemy\y = enemy\y - ENEMYSPEEDHARD
		enemy\frame = enemy\frame + 1
	EndIf

	If player\x > enemy\x
		enemy\x = enemy\x + ENEMYSPEEDHARD
		enemy\frame = enemy\frame + 1
	EndIf
EndIf

If key = KeyHit(5)
	If player\y > enemy\y
		enemy\y = enemy\y + ENEMYSPEEDCHAOS	
		enemy\frame = enemy\frame - 1
	EndIf

	If player\x < enemy\x			
		enemy\x = enemy\x - ENEMYSPEEDCHAOS
		enemy\frame = enemy\frame - 1
	EndIf

	If player\y < enemy\y
		enemy\y = enemy\y - ENEMYSPEEDCHAOS
		enemy\frame = enemy\frame + 1
	EndIf

	If player\x > enemy\x
		enemy\x = enemy\x + ENEMYSPEEDCHAOS
		enemy\frame = enemy\frame + 1
	EndIf
EndIf

If enemy\frame > 7
	enemy\frame = 0
ElseIf enemy\frame < 0
	enemy\frame = 7
EndIf

; The following will keep the enemy in motion and try to fly around you, and collide into you
enemy\x = enemy\x + enemy\xv
enemy\y = enemy\y + enemy\yv

; Once the enemy reaches the edges of the screen, move him back into range
If enemy\x <= 0
	enemy\x = 0
ElseIf enemy\x >= 800
	enemy\x = 800
EndIf

If enemy\y <= 0
	enemy\y = 0
ElseIf enemy\y >= 600
	enemy\y = 600
EndIf




; Now, draw the images of both you and the enemy
DrawImage enemy\image, enemy\x, enemy\y, enemy\frame
DrawImage player\image, player\x, player\y, player\frame


; Once the left key is pressed, move the player in that direction and decrease the frame 
If KeyDown (203)
	player\x = player\x - PLAYERSPEED
	player\frame = player\frame - 1
EndIf

; Once the right key is pressed, move the player in that direction, and increase the frame
If KeyDown (205)
	player\x = player\x + PLAYERSPEED
	player\frame = player\frame + 1
EndIf

; Once the down key is pressed, move the player in that direction, and decrease the frame
If KeyDown (208)
	player\y = player\y + PLAYERSPEED
	player\frame = player\frame - 1
EndIf

; Once the up key is pressed, move the player in that direction, and increase the frame
If KeyDown (200)
	player\y = player\y - PLAYERSPEED
	player\frame = player\frame + 1
EndIf



; Set the frag limit to 20, and display the final score and game over
If player\hits = 20
	GameOver()
EndIf

If player\collision = 20
	GameOver()
EndIf

If player\frame > 7
	player\frame = 0
ElseIf player\frame < 0
	player\frame = 7
EndIf





; Allow for a brief delay
Delay 100


Flip

Wend
; This officially ends the program loop

Function TestEnemyCollisions()
	; Reverse the enemy's velocity if he hits an edge of the screen
	If enemy\x < 0
		enemy\xv = -enemy\xv
	EndIf
	
	If enemy\x > 800
		enemy\xv = -enemy\xv
	EndIf
	
	If enemy\y < 0
		enemy\yv = -enemy\yv
	EndIf
	
	If enemy\y > 600
		enemy\yv = -enemy\yv
	EndIf

End Function

Function TestKeys()
; Once the left key is pressed, move the player in that direction and decrease the frame 
If KeyDown (203)
	player\x = player\x - PLAYERSPEED
	
EndIf

; Once the right key is pressed, move the player in that direction, and increase the frame
If KeyDown (205)
	player\x = player\x + PLAYERSPEED
	
EndIf

; Once the down key is pressed, move the player in that direction, and decrease the frame
If KeyDown (208)
	player\y = player\y + PLAYERSPEED
	
EndIf

; Once the up key is pressed, move the player in that direction, and increase the frame
If KeyDown (200)
	player\y = player\y - PLAYERSPEED
	
EndIf

End Function

Function UpDateBullets()
	For bullets.bullet = Each bullet
		bullets\y = bullets\y - 10
		bullets\frame = bullets\frame + 1
		If bullets\frame > 7
			bullets\frame = 0
		ElseIf bullets\frame < 0
			bullets\frame = 7
		EndIf
		DrawImage bullets\image,bullets\x,bullets\y,bullets\frame
		If (ImagesCollide(enemy\image,enemy\x,enemy\y,enemy\frame,bullets\image,bullets\x,bullets\y,bullets\frame))
			PlaySound explosionsound
			Cls
				Text 300,300, "ENEMY HIT!!!  Player Scores..."
				player\hits = player\hits + 1
				
				Text 300,312, "Player:\>  " + player\hits
				Text 300,324, "Enemy :\>  " + player\collision
				Flip
				Delay 4000
				; Remove all bullets from the screen
				For bullets.bullet = Each bullet
					Delete bullets
				Next
				
				; Place the positions of the enemy and yourself
				player\x = 400
				player\y = 400
				
				; Load the player image
				player\image = LoadAnimImage("player.bmp",64,64,0,8)
				
				; Now, place the enemy's position
				enemy\x = 400
				enemy\y = 200
				
				; Provide the velocity range for the enemy
					enemy\xv = Rand (-10,10)
					enemy\yv = Rand (-10,10)
				Return
		EndIf
				
		If bullets\y = 0
			Delete bullets
		EndIf
		
	Next
End Function



Function CollisionCrash()
	If (ImagesCollide(player\image,player\x,player\y,player\frame,enemy\image,enemy\x,enemy\y,enemy\frame))
		PlaySound explosionsound
		Cls
			Text 300,300, "CRASH!!!  Enemy Scores..."
			player\collision = player\collision + 1
			
			Text 300,312, "Player:\>  " + player\hits
			Text 300,324, "Enemy :\>  " + player\collision
			Flip
			Delay 4000
			
			; Remove all bullets from the screen
			For bullets.bullet = Each bullet
				Delete bullets
			Next
			
			player\x = 400
			player\y = 400
			player\image = LoadAnimImage("player.bmp",64,64,0,8)
			
			enemy\x = 400
			enemy\y = 200
			enemy\xv = Rand(-10,10)
			enemy\yv = Rand(-10,10)
	EndIf
End Function

Function GameOver()
	Cls
	For bullets.bullet = Each bullet
		Delete bullets
	Next
	
	Text 300,300, "G A M E   O V E R ! ! !"
	Text 300,312, "-----------------------"
	Text 300,324, "Final Score..."
	Text 300,336, "Player:\>  " + player\hits
	Text 300,348, "Enemy :\>  " + player\collision
	Text 0,512, "Press <ESC> to Return to Desktop:  "
	Text 0,524, "Press <TAB> to Start New Round  :  "
	
	If KeyDown (15)
		Cls
		Text 300,300, "Resetting Game, Please Wait..."
		Flip
		Delay 4000
		

		; Now, reset the scores for the hits and collisions
		player\hits = 0
		player\collision = 0
		
		; Reset the images for the player and enemy
		
		player\x = 400
		player\y = 400
		player\image = LoadAnimImage("player.bmp",64,64,0,8)
		
		enemy\x = 400
		enemy\y = 200
		enemy\xv = Rand(-10,10)
		enemy\yv = Rand(-10,10)
	EndIf
	

End Function


This really didn't do as I expected whatsoever, there has to be a better approach than this for doing difficulty settings for the enemy AI.