Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; Moving a Graphic
- ; Aiden Kerr
- ; Set up graphics stuff
- Const SCREENWIDTH = 1280, SCREENHEIGHT = 720
- Graphics SCREENWIDTH, SCREENHEIGHT
- SetBuffer BackBuffer()
- ; Constants
- Const ESCKEY = 1, SPACEBAR = 57, LEFTKEY = 203, RIGHTKEY = 205, UPKEY = 200, DOWNKEY = 208
- ; Types
- Type PlayerType
- Field x, y
- Field Speed
- Field Graphic
- End Type
- Type BulletType
- Field x, y
- Field Speed
- Field Graphic
- Field Alive
- End Type
- Type EnemyType
- Field x,y
- Field Graphic
- Field XSpeed
- Field YSpeed
- Field Alive
- Field Frame
- End Type
- Type ExplosionType
- Field x, y
- Field Alive
- Field Graphic
- Field Frame
- End Type
- ; Setup
- SeedRnd MilliSecs()
- Global LaserSound = LoadSound("laser.wav")
- Global clock = 0
- Global Player.PlayerType = New PlayerType
- SetupPlayer()
- SetupEnemies(4)
- ; Main Loop
- While Not KeyDown(ESCKEY)
- Cls
- PlayerControls()
- MoveBullet()
- MoveEnemies()
- CheckCollisions()
- EraseDead()
- DrawAll()
- DrawExplosion()
- Time()
- Flip
- Wend
- ; Functions
- Function SetupPlayer()
- Player\Graphic = LoadImage("player.bmp")
- Player\x = SCREENWIDTH/2
- Player\y = SCREENHEIGHT/2
- Player\Speed = 1.5
- End Function
- Function PlayerControls()
- ; player movement
- If KeyDown(UPKEY) Then Player\y = Player\y - Player\Speed
- If KeyDown(DOWNKEY) Then Player\y = Player\y + Player\Speed
- If KeyDown(LEFTKEY) Then Player\x = Player\x - Player\Speed
- If KeyDown(RIGHTKEY) Then Player\x = Player\x + Player\Speed
- ; Restraining the player locations to the screen.
- If Player\y < 0 Then Player\y = 0
- If Player\y > SCREENHEIGHT-33 Then Player\y = SCREENHEIGHT-33
- If Player\x < -1 Then Player\x = -1
- If Player\x > SCREENWIDTH-33 Then Player\x = SCREENWIDTH-33
- ; Other controls
- ; Shooting
- If KeyHit(SPACEBAR)
- NewBullet.BulletType = New BulletType
- NewBullet\x = Player\x
- NewBullet\y = player\y
- NewBullet\Speed = 5
- NewBullet\Graphic = LoadImage("PlayerShot.bmp")
- NewBullet\Alive = True
- PlaySound(LaserSound)
- EndIf
- End Function
- Function MoveBullet()
- For Bcnt.BulletType = Each BulletType
- Bcnt\y = Bcnt\y - Bcnt\Speed
- ; Delete when off-screen
- If Bcnt\y < -7 Then Delete Bcnt
- Next
- End Function
- ; Creates enemies and sets values
- Function SetupEnemies(num)
- For i = 1 To num
- Enemy.EnemyType = New EnemyType
- Enemy\x = Rand(0, SCREENWIDTH-80)
- Enemy\y = Rand(0, SCREENHEIGHT-80)
- Enemy\Graphic = LoadAnimImage("Enemy.bmp",64,64,0,2)
- Enemy\XSpeed = Rand(-1, 1)
- Enemy\YSpeed = Rand(-1, 1)
- Enemy\Alive = True
- Enemy\Frame = 2 ; so it starts with frame 0
- Next
- End Function
- ; Handles enemy movement and edge detection
- Function MoveEnemies()
- For Ecnt.EnemyType = Each EnemyType
- ; Flip their directions if they're near the edge
- If Ecnt\y < -10 Or Ecnt\y > SCREENHEIGHT-55 Then Ecnt\YSpeed = Ecnt\YSpeed * -1
- If Ecnt\x < -10 Or Ecnt\x > SCREENWIDTH-55 Then Ecnt\XSpeed = Ecnt\XSpeed * -1
- ; Move them by adding their speed to their location
- Ecnt\x = Ecnt\x + Ecnt\XSPeed
- Ecnt\y = Ecnt\y + Ecnt\YSpeed
- ; Animation
- If Clock Mod 100 = 0 Then
- Ecnt\Frame = Ecnt\Frame + 1
- If Ecnt\Frame > 1 Then Ecnt\Frame = 0
- EndIf
- Next
- End Function
- Function CheckCollisions()
- ; Loop through bullets and enemies to
- For Bcnt.BulletType = Each BulletType
- For Ecnt.EnemyType = Each EnemyType
- If ImagesCollide(Bcnt\Graphic, Bcnt\x, Bcnt\y, 0, Ecnt\Graphic, Ecnt\x, Ecnt\y, 0) Then
- Ecnt\Alive = False
- Bcnt\Alive = False
- CreateExplosion(Ecnt\x, Ecnt\y)
- EndIf
- Next
- Next
- End Function
- Function EraseDead()
- ; Check if each enemy should be dead. Also, explosion animations
- For Bcnt.BulletType = Each BulletType
- If Bcnt\Alive = False Then Delete Bcnt
- Next
- For Ecnt.EnemyType = Each EnemyType
- If Ecnt\Alive = False Then Delete Ecnt
- Next
- For ExpCnt.ExplosionType = Each ExplosionType
- If ExpCnt\Alive = False Then Delete ExpCnt
- Next
- End Function
- Function DrawAll()
- ; Draw player
- DrawImage Player\Graphic, Player\x, Player\y
- ; Draw Bullets
- For Bcnt.BulletType = Each BulletType
- DrawImage Bcnt\Graphic, Bcnt\x, Bcnt\y
- Next
- ;Draw Enemies
- For Ecnt.EnemyType = Each EnemyType
- DrawImage Ecnt\Graphic, Ecnt\x, Ecnt\y, Ecnt\Frame
- Next
- End Function
- ; Creates the explosion type when required
- Function CreateExplosion(x, y)
- Explosion.ExplosionType = New ExplosionType
- Explosion\x = x
- Explosion\y = y
- Explosion\Alive = True
- Explosion\Graphic = LoadAnimImage("EnemyKaboom.bmp",64,64,0,5)
- End Function
- ; Animates the explosions
- Function DrawExplosion()
- For ExpCnt.ExplosionType = Each ExplosionType
- If Clock Mod 40 = 0 Then
- ExpCnt\Frame = ExpCnt\Frame + 1
- If ExpCnt\Frame > 4 Then
- ExpCnt\alive = False
- ExpCnt\Frame = 0
- EndIf
- EndIf
- DrawImage ExpCnt\Graphic, ExpCnt\x, ExpCnt\y, ExpCnt\Frame
- Next
- End Function
- ;Increments the clock every frame
- Function Time()
- Clock = Clock + 1
- End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement