Results 1 to 6 of 6

Thread: Simple Snake Game - Castle Game Engine

  1. #1

    Post Simple Snake Game - Castle Game Engine

    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

    Attached Images Attached Images
    My free and opensource games: http://decoherence.itch.io/
    Sources are here: https://github.com/eugeneloza?tab=repositories

  2. #2
    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.
    Last edited by SilverWarior; 08-11-2016 at 06:46 PM.

  3. #3
    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!
    My free and opensource games: http://decoherence.itch.io/
    Sources are here: https://github.com/eugeneloza?tab=repositories

  4. #4
    Quote Originally Posted by eugeneloza View Post
    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.

    Quote Originally Posted by eugeneloza View Post
    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.

    Quote Originally Posted by eugeneloza View Post
    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"

  5. #5
    Downloading. Thank-you!

    p.s: If it runs on Android...
    Last edited by Ñuño Martínez; 11-11-2016 at 10:58 AM.
    No signature provided yet.

  6. #6
    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.
    My free and opensource games: http://decoherence.itch.io/
    Sources are here: https://github.com/eugeneloza?tab=repositories

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •