Xboxerdude
Member
- Joined
- Jul 6, 2008
- Sythe Gold
- 0
- Messages
- 41
;Demonstrates ImagesCollide, Explosion, Bullet and Bullet Collide
Graphics 800,600 ;,16,1
;Set up backbuffer, random number generator, and automidhandle
SetBuffer BackBuffer()
SeedRnd MilliSecs()
AutoMidHandle True
;Constants
;The key code constants
Const UPKEY = 200, DOWNKEY = 208, LEFTKEY = 203, RIGHTKEY = 205, SPACEBAR = 57
;How many pixels the ship should move per frame
Const MOVEX = 8
Const MOVEY = 8
;Background
;The close and quickly scrolled background
Global backgroundimageCLOSE = LoadImage("stars.bmp")
;The further and slowly scrolled background
Global backgroundimageFAR = LoadImage("starsfarther.bmp")
;Create scrolling tracker variable for background
Global scrolly = 0
;Types
;The enemy ship is a randomly moving object on the screen
Type enemyship
Field x,y
Field xv,yv ;the velocity
Field image ;the image
End Type
;The playership type defines the player
Type playership
Field x,y ;The x and y coordinate position
Field collision ;How many collisions have occured
Field frame ;Frames in the image
Field image ;The player's image
Field hits
End Type
;The explosion type
Type explosion
Field frame ;Frames of the explosion
Field image ;Explosion image
End Type
;The bullet type
Type bullet
Field x,y ;The x and y coordinates
Field image ;Bullet image
End Type
;Create the enemy and assign it default variables
Global enemy.enemyship = New enemyship
enemy\x = 400
enemy\y = 200
enemy\xv = Rand(-25,25)
enemy\yv = Rand(-25,25)
enemy\image = LoadImage("player.bmp")
;Create player and assign it default variables
Global player.playership = New playership
player\x = 400
player\y = 400
player\collision = 0
player\hits = 0
player\frame = 0
player\image = LoadAnimImage("playerv2.bmp",68,64,0,13)
;Create the explosion frames
Global ex.explosion = New explosion
ex\image = LoadAnimImage("enemyexplosion.bmp",64,64,0,8)
ex\frame = 0
;Create the bullet image before the first instance of the bullet
Global bullet_image = LoadImage("bullet.bmp")
;Main Loop
While Not KeyDown(1)
;Clear the screen
Cls
ShowHUD() ;Heads up display
ParallaxBackground() ;Scrolling background
;Find out if it hit a wall
TestEnemyOffWall() ;Have the player-enemy collided
;Test keyboard
TestKeys() ;What key has been hit
Updatebullet() ;Check Bullets
;If player and enemy collide, increment collisions
;Show explosion and reset player and enemy
If (ImagesCollide(player\image,player\x,player\y,player\frame,enemy\image,enemy\x,enemy\y,0))
player\Collision = player\Collision + 1
ExplodeShip() ;Do explosion
player\x = 400 ;Reset players
player\y = 400
player\frame = 0 ;Frame back to zero
enemy\x = 400 ;Reset players
enemy\y = 200 ;Reset players
EndIf
;Move the enemy
enemy\x = enemy\x + enemy\xv
enemy\y = enemy\y + enemy\yv
;Draw the player and the enemy
DrawImage enemy\image,enemy\x,enemy\y
DrawImage player\image,player\x,player\y,player\frame
;Slow it down
Delay 20
Flip
Wend
;End of main loop
End
;Functions
;Function TestEnemyOffWall() - Finds out if enemy hit a wall and does something about it
Function TestEnemyOffWall()
;If enemy goes too far to the left or right, flip its x velocity
If enemy\x < 0
enemy\xv = -enemy\xv
EndIf
If enemy\x > 800
enemy\xv = -enemy\xv
EndIf
;If enemy goes too high or too low, flip its y velocity
If enemy\y < 0
enemy\yv = -enemy\yv
EndIf
If enemy\y > 600
enemy\yv = -enemy\yv
EndIf
End Function
;End of TestEnemyOffWall
;Function TestKeys() - Tests all of the keys to see if they were hit
Function TestKeys()
;If up key is hit, move player up
If KeyDown(UPKEY)
player\y = player\y - MOVEY
player\frame = player\frame + 1
If player\y <= 0
player\y = 600
EndIf
EndIf
;If down key is hit, move player down
If KeyDown(DOWNKEY)
player\y = player\y + MOVEY
player\frame = player\frame - 1
If player\y >= 0
player\y = 600
EndIf
EndIf
;If left key is hit, move player left
If KeyDown(LEFTKEY)
player\x = player\x - MOVEX
player\frame = player\frame - 1
If player\x <= 0
player\x = 800
EndIf
EndIf
;If right key is hit, move player right
If KeyDown(RIGHTKEY)
player\x = player\x + MOVEX
player\frame = player\frame + 1
If player\x >= 800
player\x = 0
EndIf
EndIf
;If spacebar is hit, shoot bullet
If KeyDown(SPACEBAR)
nbullet.bullet = New bullet
nbullet\x = player\x
nbullet\y = player\y
nbullet\image = CopyImage(bullet_image)
;DrawImage nbullet\image, nbullet\x, nbullet\y
;WaitKey
EndIf
;If the frame gets too high, reset it back to zero
If player\frame > 12
player\frame = 0
;If the frame gets too low, reset it to 13
ElseIf player\frame < 0
player\frame = 12
EndIf
End Function
;End TestKeys()
;Function to parallax
Function ParallaxBackground()
;Tile both backgrounds at proper speed
TileImage backgroundimageFAR,0,scrolly
TileImage backgroundimageCLOSE,0,scrolly*2
DrawImage player\image,player\x,player\y,player\frame
;Increment scrolly
scrolly = scrolly + 1
;Reset tracker variable if it gets too large
If scrolly >= ImageHeight(backgroundimageCLOSE)
scrolly = 0
EndIf
End Function
;Function to run the explosion
Function ExplodeShip()
ex\frame = 0 ;Set to zero initially
While ex\frame <= 7 ;Until it gets to end of the sprite
Cls ;Clear screen to show explosion better
TileImage backgroundimageFAR,0,0 ;Redo Background
TileImage backgroundimageCLOSE,0,0
DrawImage ex\image,enemy\x,enemy\y,ex\frame ;Draw explosion
Delay 100
ex\frame = ex\frame + 1
TileImage backgroundimageFAR,0,0
TileImage backgroundimageCLOSE,0,0
Flip
Wend
End Function
;Function to show HUD
Function ShowHUD()
;Make sure all text appears in top left corner
;Position text at the top left corner of the screen
Text 0,0, "Player X Coordinate: " + player\x
Text 0,12, "Player Y Coordinate: " + player\y
Text 0,24, "Enemy X Coordinate: " + enemy\x
Text 0,36, "Enemy Y Coordinate: " + enemy\y
Text 0,48, "Collisions: " + player\collision
Text 0,60, "Hits: " + player\hits
End Function
;Function to draw the bullet and keep track of movement
Function UpdateBullet()
For nbullet.bullet = Each bullet
nbullet\y = nbullet\y - 20 ;Move it up 20 pixels
If nbullet\y <= 0 ;if it is off the scren
Delete nbullet ;Delete it
Return ;Leave the function
;Explain Return
Else
DrawImage nbullet\image, nbullet\x, nbullet\y
EndIf
;Bullet and ship collision
Next ;Next bullet
End Function
;End Program
;End functions
Graphics 800,600 ;,16,1
;Set up backbuffer, random number generator, and automidhandle
SetBuffer BackBuffer()
SeedRnd MilliSecs()
AutoMidHandle True
;Constants
;The key code constants
Const UPKEY = 200, DOWNKEY = 208, LEFTKEY = 203, RIGHTKEY = 205, SPACEBAR = 57
;How many pixels the ship should move per frame
Const MOVEX = 8
Const MOVEY = 8
;Background
;The close and quickly scrolled background
Global backgroundimageCLOSE = LoadImage("stars.bmp")
;The further and slowly scrolled background
Global backgroundimageFAR = LoadImage("starsfarther.bmp")
;Create scrolling tracker variable for background
Global scrolly = 0
;Types
;The enemy ship is a randomly moving object on the screen
Type enemyship
Field x,y
Field xv,yv ;the velocity
Field image ;the image
End Type
;The playership type defines the player
Type playership
Field x,y ;The x and y coordinate position
Field collision ;How many collisions have occured
Field frame ;Frames in the image
Field image ;The player's image
Field hits
End Type
;The explosion type
Type explosion
Field frame ;Frames of the explosion
Field image ;Explosion image
End Type
;The bullet type
Type bullet
Field x,y ;The x and y coordinates
Field image ;Bullet image
End Type
;Create the enemy and assign it default variables
Global enemy.enemyship = New enemyship
enemy\x = 400
enemy\y = 200
enemy\xv = Rand(-25,25)
enemy\yv = Rand(-25,25)
enemy\image = LoadImage("player.bmp")
;Create player and assign it default variables
Global player.playership = New playership
player\x = 400
player\y = 400
player\collision = 0
player\hits = 0
player\frame = 0
player\image = LoadAnimImage("playerv2.bmp",68,64,0,13)
;Create the explosion frames
Global ex.explosion = New explosion
ex\image = LoadAnimImage("enemyexplosion.bmp",64,64,0,8)
ex\frame = 0
;Create the bullet image before the first instance of the bullet
Global bullet_image = LoadImage("bullet.bmp")
;Main Loop
While Not KeyDown(1)
;Clear the screen
Cls
ShowHUD() ;Heads up display
ParallaxBackground() ;Scrolling background
;Find out if it hit a wall
TestEnemyOffWall() ;Have the player-enemy collided
;Test keyboard
TestKeys() ;What key has been hit
Updatebullet() ;Check Bullets
;If player and enemy collide, increment collisions
;Show explosion and reset player and enemy
If (ImagesCollide(player\image,player\x,player\y,player\frame,enemy\image,enemy\x,enemy\y,0))
player\Collision = player\Collision + 1
ExplodeShip() ;Do explosion
player\x = 400 ;Reset players
player\y = 400
player\frame = 0 ;Frame back to zero
enemy\x = 400 ;Reset players
enemy\y = 200 ;Reset players
EndIf
;Move the enemy
enemy\x = enemy\x + enemy\xv
enemy\y = enemy\y + enemy\yv
;Draw the player and the enemy
DrawImage enemy\image,enemy\x,enemy\y
DrawImage player\image,player\x,player\y,player\frame
;Slow it down
Delay 20
Flip
Wend
;End of main loop
End
;Functions
;Function TestEnemyOffWall() - Finds out if enemy hit a wall and does something about it
Function TestEnemyOffWall()
;If enemy goes too far to the left or right, flip its x velocity
If enemy\x < 0
enemy\xv = -enemy\xv
EndIf
If enemy\x > 800
enemy\xv = -enemy\xv
EndIf
;If enemy goes too high or too low, flip its y velocity
If enemy\y < 0
enemy\yv = -enemy\yv
EndIf
If enemy\y > 600
enemy\yv = -enemy\yv
EndIf
End Function
;End of TestEnemyOffWall
;Function TestKeys() - Tests all of the keys to see if they were hit
Function TestKeys()
;If up key is hit, move player up
If KeyDown(UPKEY)
player\y = player\y - MOVEY
player\frame = player\frame + 1
If player\y <= 0
player\y = 600
EndIf
EndIf
;If down key is hit, move player down
If KeyDown(DOWNKEY)
player\y = player\y + MOVEY
player\frame = player\frame - 1
If player\y >= 0
player\y = 600
EndIf
EndIf
;If left key is hit, move player left
If KeyDown(LEFTKEY)
player\x = player\x - MOVEX
player\frame = player\frame - 1
If player\x <= 0
player\x = 800
EndIf
EndIf
;If right key is hit, move player right
If KeyDown(RIGHTKEY)
player\x = player\x + MOVEX
player\frame = player\frame + 1
If player\x >= 800
player\x = 0
EndIf
EndIf
;If spacebar is hit, shoot bullet
If KeyDown(SPACEBAR)
nbullet.bullet = New bullet
nbullet\x = player\x
nbullet\y = player\y
nbullet\image = CopyImage(bullet_image)
;DrawImage nbullet\image, nbullet\x, nbullet\y
;WaitKey
EndIf
;If the frame gets too high, reset it back to zero
If player\frame > 12
player\frame = 0
;If the frame gets too low, reset it to 13
ElseIf player\frame < 0
player\frame = 12
EndIf
End Function
;End TestKeys()
;Function to parallax
Function ParallaxBackground()
;Tile both backgrounds at proper speed
TileImage backgroundimageFAR,0,scrolly
TileImage backgroundimageCLOSE,0,scrolly*2
DrawImage player\image,player\x,player\y,player\frame
;Increment scrolly
scrolly = scrolly + 1
;Reset tracker variable if it gets too large
If scrolly >= ImageHeight(backgroundimageCLOSE)
scrolly = 0
EndIf
End Function
;Function to run the explosion
Function ExplodeShip()
ex\frame = 0 ;Set to zero initially
While ex\frame <= 7 ;Until it gets to end of the sprite
Cls ;Clear screen to show explosion better
TileImage backgroundimageFAR,0,0 ;Redo Background
TileImage backgroundimageCLOSE,0,0
DrawImage ex\image,enemy\x,enemy\y,ex\frame ;Draw explosion
Delay 100
ex\frame = ex\frame + 1
TileImage backgroundimageFAR,0,0
TileImage backgroundimageCLOSE,0,0
Flip
Wend
End Function
;Function to show HUD
Function ShowHUD()
;Make sure all text appears in top left corner
;Position text at the top left corner of the screen
Text 0,0, "Player X Coordinate: " + player\x
Text 0,12, "Player Y Coordinate: " + player\y
Text 0,24, "Enemy X Coordinate: " + enemy\x
Text 0,36, "Enemy Y Coordinate: " + enemy\y
Text 0,48, "Collisions: " + player\collision
Text 0,60, "Hits: " + player\hits
End Function
;Function to draw the bullet and keep track of movement
Function UpdateBullet()
For nbullet.bullet = Each bullet
nbullet\y = nbullet\y - 20 ;Move it up 20 pixels
If nbullet\y <= 0 ;if it is off the scren
Delete nbullet ;Delete it
Return ;Leave the function
;Explain Return
Else
DrawImage nbullet\image, nbullet\x, nbullet\y
EndIf
;Bullet and ship collision
Next ;Next bullet
End Function
;End Program
;End functions















