PDA

View Full Version : Simple Snake Game - Castle Game Engine



eugeneloza
07-11-2016, 01:47 PM
This is an thoroughly-commented example of a very simple 2D game featuring:

* graphics with spritesheets,
* text output,
* keyboard/mouse input,
* read&write text files,
* sound & music,
* timer,
* Log,
* random

basically everything you might need to start making a simple 2D game in Castle Game Engine.

https://github.com/eugeneloza/SnakeGame

http://www.pascalgamedevelopment.com/attachment.php?attachmentid=1431&stc=1

SilverWarior
08-11-2016, 06:43 PM
Nice example.

I haven't actually tried your game as I don't have Lazarus installed on this computer but after taking a quick look at your code I believe I found a small error/bug.

When you are setting the window size you are using maxx for calculating both window height and window width. Shouldn't you be using maxy when calculating window height?

Also after looking at your TRabbit.ResetRabbit procedure I fear that when snake becomes really big and occupies most of the playfield this procedure would take quite some time before being able to place the rabbit at new location since the random point could be often generated at position that is currently occupied by snake.
You could solve this by having additional list of all free cells where rabbit can be placed so you would be simply randomly choosing one of this list items (cells) instead of choosing random coordinates (one random call and no loop).
But the problem is that such list would have to be constantly updated on every snake move which could lead to its own overhead especially at the start of the game where such list would contain lots of items due to lots of free spaces which would result in lots of data movement when an list item is removed..
So perhaps you could use current approach until the snake size reaches certain point like 75% of its maximum size (the number of all cells on the game map) and then switch to using list of free cells as suggested above.

EDIT: By the way I recommend you rename your unit1.pas to some more meaningful name which would allow others to quickly realize what it is used for.

eugeneloza
09-11-2016, 12:10 PM
Shouldn't you be using maxy
Fixed, thanks!

I recommend you rename your unit1.pas
Yes, I've fixed this also. Thank you!

this procedure would take quite some time
yes, that's possible, but I'm afraid optimizations will obfuscate the code. Still it'll have to test only 16*16*sqrt(2) combinations in average to find the last remaining tile to place a rabbit, i.e. 360 tests, which is affordable. The other problem is that it will hang up the next moment when there are no free tiles left :)
Yes, I couldn't resist adding sprites for the snake, but actually I've tried to keep the code as simple and clear as it is possible and separate out any game-specific algorithms.
Fixed possible freezing. Thanks!

SilverWarior
09-11-2016, 06:39 PM
yes, that's possible, but I'm afraid optimizations will obfuscate the code.

I'm aware of that. That is why I suggested using combination of current and optimized approach based on certain condition. This would allow people to know that optimization is optional.
But regardless you should notify people that would potentially use your code about possible caveats that such code represents.


Still it'll have to test only 16*16*sqrt(2) combinations in average to find the last remaining tile to place a rabbit, i.e. 360 tests, which is affordable.

I'm afraid your math won't hold. Why? Because it is quite possible that your random algorithm would generate same pair of X and Y coordinates multiple times. Now why is that? When generating random numbers most random number algorithms internally generate random floating value between 0 and 1 and then this number is linearly translated to integer value where internal value of 1 would be translated to maximum integer value you specified. This results in rounding the internal number at some time which means that several near floating numbers would translate to same output integer value and thus also the possibility for generating same pair of output values.


The other problem is that it will hang up the next moment when there are no free tiles left :)

Easy solution.
If your snake size equals to the number of all tiles on the map "Congratulations you won the game" :D

Ñuño Martínez
10-11-2016, 01:06 PM
Downloading. Thank-you!

p.s: If it runs on Android... ::)

eugeneloza
14-11-2016, 10:24 AM
p.s: If it runs on Android...
I've thought of porting it to Android. That's easy in Castle Game Engine. But I haven't done it yet.