help with spaceinvader programming

BlitzMax Forums/BlitzMax Tutorials/help with spaceinvader programming

Sanjit(Posted 2015) [#1]
I programmed this so far:

'Keep good coding practice
SuperStrict

'Set application and Graphics
AppTitle="Space Invaderz"
Graphics 800,600

'*****Variables****
Global entityList:TList = CreateList()

'Load and create background
Local Background:TBackground = TBackground.Create("/Users/sanjitsingh/Downloads/Data/ScreenshotStarfield.png",0,0,1.0)
Local Background2:TBackground = TBackground.Create("/Users/sanjitsingh/Downloads/Data/ScreenshotStarfield.png",0,-600,1.0)
'Load and create our player
Local player:TPlayer = TPlayer.Create("/Users/sanjitsingh/Downloads/Data/9.png",400,300)
'Load and create our enemy
Local enemy:TEnemy = TEnemy.Create("/Users/sanjitsingh/Downloads/Data/Enemy.png",400,0)



'Main Loop
Repeat
Cls 'Clear the screen
For Local a:TEntityObject = EachIn entityList
a.DrawState()
a.UpdateState()

Next

Flip'flip our buffers




Until AppTerminate() Or KeyHit(KEY_ESCAPE)

'*****Types*****
'Create a generic Type
Type TEntityObject
Field X:Int
Field Y:Int
Field Speed:Int = 5
Field Image:TImage

'Draw Entity
Method DrawState()
DrawImage Image,X,Y

'Let empty for now

EndMethod

'Update our player
Method UpdateState() Abstract


EndType


'Create a type to hold our player
Type TPlayer Extends TEntityObject

'Create a function to load our player
Function Create:TPlayer(File:String,xStart:Int, yStart:Int)
Local player:TPlayer = New TPlayer
player.X = xStart
player.Y = yStart
player.Image = LoadImage(File)

'Check to see if image is loaded
If player.Image = Null
Print "9.png not found"
End 'Exit program
EndIf


'Add player to list
ListAddLast entityList, player

Return player
EndFunction



Method UpdateState()
'Move our player up
If KeyDown(KEY_UP)
Y:-Speed
ElseIf KeyDown(Key_W)
Y:-Speed
EndIf
'Move our player down
If KeyDown(KEY_DOWN)

Y:+Speed
ElseIf KeyDown(KEY_S)
Y:+Speed
EndIf
'Move our player left

If KeyDown(KEY_LEFT)
X:-Speed
ElseIf KeyDown(KEY_A)
X:-Speed
EndIf
'Move our player RIGHT
If KeyDown(KEY_RIGHT)
X:+Speed
ElseIf KeyDown(KEY_D)
X:+Speed
EndIf




'Check for screen boundries
If X<0 Then X=0
If X>736 Then x=736
If Y<0 Then Y=0
If Y>536 Then Y=536
EndMethod
EndType

'create our enemy
Type TEnemy Extends TEntityObject

Field Speed:Int = 3
'Create our enemy
Function Create: TEnemy(File:String, xStart:Int , yStart:Int)
Local enemy:TEnemy = New TEnemy
enemy.X = xStart
enemy.Y = yStart
enemy.Image = LoadImage(File)

'Check to see if enemy exists

If enemy.Image = Null
Print "mothership.gif not found"
End 'Exit program
EndIf
'Add Enemy to list
ListAddLast entityList, Enemy


Return enemy

EndFunction



Method UpdateState()
If Y > 634 Then Y =-64
Y:+Speed

EndMethod


EndType

'TBackground
Type TBackground Extends TEntityObject
Field yScroll:Float

'Create our background
Function Create:TBackground(File:String, xStart:Int, yStart:Int, yScroll:Float)
Local background:TBackground = New Tbackground
background.X = xStart
Background.Y = yStart
background.yScroll = yScroll
background.image = LoadImage(File)

'Check our background

If background.Image = Null
Print "Background.png not found"
End 'Exit Program

EndIf
ListAddLast entityList, background

Return background

EndFunction

Method UpdateState()
If Y > 600
Y = -599
EndIf
Y:+yScroll

EndMethod
EndType

now i want to program a missile button but don't know the code to do it or where to find the tutorial for it. Can someone tell me how to program a missile to kill the enemy for this game. thanks


Midimaster(Posted 2015) [#2]
A missile is (nearly) the same as a player entity... You need additional fields like addX and addY for moving the missile.

In the moment of creation the starting coordinates are the player X, and y value. The direction depends on your wishes.

f.e.
If the missile only shoots upwards the addX is 0 and the addY is -1

f.e.
If the missile should have a direction the addX=Sin(Dir) and the addY=Cos(dir)

My suggestion for beginners is always is to simplify the game. At first write the code of your missile without Collision check. Only launch and flight...

In a second step you can extent the game.

A movement that depends on directions is always made by four variables DIR%, SPEED#, addX#, addY#. DIR runs from 0 to 360. SPEED from 0 to SPEEDMAX (f.e. 1). With this and SIN() you calculate the addX and addY. This values are added to the Spaceship, Player, Missile, etc...

SuperStrict
Graphics 800,600
Const SPEED_MAX#=1
Global X#=400, Y#=300, addX#, addY#, Dir%, Speed#

Speed=1
Repeat
	Cls
	DrawText "Press L for Left and R for Right turn",100,100 
	If KeyDown(KEY_R)
		Dir=Dir+1
	ElseIf KeyDown(KEY_L)
		Dir=Dir-1
	EndIf
	Dir=(Dir+360) Mod 360
	addX=Cos(Dir)*Speed
	addY=Sin(Dir)*Speed
	X=X+addX
	Y=Y+addY
	DrawRect X,Y,5,5
		
	Flip(1)
Until KeyHit(KEY_ESCAPE)




Sanjit(Posted 2015) [#3]
'Keep good coding practice
SuperStrict

'Set application and Graphics
AppTitle="Space Invaderz"
Graphics 800,600
Const SPEED_MAX#=1
Global X#=400, Y#=300, addX#, addY#, Dir%, Speed#

Speed=1




'*****Variables****
Global entityList:TList = CreateList()

'Load and create background
Local Background:TBackground = TBackground.Create("/Users/sanjitsingh/Downloads/Data/ScreenshotStarfield.png",0,0,1.0)
Local Background2:TBackground = TBackground.Create("/Users/sanjitsingh/Downloads/Data/ScreenshotStarfield.png",0,-600,1.0)
'Load and create our player
Local player:TPlayer = TPlayer.Create("/Users/sanjitsingh/Downloads/Data/9.png",400,300)
'Load and create our enemy
Local enemy:TEnemy = TEnemy.Create("/Users/sanjitsingh/Downloads/Data/Enemy.png",400,0)



'Main Loop
Repeat
Cls 'Clear the screen
For Local a:TEntityObject = EachIn entityList
a.DrawState()
a.UpdateState()


Dir=(Dir+360) Mod 360
addX=0
addY=-1
X=X+addX
Y=Y+addY
DrawRect X,Y,5,5








Next

Flip 'flip our buffers




Until AppTerminate() Or KeyHit(KEY_ESCAPE)

'*****Types*****
'Create a generic Type
Type TEntityObject
Field X:Int
Field Y:Int
Field Speed:Int = 5
Field Image:TImage

'Draw Entity
Method DrawState()
DrawImage Image,X,Y

'Let empty for now

EndMethod

'Update our player
Method UpdateState() Abstract


EndType


'Create a type to hold our player
Type TPlayer Extends TEntityObject

'Create a function to load our player
Function Create:TPlayer(File:String,xStart:Int, yStart:Int)
Local player:TPlayer = New TPlayer
player.X = xStart
player.Y = yStart
player.Image = LoadImage(File)

'Check to see if image is loaded
If player.Image = Null
Print "9.png not found"
End 'Exit program
EndIf


'Add player to list
ListAddLast entityList, player

Return player
EndFunction



Method UpdateState()
'Move our player up
If KeyDown(KEY_UP)
Y:-Speed
ElseIf KeyDown(Key_W)
Y:-Speed
EndIf
'Move our player down
If KeyDown(KEY_DOWN)

Y:+Speed
ElseIf KeyDown(KEY_S)
Y:+Speed
EndIf
'Move our player left

If KeyDown(KEY_LEFT)
X:-Speed
ElseIf KeyDown(KEY_A)
X:-Speed
EndIf
'Move our player RIGHT
If KeyDown(KEY_RIGHT)
X:+Speed
ElseIf KeyDown(KEY_D)
X:+Speed
EndIf




'Check for screen boundries
If X<0 Then X=0
If X>736 Then x=736
If Y<0 Then Y=0
If Y>536 Then Y=536



EndMethod
EndType

'create our enemy
Type TEnemy Extends TEntityObject

Field Speed:Int = 3
'Create our enemy
Function Create: TEnemy(File:String, xStart:Int , yStart:Int)
Local enemy:TEnemy = New TEnemy
enemy.X = xStart
enemy.Y = yStart
enemy.Image = LoadImage(File)

'Check to see if enemy exists

If enemy.Image = Null
Print "mothership.gif not found"
End 'Exit program
EndIf
'Add Enemy to list
ListAddLast entityList, Enemy


Return enemy

EndFunction



Method UpdateState()
If Y > 634 Then Y =-64
Y:+Speed

EndMethod


EndType

'TBackground
Type TBackground Extends TEntityObject
Field yScroll:Float

'Create our background
Function Create:TBackground(File:String, xStart:Int, yStart:Int, yScroll:Float)
Local background:TBackground = New Tbackground
background.X = xStart
Background.Y = yStart
background.yScroll = yScroll
background.image = LoadImage(File)

'Check our background

If background.Image = Null
Print "Background.png not found"
End 'Exit Program

EndIf
ListAddLast entityList, background

Return background

EndFunction

Method UpdateState()
If Y > 600
Y = -599
EndIf
Y:+yScroll

EndMethod
EndType
I have the code with a shoot button but now its auto shooting. how do i have it programmed so it only shoots when I press the button to. I want to have the spacebar programmed for pressed to be able to shoot. plus i also want the enemy destroyed when I shoot it. how do i program that. can you help me


Midimaster(Posted 2015) [#4]
My code was only a sample without objects... Transfered to your code you would write a "missile type"

The missile object will be created when pressing the SPACE bar and die when leaving the screen.

SuperStrict
Graphics 800,600


Const SPEED_MAX#=5



Global entityList:TList = CreateList()
Local player:TPlayer = TPlayer.Create("/Users/sanjitsingh/Downloads/Data/9.png",400,300)



Repeat
	Cls
	For Local a:TEntityObject = EachIn entityList
		a.DrawState()
		a.UpdateState()
	Next
	Flip
Until AppTerminate() Or KeyHit(KEY_ESCAPE)




Type TEntityObject
	Field X:Int
	Field Y:Int
	Field Speed:Int = 5
	Field Image:TImage

	
	Method DrawState()
		DrawRect X,y,5,5
	EndMethod
	
	Method UpdateState() Abstract	

EndType




Type TPlayer Extends TEntityObject
	
	Field LaunchTimer%

	Function Create:TPlayer(File:String,xStart:Int, yStart:Int)
		Local player:TPlayer = New TPlayer
		player.X = xStart
		player.Y = yStart
		ListAddLast entityList, player
		Return player
	EndFunction
	
	
	
	Method UpdateState()
		If KeyDown(KEY_LEFT)
			X:-Speed
		ElseIf KeyDown(KEY_RIGHT)
			X:+Speed
		EndIf
		If KeyDown(KEY_SPACE)
			If LaunchTimer<MilliSecs()
				LaunchTimer=MilliSecs()+100
				Local missile:TMissile = TMissile.Create("",X,Y)
			EndIf
		EndIf
		

		If X<0 Then X=0
		If X>736 Then x=736
		If Y<0 Then Y=0
		If Y>536 Then Y=536
	EndMethod

EndType



Type TMissile Extends TEntityObject

	Function Create:TMissile(File:String,xStart:Int, yStart:Int)
		Local missile:TMissile = New TMissile
		missile.X = xStart
		missile.Y = yStart
		ListAddLast entityList, missile
		Return missile
	EndFunction
	
	
	
	Method UpdateState()
		Y=Y-SPEED_MAX
		If Y<0 Then 
			ListRemove entityList, Self
		EndIf		
	EndMethod

EndType


please use the code tags in this forum to get code boxes. See more information here: forum codes


Midimaster(Posted 2015) [#5]
by the way... here are some more ideas for your game logic:

1. Enemies should also die when leaving the screen. New one will be born independent. So you can create more than one enemy at the same time:
Type TEnemy Extends TEntityObject
    ....
	Global LaunchTimer%
	
    Method UpdateState()
	If LaunchTimer<MilliSecs()
		LaunchTimer=MilliSecs()+1000
		Local enemy:TEnemy = TEnemy.Create("",Random(800),0)
	EndIf
	
	If Y > 634 Then 
			ListRemove entityList, Self
	EndMethod



2. The checking of the KEY_DOWNs will need a TIMER, otherwise the SPEED will react to intensive. You can already see this with the KEY_SPACE in my code...

3. In TYPES it is usefull to have GLOBAL and FIELD variables. The GLOBAL handle all things belonging to all members of this object. Here your TEnemy type as example: The images can be loaded at the beginning and used often during the game without loosing speed:
SuperStrict
AppTitle="Space Invaderz"
Graphics 800,600
....
'*****Variables****
Global entityList:TList = CreateList()
....
'Load and create our enemy
TEnemy.StartUp("/Users/sanjitsingh/Downloads/Data/Enemy.png")
Local enemy:TEnemy = TEnemy.Create(400,0)
.....

'create our enemy
Type TEnemy Extends TEntityObject
	Global CommonImage:TImage
	Field Speed:Int = 3

	Function StartUp(File:String)
		CommonImage = LoadImage(File)
		If CommonImage = Null
		     Print "File: " + File +" not found"
		     End 
		EndIf
	End Function

	'Create our enemy
	Function Create: TEnemy(xStart:Int , yStart:Int)
		Local enemy:TEnemy = New TEnemy
		enemy.X = xStart
		enemy.Y = yStart
		enemy.Image = CommonImage
		ListAddLast entityList, Enemy
		Return enemy
	EndFunction
	
	.....

EndType



Sanjit(Posted 2015) [#6]
Hey mindmaster its still not working there is one probelm now.

here is the code:


'Keep good coding practice
SuperStrict

'Set application and Graphics
AppTitle="Space Invaderz"
Graphics 800,600
Const SPEED_MAX#=5







'*****Variables****
Global entityList:TList = CreateList()

'Load and create background
Local Background:TBackground = TBackground.Create("/Users/sanjitsingh/Downloads/Data/ScreenshotStarfield.png",0,0,1.0)
Local Background2:TBackground = TBackground.Create("/Users/sanjitsingh/Downloads/Data/ScreenshotStarfield.png",0,-600,1.0)
'Load and create our player
Local player:TPlayer = TPlayer.Create("/Users/sanjitsingh/Downloads/Data/9.png",400,300)
'Load and create our enemy
TEnemy.StartUp("/Users/sanjitsingh/Downloads/Data/Enemy.png")
Local enemy:TEnemy = TEnemy.Create(400,0)



'Main Loop
Repeat
Cls 'Clear the screen
For Local a:TEntityObject = EachIn entityList
a.DrawState()
a.UpdateState()

Next

Flip 'flip our buffers




Until AppTerminate() Or KeyHit(KEY_ESCAPE)

'*****Types*****
'Create a generic Type
Type TEntityObject
Field X:Int
Field Y:Int
Field Speed:Int = 5
Field Image:TImage

'Draw Entity


Method DrawState()
DrawImage Image,X,Y
DrawRect X,y,5,5

'Let empty for now

EndMethod



'Update our player
Method UpdateState() Abstract


EndType


'Create a type to hold our player
Type TPlayer Extends TEntityObject
Field LaunchTimer%



'Create a function to load our player
Function Create:TPlayer(File:String,xStart:Int, yStart:Int)
Local player:TPlayer = New TPlayer
player.X = xStart
player.Y = yStart

ListAddLast entityList, player
Return player

'Check to see if image is loaded
If player.Image = Null
Print "9.png not found"
End 'Exit program
EndIf


'Add player to list

EndFunction



Method UpdateState()
'Move our player up
If KeyDown(KEY_UP)
Y:-Speed

EndIf
'Move our player down
If KeyDown(KEY_DOWN)

Y:+Speed
If LaunchTimer<MilliSecs()
LaunchTimer=MilliSecs()+100

EndIf
'Move our player left

If KeyDown(KEY_LEFT)
X:-Speed
EndIf
'else Move our player RIGHT else if
ElseIf KeyDown(KEY_RIGHT)
X:+Speed

EndIf
If KeyDown(KEY_SPACE)
If LaunchTimer<MilliSecs()
LaunchTimer=MilliSecs()+100
Local missile:TMissile = TMissile.Create("",X,Y)
EndIf
EndIf




'Check for screen boundries
If X<0 Then X=0
If X>736 Then x=736
If Y<0 Then Y=0
If Y>536 Then Y=536



EndMethod
EndType

Type TMissile Extends TEntityObject

Function Create:TMissile(File:String,xStart:Int, yStart:Int)
Local missile:TMissile = New TMissile
missile.X = xStart
missile.Y = yStart
ListAddLast entityList, missile
Return missile
EndFunction



Method UpdateState()
Y=Y-SPEED_MAX
If Y<0 Then
ListRemove entityList, Self
EndIf
EndMethod

EndType



'create our enemy
Type TEnemy Extends TEntityObject
Global CommonImage:TImage
Field Speed:Int = 3

Function StartUp(File:String)
CommonImage = LoadImage(File)
If CommonImage = Null
Print "File: " + File +" not found"
End
EndIf
End Function


'Create our enemy
Function Create: TEnemy(xStart:Int , yStart:Int)
Local enemy:TEnemy = New TEnemy
enemy.X = xStart
enemy.Y = yStart
enemy.Image = CommonImage
ListAddLast entityList, Enemy
Return enemy

'Check to see if enemy exists

If enemy.Image = Null
Print "mothership.gif not found"
End 'Exit program
EndIf
'Add Enemy to list


EndFunction

Global LaunchTimer%


Method UpdateState()
If LaunchTimer<MilliSecs()
LaunchTimer=MilliSecs()+1000
Local enemy:TEnemy = TEnemy.Create("",Random(800),0)


EndIf

If Y > 634 Then
ListRemove entityList, Self


EndIf
EndMethod


EndType


'TBackground
Type TBackground Extends TEntityObject
Field yScroll:Float

'Create our background
Function Create:TBackground(File:String, xStart:Int, yStart:Int, yScroll:Float)
Local background:TBackground = New Tbackground
background.X = xStart
Background.Y = yStart
background.yScroll = yScroll
background.image = LoadImage(File)

'Check our background

If background.Image = Null
Print "Background.png not found"
End 'Exit Program

EndIf
ListAddLast entityList, background

Return background

EndFunction

Method UpdateState()
If Y > 600
Y = -599
EndIf
Y:+yScroll

EndMethod
EndType

error returned: Too many function parameters:

this is the error code: Local enemy:TEnemy = TEnemy.Create("",Random(800),0)


Midimaster(Posted 2015) [#7]
Random
Sorry, my mistake: RANDOM is the wrong command. Correct is RAND. And the TEnemy.Create() now has only 2 parameters X and Y and not longer a path!
Local enemy:TEnemy = TEnemy.Create(Rand(800),0)


Understanding?
You should use the new StartUp() technics also at type MISSILE. Do not copy the code, but try to understand it. Did you understand it?

You have some new bugs in your code....

In Type ENEMY UpdateState() you forgot the line which adds the y-movement of the enemies:
Y:+Speed




Use TABs in your code
There is a bug inside your PLAYERS UpdatesState() method: Check all ENDIF commands: One is at a wrong place. But it is difficult to find. Prevent those problems by using the TABs to structure your code. Otherwise it will be very difficult to find such errors.

Without TABs:
Method UpdateState()
'Move our player up
If KeyDown(KEY_UP)
Y:-Speed

EndIf
'Move our player down
If KeyDown(KEY_DOWN)

Y:+Speed
If LaunchTimer<MilliSecs()
LaunchTimer=MilliSecs()+100

EndIf
'Move our player left

If KeyDown(KEY_LEFT)
X:-Speed
EndIf
'else Move our player RIGHT else if
ElseIf KeyDown(KEY_RIGHT)
X:+Speed

EndIf
If KeyDown(KEY_SPACE)
If LaunchTimer<MilliSecs()
LaunchTimer=MilliSecs()+100
Local missile:TMissile = TMissile.Create("",X,Y)
EndIf
EndIf




'Check for screen boundries
If X<0 Then X=0
If X>736 Then x=736
If Y<0 Then Y=0
If Y>536 Then Y=536



EndMethod

With TABs:
Method UpdateState()
	
	'Move our player up
	If KeyDown(KEY_UP)
		Y:-Speed
	EndIf
	
	'Move our player down
	If KeyDown(KEY_DOWN)
		Y:+Speed
		If LaunchTimer<MilliSecs()
			LaunchTimer=MilliSecs()+100	
		EndIf

		'Move our player left		
		If KeyDown(KEY_LEFT)
			X:-Speed
		EndIf

	'else Move our player RIGHT else if
	ElseIf KeyDown(KEY_RIGHT)
		X:+Speed		
	EndIf
	
	If KeyDown(KEY_SPACE)
		If LaunchTimer<MilliSecs()
			LaunchTimer=MilliSecs()+100
			Local missile:TMissile = TMissile.Create("",X,Y)
		EndIf
	EndIf
	
	'Check for screen boundries
	If X<0 Then X=0
	If X>736 Then x=736
	If Y<0 Then Y=0
	If Y>536 Then Y=536
EndMethod


Forum here
please use the code tags in this forum to get code boxes. With "codebox" and "/codebox" in square brackets you can put code into boxes:
[codebox ] Graphics 800,600 [/codebox ]
See more information here: forum codes


PRINT for debugging
And another advise: During your debug phase of the game you should use more PRINT commands to know in each moment, at which line of the code your game is. See these samples:



markcw(Posted 2015) [#8]
Nice work Midimaster.


Sanjit(Posted 2015) [#9]
hey midi master the shoot command works. i understand how to do it now. but there is one minor error now. my image is not showing up.

code:
'Keep good coding practice
SuperStrict
'Set application and Graphics
AppTitle="Space Invaderz"
Graphics 800,600
Const SPEED_MAX#=5
'*****Variables****
Global entityList:TList = CreateList()

'*******************************
Print "Load and create background"
'*******************************

'Load and create background
Local Background:TBackground = TBackground.Create("/Users/sanjitsingh/Downloads/Data/ScreenshotStarfield.png",0,0,1.0)
Local Background2:TBackground = TBackground.Create("/Users/sanjitsingh/Downloads/Data/ScreenshotStarfield.png",0,-600,1.0)
'*******************************
Print "Load and create our player"
'*******************************

'Load and create our player
Local player:TPlayer = TPlayer.Create("/Users/sanjitsingh/Downloads/Data/9.png",400,300)
'Load and create our enemy
TEnemy.StartUp("/Users/sanjitsingh/Downloads/Data/Enemy.png")
Local enemy:TEnemy = TEnemy.Create(400,0)




'*******************************
Print "Starting Main Loop"
'*******************************
'Main Loop
Repeat
Cls 'Clear the screen
For Local a:TEntityObject = EachIn entityList
a.DrawState()
a.UpdateState()

Next

Flip 'flip our buffers




Until AppTerminate() Or KeyHit(KEY_ESCAPE)

'*****Types*****
'Create a generic Type
Type TEntityObject
Field X:Int
Field Y:Int
Field Speed:Int = 5
Field Image:TImage

'Draw Entity


Method DrawState()
DrawRect X,Y,5,5
DrawImage Image,X,Y






EndMethod



'Update our player
Method UpdateState() Abstract


EndType


'Create a type to hold our player
Type TPlayer Extends TEntityObject
Field LaunchTimer%



'Create a function to load our player
Function Create:TPlayer(File:String,xStart:Int, yStart:Int)
Local player:TPlayer = New TPlayer
player.X = xStart
player.Y = yStart
ListAddLast entityList, player
Return player
EndFunction



Method UpdateState()

'Move our player up
If KeyDown(KEY_UP)
Y:-Speed

EndIf
'Move our player down
If KeyDown(KEY_DOWN)

Y:+Speed
If LaunchTimer<MilliSecs()
LaunchTimer=MilliSecs()+100

EndIf
'Move our player left

If KeyDown(KEY_LEFT)
X:-Speed
EndIf
'else Move our player RIGHT else if
ElseIf KeyDown(KEY_RIGHT)
X:+Speed

EndIf
If KeyDown(KEY_SPACE)
If LaunchTimer<MilliSecs()
LaunchTimer=MilliSecs()+100
Local missile:TMissile = TMissile.Create("",X,Y)
EndIf
EndIf




'Check for screen boundries
If X<0 Then X=0
If X>736 Then x=736
If Y<0 Then Y=0
If Y>536 Then Y=536



EndMethod
EndType

Type TMissile Extends TEntityObject

Function Create:TMissile(File:String,xStart:Int, yStart:Int)
Local missile:TMissile = New TMissile
missile.X = xStart
missile.Y = yStart

'*******************************
Print "Launch another missile"
'*******************************

ListAddLast entityList, missile
Return missile
EndFunction



Method UpdateState()
Y=Y-SPEED_MAX


If Y<0 Then
ListRemove entityList, Self
EndIf

EndMethod

EndType



'create our enemy
Type TEnemy Extends TEntityObject
Global CommonImage:TImage
Field Speed:Int = 3

Function StartUp(File:String)
CommonImage = LoadImage(File)
If CommonImage = Null
Print "File: " + File +" not found"
End
EndIf
End Function


'Create our enemy
Function Create: TEnemy(xStart:Int , yStart:Int)
Local enemy:TEnemy = New TEnemy
enemy.X = xStart
enemy.Y = yStart
enemy.Image = CommonImage
ListAddLast entityList, Enemy
Return enemy






EndFunction

Global LaunchTimer%


Method UpdateState()
Y:+Speed
If LaunchTimer<MilliSecs()
LaunchTimer=MilliSecs()+1000
Local enemy:TEnemy = TEnemy.Create(Rand(800),0)

EndIf

If Y > 634 Then
ListRemove entityList, Self


EndIf
EndMethod


EndType


'TBackground
Type TBackground Extends TEntityObject
Field yScroll:Float

'Create our background
Function Create:TBackground(File:String, xStart:Int, yStart:Int, yScroll:Float)
Local background:TBackground = New Tbackground
background.X = xStart
Background.Y = yStart
background.yScroll = yScroll
background.image = LoadImage(File)

'Check our background

If background.Image = Null
Print "Background.png not found"
End 'Exit Program

EndIf
ListAddLast entityList, background

Return background

EndFunction

Method UpdateState()
If Y > 600
Y = -599
EndIf
Y:+yScroll

EndMethod
EndType

DrawImage Image,X,Y error: "unhandled Exception: Attempt to access field or method of null Object"


Midimaster(Posted 2016) [#10]
The question is: "Which of the images in not shown?".

Well... your DrawImage function is used by four types: PLAYER, ENEMY, BACKGROUND and MISSILE. Did you really create 4 images? Did you really load it? Did you check all four?

This code should exist (similar) for all four types:
If background.Image = Null
    Print "Background.png not found"
    End 'Exit Program
EndIf 


In your Blitzmax IDE switch PROGRAM - BUILD OPTIONS - DEBUG BUILD on!

Dear friend...

You are working too fast and not thoroughly enough for coding games. But accurateness (as a first step) is the basis for finding potential bugs in code (later).Check all my advises again and use them on all four types in your game. Sorry, but you are very immune to feedback...


Dan(Posted 2016) [#11]
sanjit, can you please use the




when pasting your code here in this forum, it is really helpfull for us to copy and paste your code and not having to worry if we got your questions and comments.