My first attempt at game programming.

Community Forums/Developer Stations/My first attempt at game programming.

Makis(Posted 2017) [#1]
Below is the code for my first ever game programmed in BlitzPlus.
I delved for a while with C# but i found it unnecessary difficult for me with all those methods,long types, short types, namespaces etc, so i returned to blitzplus. I am still a beginner, but at least i can program a little game in 160 lines of code.
I found it very rewarding for a newbie like me to create a game in a programming language. I tried Game Maker Studio in the past however the built in scripting language is nowhere near as easy (and charming!) as B+.
Can you please recommend any improvements and/or errors in my code?
(I'm still trying to learn how to create a simple menu controlled by the cursor keys....)

;Rev 0.69 07/02/2017
;by Makis Kampouris

AppTitle "Matatias Adventures"
Graphics 1024,768,32,2
SetBuffer BackBuffer()
SeedRnd MilliSecs()

; R E S O U R C E S L O A D I N G
AutoMidHandle True
Global img_matatiaslogo = LoadImage ("gfx/matatiaslogo.png")
Global img_Matatias = LoadImage ("gfx/Matatias.png")
Global img_Diamond = LoadImage ("gfx/Diamond.png")
Global img_Enemy = LoadImage ("gfx/cocoviko.png")
Global snd_pickup = LoadSound ("snd/Pickup.wav")
Global snd_gameover=LoadSound ("snd/gameover.wav")
Global fnt_8bitwonder=LoadFont ("8BIT WONDER",20)
Global fnt_8bitwonder14=LoadFont ("8BIT WONDER",14)

;Player variables
Global matX = 512
Global matY = 384
Global matspeed# = 2.0
Global Score = 0
Global lives = 3
Global diamondsleft = 18

;T Y P E S

Type Diamond
Field x
Field y
Field diamondsleft
End Type

Type Enemy
Field x
Field y
End Type

Generatediamonds ()
Generateenemies ()
PresSpace ()

; M A I N L O O P
While Not KeyHit (1)
Score ()
Outofbounds ()

DrawImage img_Matatias,matX,matY

;draw diamonds
For pickup.Diamond = Each Diamond
DrawImage img_Diamond,pickup\x, pickup\y
;pick up diamonds
If ImagesCollide (img_Diamond,pickup\x,pickup\y,0,img_Matatias,matX,matY,0)
PlaySound snd_pickup
Score = Score + 10
matspeed = matspeed + 0.5
Delete pickup
End If

;draw enemies
For cocoviko.Enemy=Each Enemy
DrawImage img_Enemy,cocoviko\x,cocoviko\y

;draw game over
If ImagesCollide (img_Matatias,matX,matY,0,img_Enemy,cocoviko\x,cocoviko\y,0) Then

SetFont fnt_8bitwonder
PlaySound snd_gameover
lives = lives -1
Delete cocoviko

End If


Welldone ()
Endgame ()

; F U N C T I O N S

;Player Score
Function Score()
SetFont fnt_8bitwonder14
Text 10,10," SCORE: " + Score
Text 900,10," LIVES : " + lives
End Function

;Player movement
Function Playermovement()
If KeyDown (203) Then matX = matX - matspeed ;left
If KeyDown (205) Then matX = matX + matspeed ;right
If KeyDown (200) Then matY = matY - matspeed ;up
If KeyDown (208) Then matY = matY + matspeed ;down
End Function

;Prevents player from moving out of screen
Function Outofbounds ()
If matX > 1008 Then matX = 1008
If matY > 748 Then matY = 748
If matX < 15 Then matX = 15
If matY < 20 Then matY = 20
End Function

;Intro screen "press space" message
Function PresSpace()
SetBuffer BackBuffer()
SetFont fnt_8bitwonder
DrawImage img_matatiaslogo,522,200
Text 250,384," Press space to start the game "
SetFont fnt_8bitwonder14
Color 255,255,0
Text 390,700, "by Makis Kampouris (c) 2015"
Repeat Until KeyHit (57)
End Function

;Well done screen
Function Welldone ()
If diamondsleft = 0
SetBuffer BackBuffer ()
Color Rnd(0,255),Rnd(0,255),Rnd(0,255)
Text 420,350," W E L L D O N E "
Delay 200
End If
End Function

;Endgame screen
Function Endgame ()
If lives=-1 Then
Text 512,384, " G A M E O V E R",1,1
Delay 3000
End If
End Function

;Generate diamonds
Function Generatediamonds ()
For z = 1 To 18
pickup.Diamond = New Diamond
pickup\x = Rand (1024)
pickup\y = Rand (768)
End Function

;Generate enemies
Function Generateenemies()
For z=1 To 15
cocoviko.Enemy=New Enemy
End Function

Dan(Posted 2017) [#2]
Hi, it is a bit hard to test the code without the media.

But if you would like to write a menu which is driven by cursor keys, i have allready explained it in this post (#6 and #11)

Makis(Posted 2017) [#3]
Thanks Dan I'll check it out.
Can I upload the graphics on the forum?

Steve Elliott(Posted 2017) [#4]
You have to send a link to where the media is hosted.

Rick Nasher(Posted 2017) [#5]
Suggestion(I like this free one):

Makis(Posted 2017) [#6]
Assets files uploaded to mediafire.

Dan(Posted 2017) [#7]
Its Great. It reminds me of an homebrew c64 game, which i used to remake on Amiga with the Amos language.

Edit: Right, i have overlook that this code is for BlitzPlus and not Blitz3d (which im using here) but it is running on the both versions.

There aren't many errors, maybe few improvements.

The Game does not run well, when used in fullscreen display. And i mean the Press space function does not display anything over here.
The screen is black. The Game Over and Well done screens flickers with the game over text and the remaining bugs.

When you'r using the backbuffer, then it is advisable to use it in this way:

Set the Backbuffer
Clear the Screen
Draw things
flip (the backbuffer)
repeat it

The SetBuffer command can be removed from every function call, because you are not using different buffers in this game.

In the function Endgame and Welldone i would replace the delay with an timer variable.
Like this:
			Color Rnd(0,255),Rnd(0,255),Rnd(0,255) 
			Text 420,350," W E L L D O N E "
			Delay 1
		Until MilliSecs()-Delaytimer>3000

Here are all the changes i would set up and still keep your way of writing it:

And eventually split up the Drawing code and the collision detection code into 2 parts.
It makes the code longer, but you will avoid the short flickering, which occurs when the player pickup the first gem/bug in the list.

;draw diamonds


For pickup.Diamond = Each Diamond

	DrawImage img_Diamond,pickup\x, pickup\y
	If diamondnumber=1 Then Color $00,$ff,$00 : Rect pickup\x - 10, pickup\y - 10,20,20,0
;pick up diamonds

If you want to see what i mean, use the above code, then before picking up the green squared gem, watch the other gems.

Makis(Posted 2017) [#8]
Thanks for your suggestions Dan. I 'll give them a try later.

Makis(Posted 2017) [#9]
Is it good practice to use gosub? I read somewhere that it should be avoided.
I'm having difficulty to understand the [ Text GraphicsWidth()/2-((Len(txt$)/2)*9),384,txt$] line.

Dan(Posted 2017) [#10]
The gosub should be avoided in the cases where you use multiple gosubs where the chance that one return may be missed.
Single gosubs with clear returning point should be harmless.

I'm having difficulty to understand the [ Text GraphicsWidth()/2-((Len(txt$)/2)*9),384,txt$] line.

GraphicsWidth()/2 gives the middle point of the screen horizontaly. The Resolution is set with Graphics 1024,768,32,2 and the half of 1024 is 512.

Txt$ holds the text : " Press space to start the game "

Len(txt$) gives the length of that text, which should be 31

Len(txt$)/2 gives the half of the length of that text, which is arround 15

the 9 should be the width of the font. multiplied with the half of the text length. it sets the starting point for the text command to halfscreen-(half of textlength * single char width)

so the GraphicsWidth()/2-((Len(txt$)/2)*9) gives a number, which alings the text horizontally, so that it appears to be in the middle.

It was written so, in case you want to change the graphic resolution, that the text will be centered horizontally on the screen.

And if you change the text, it will/should still appear centered to the screen.

coffeedotbean(Posted 2017) [#11]
My advice is to use


if Bplus has a strict mode?!

Makis(Posted 2017) [#12]
Never heard of strict before!!

Dan(Posted 2017) [#13]
No b+ does not have it. Strict is from blitzmax.

Makis(Posted 2017) [#14]
Interesting feature.