Hi all,
Having trouble with my attempt at a game slowing down ? even if I just sit and move the ship left and right without firing a shot it suddenly slows right down ?...The other thing I was scratching my head about was the fact that when I put my Setbuffer backbuffer() command at the top of the code the gamescreen flickered terribly...Moved it to the main game loop and its fine ?...Scuse the code in general..its my first attempt at something a bit bigger than "Hello World " !! Any constructive critisism will be very welcome as it will of course help me learn !!
Code as follows:
;Space invaders....First attempt!!
;By Dave Simmonite
;21st Dec 2005
;------------
;Stuff to do-
;------------
;Sort out the alien movement !!...............
;Draw the images..............................DONE
;draw the titlescreen.........................DONE
;Sort out ther types and their fields.........DONE
;Define the correct functions ................ONGOING
;Sort out the Music...........................SOON
;Sort out constants - variables etc...........ADDING AS REQ
;Do a screen for the keymapping...............DONE
;Functions:
;drawtitlescreen().........Draws the titlescreen and play music intro
;drawgamescreen()..........Draws the game
;Checkinput()..............Check for movement and collisions
;update()..................Move the missile upwards
;Drawenemy()...............Draw the aliens
;Movealien()...............Move the aliens accros and down
;Check_Collision().........Check for missile hitting alien
;---------------------------------------------------------------------------------------------------------------|
;------------------------
;Variables and constants-
;------------------------
Const width = 1024 ;Graphics height
Const height = 768 ;Graphics width
Const quit =1 ;Escape key scancode
Const leftkey = 203 ;left arrow key scancode
Const rightkey = 205 ;right arrow key scancode
Const firekey = 57 ;spacebar scancode
Const playerspeed = 7 ;Speed that player moves
Const enemyspeed = 5 ;Speed that alien moves
Const missilespeed = 20 ;Speed of bullet
Global xorigin = 24 ;Origin of first alien x position
Global yorigin = 12 ;Origin of first alien y position
Const score = 0 ;Starting score
Const lives = 3 ;Starting lives
;---------------------------------------------------------------------------------------------------------------|
;--------------
;SOUND EFFECTS-
;--------------
Global clip=LoadSound ("C:\blitzsounds/assholes.wav")
Global gunshot=LoadSound ("C:/blitzsounds/gunshot.wav")
;---------------------------------------------------------------------------------------------------------------|
Graphics width,height,32,1 ; set graphics to 1024 x 768 and run at 32 bit colour in fullscreen mode
;---------------------------------------------------------------------------------------------------------------|
;------
;TYPES-
;------
Type ship
Field xpos
Field ypos
Field hits
Field image
End Type
Type alien
Field x,y
Field hits
End Type
Type bullet
Field x
Field y
End Type
Global player.ship = New ship
player\xpos=512
player\ypos =650
player\hits = 0
player\image = LoadImage("spaceship001.jpg")
;---------------------------------------------------------------------------------------------------------------|
;---------------
;Main game loop-
;---------------
drawtitlescreen() ; call the drawtitlescreen function
SetBuffer BackBuffer() ; Make sure that this is put in the correct place or it screws the game up !!!!
; It causes the gamescreen to flicker ...Dont know why yet ??
While Not KeyHit(quit)
drawgamescreen() ; call the drawgamescreen function
checkInput()
update()
drawenemy()
movealien()
Flip
Wend
;-------------------
;End main game loop-
;-------------------
;---------------------------------------------------------------------------------------------------------------|
;----------
;FUNCTIONS-
;----------
Function drawtitlescreen() ; Define function for the titlescreen
titlescreen = LoadImage("titlescreen.jpg")
DrawImage titlescreen,0,0
PlaySound clip
Delay 5000
Text 80,60,"RIGHT ARROW KEY TO MOVE SHIP RIGHT"
Delay 2000
Text 80,80,"LEFT ARROW KEY TO MOVE SHIP LEFT "
Delay 2000
Text 80,100,"SPACE TO FIRE"
Delay 2000
Text 80,140,"ESCAPE KEY WILL QUIT IF YOU CANT TAKE IT!!!"
Delay 2000
Text 80,180,"PRESS ANY KEY TO CONTINUE....."
WaitKey
End Function
;---------------------------------------------------------------------------------------------------------------|
Function drawgamescreen() ; Draw the starting screen
Cls
DrawImage player\image,player\xpos,player\ypos
Text 50,730,"SCORE..." + score
Text 850,730, "LIVES LEFT..." + lives
End Function
;---------------------------------------------------------------------------------------------------------------|
Function checkinput() ; Check for keys pressed...
If KeyHit(firekey)
PlaySound gunshot
bullet.bullet= New bullet
bullet\x =player\xpos -5
bullet\y = player\ypos-15
EndIf
If KeyDown (rightkey) ;Move ship to the right
player\xpos = player\xpos + playerspeed
If player\xpos > width - 80 ; 80 is the player image width...this stops it going offscreen
player\xpos= width -80
EndIf
EndIf
If KeyDown (leftkey) ; Move ship left
player\xpos = player\xpos - playerspeed
If player\xpos <0
player\xpos=0
EndIf
EndIf
End Function
;---------------------------------------------------------------------------------------------------------------|
Function update() ; This moves the bullet upwards
missile = LoadImage ("c:\blitzpics\missile.jpg")
For bullet.bullet=Each bullet
bullet\y = bullet\y -10 ; -10 is the speed it will travel...this can be altered to suit
If bullet\y < 0
Delete bullet
Else DrawImage missile,bullet\x,bullet\y
EndIf
Next
End Function
Function drawenemy()
enemyship = LoadImage ("C:\blitzpics\alien.jpg")
enemy.alien = New alien
enemy\x = xorigin
enemy\y = yorigin
For numenemy = 0 To 59
DrawImage enemyship,enemy\x,enemy\y
enemy\x = enemy\x + 80
If enemy\x >width - 64
enemy\x = xorigin
enemy\y = enemy\y + 64
EndIf
Next
End Function
;---------------------------------------------------------------------------------------------------------------|
Function movealien() ; Need to sort this out not finished yet !
For enemy.alien = Each alien
enemy\x = enemy\x + 16
If enemy\x >width - 32
enemy\y = enemy\y + 64
enemy\x = enemy\x - 16
EndIf
Next
End Function
;--------------------------------------------------------------------------------------------------------------|
Function check_collision () ;This the next step to work on
End Function
;--------------------------------------------------------------------------------------------------------------|
Having trouble with my attempt at a game slowing down ? even if I just sit and move the ship left and right without firing a shot it suddenly slows right down ?...The other thing I was scratching my head about was the fact that when I put my Setbuffer backbuffer() command at the top of the code the gamescreen flickered terribly...Moved it to the main game loop and its fine ?...Scuse the code in general..its my first attempt at something a bit bigger than "Hello World " !! Any constructive critisism will be very welcome as it will of course help me learn !!
Code as follows:
;Space invaders....First attempt!!
;By Dave Simmonite
;21st Dec 2005
;------------
;Stuff to do-
;------------
;Sort out the alien movement !!...............
;Draw the images..............................DONE
;draw the titlescreen.........................DONE
;Sort out ther types and their fields.........DONE
;Define the correct functions ................ONGOING
;Sort out the Music...........................SOON
;Sort out constants - variables etc...........ADDING AS REQ
;Do a screen for the keymapping...............DONE
;Functions:
;drawtitlescreen().........Draws the titlescreen and play music intro
;drawgamescreen()..........Draws the game
;Checkinput()..............Check for movement and collisions
;update()..................Move the missile upwards
;Drawenemy()...............Draw the aliens
;Movealien()...............Move the aliens accros and down
;Check_Collision().........Check for missile hitting alien
;---------------------------------------------------------------------------------------------------------------|
;------------------------
;Variables and constants-
;------------------------
Const width = 1024 ;Graphics height
Const height = 768 ;Graphics width
Const quit =1 ;Escape key scancode
Const leftkey = 203 ;left arrow key scancode
Const rightkey = 205 ;right arrow key scancode
Const firekey = 57 ;spacebar scancode
Const playerspeed = 7 ;Speed that player moves
Const enemyspeed = 5 ;Speed that alien moves
Const missilespeed = 20 ;Speed of bullet
Global xorigin = 24 ;Origin of first alien x position
Global yorigin = 12 ;Origin of first alien y position
Const score = 0 ;Starting score
Const lives = 3 ;Starting lives
;---------------------------------------------------------------------------------------------------------------|
;--------------
;SOUND EFFECTS-
;--------------
Global clip=LoadSound ("C:\blitzsounds/assholes.wav")
Global gunshot=LoadSound ("C:/blitzsounds/gunshot.wav")
;---------------------------------------------------------------------------------------------------------------|
Graphics width,height,32,1 ; set graphics to 1024 x 768 and run at 32 bit colour in fullscreen mode
;---------------------------------------------------------------------------------------------------------------|
;------
;TYPES-
;------
Type ship
Field xpos
Field ypos
Field hits
Field image
End Type
Type alien
Field x,y
Field hits
End Type
Type bullet
Field x
Field y
End Type
Global player.ship = New ship
player\xpos=512
player\ypos =650
player\hits = 0
player\image = LoadImage("spaceship001.jpg")
;---------------------------------------------------------------------------------------------------------------|
;---------------
;Main game loop-
;---------------
drawtitlescreen() ; call the drawtitlescreen function
SetBuffer BackBuffer() ; Make sure that this is put in the correct place or it screws the game up !!!!
; It causes the gamescreen to flicker ...Dont know why yet ??
While Not KeyHit(quit)
drawgamescreen() ; call the drawgamescreen function
checkInput()
update()
drawenemy()
movealien()
Flip
Wend
;-------------------
;End main game loop-
;-------------------
;---------------------------------------------------------------------------------------------------------------|
;----------
;FUNCTIONS-
;----------
Function drawtitlescreen() ; Define function for the titlescreen
titlescreen = LoadImage("titlescreen.jpg")
DrawImage titlescreen,0,0
PlaySound clip
Delay 5000
Text 80,60,"RIGHT ARROW KEY TO MOVE SHIP RIGHT"
Delay 2000
Text 80,80,"LEFT ARROW KEY TO MOVE SHIP LEFT "
Delay 2000
Text 80,100,"SPACE TO FIRE"
Delay 2000
Text 80,140,"ESCAPE KEY WILL QUIT IF YOU CANT TAKE IT!!!"
Delay 2000
Text 80,180,"PRESS ANY KEY TO CONTINUE....."
WaitKey
End Function
;---------------------------------------------------------------------------------------------------------------|
Function drawgamescreen() ; Draw the starting screen
Cls
DrawImage player\image,player\xpos,player\ypos
Text 50,730,"SCORE..." + score
Text 850,730, "LIVES LEFT..." + lives
End Function
;---------------------------------------------------------------------------------------------------------------|
Function checkinput() ; Check for keys pressed...
If KeyHit(firekey)
PlaySound gunshot
bullet.bullet= New bullet
bullet\x =player\xpos -5
bullet\y = player\ypos-15
EndIf
If KeyDown (rightkey) ;Move ship to the right
player\xpos = player\xpos + playerspeed
If player\xpos > width - 80 ; 80 is the player image width...this stops it going offscreen
player\xpos= width -80
EndIf
EndIf
If KeyDown (leftkey) ; Move ship left
player\xpos = player\xpos - playerspeed
If player\xpos <0
player\xpos=0
EndIf
EndIf
End Function
;---------------------------------------------------------------------------------------------------------------|
Function update() ; This moves the bullet upwards
missile = LoadImage ("c:\blitzpics\missile.jpg")
For bullet.bullet=Each bullet
bullet\y = bullet\y -10 ; -10 is the speed it will travel...this can be altered to suit
If bullet\y < 0
Delete bullet
Else DrawImage missile,bullet\x,bullet\y
EndIf
Next
End Function
Function drawenemy()
enemyship = LoadImage ("C:\blitzpics\alien.jpg")
enemy.alien = New alien
enemy\x = xorigin
enemy\y = yorigin
For numenemy = 0 To 59
DrawImage enemyship,enemy\x,enemy\y
enemy\x = enemy\x + 80
If enemy\x >width - 64
enemy\x = xorigin
enemy\y = enemy\y + 64
EndIf
Next
End Function
;---------------------------------------------------------------------------------------------------------------|
Function movealien() ; Need to sort this out not finished yet !
For enemy.alien = Each alien
enemy\x = enemy\x + 16
If enemy\x >width - 32
enemy\y = enemy\y + 64
enemy\x = enemy\x - 16
EndIf
Next
End Function
;--------------------------------------------------------------------------------------------------------------|
Function check_collision () ;This the next step to work on
End Function
;--------------------------------------------------------------------------------------------------------------|