UzeSnake game - Understanding the 'under the hood mechanics'

Use this forum to share and discuss Uzebox games and demos.
User avatar
mapes
Posts: 174
Joined: Sun Feb 13, 2011 7:04 am
Location: Seattle

UzeSnake game - Understanding the 'under the hood mechanics'

Post by mapes »

I have been 'voluntold' that I need present something at elementary school's 'STEM fair'.

In my mind, I thought "I can bring my UZEBOX JAMMA, put the school's logo on a easy to make game, like UzeSnakes, or pong, and these kids will be pleased".

Well several hours of later, I'm still trying to figure out the proper way of doing the data structure for the snake expanding without using all of the memory.

I will admit I am not a high end programmer as it is not occupation. I can get by, and I know how to tweak certain things, but this one is a bit beyond me. Most of my programming is experience is on the Arduino platform, or excel macros.

Initially I was thinking of a push/pop method, cycling ring buffer, or some other trick, but I'm not sure what the right way of doing it would be.

Any input would be welcome.
User avatar
Artcfox
Posts: 1382
Joined: Thu Jun 04, 2015 5:35 pm
Contact:

Re: UzeSnake game - Understanding the 'under the hood mechanics'

Post by Artcfox »

I'm just going to give you a brain-dump of my first thought.

I'm assuming that your snake will start off one unit long, and every time it eats an apple it grows by one unit. But why is it always an apple? Snakes don't even eat apples! Some unhinge their jaws to swallow whole eggs though, that might look cool in a game.

I'm also assuming that you'll be using something like Mode 3, with no scrolling. Unless you want to get fancy, you can probably make this all tile-based.

So my thought is, you have all this video RAM, why not just use that to keep track of the entire snake automatically? Then all you would need to store as variables in RAM are the X and Y coordinate for its head (uint8_t each), and the X and Y coordinate for its tail (uint8_t each), and its length (in a uint16_t) for keeping score.

If you're going to do it this way, then you need to make sure that as your snake moves, the snake tiles are unique enough such that just by examining the neighboring tiles around both the head and the tail, you can follow them to find the snake's next connecting segment. So don't make them all squares for instance. Make them be things like a head segment, a horizontal segment, a vertical segment, a top left corner segment, a top right corner segment, a bottom left corner segment, a bottom right corner segment, and a tail segment. And then when the head moves to the next tile, update the head's X and Y, overwrite the previous head segment segment with the appropriate connecting piece w/ SetTile() and if it hasn't eaten that frame, examine the neighbors around the current tail with GetTile() to find where the first body piece connecting to its tail is, and then make that body segment become the new tail, and delete the previous tail by calling SetTile() on it with the background tile, and update the tail coordinates to point to the new tail's location. If it has eaten that frame, increase the length variable and don't delete the tail tile that frame, so it "grows".

If the head of the snake turns, you'd want to overwrite the current head with a corner body segment piece, and draw the new head going in the new direction, and if it went straight, overwrite the current head with a straight body segment piece, etc.

When it moves, check to see if it's moving into a tile that is one of its body segments, or the game border, and if so then game over.

Here is an example of making the tiles unique enough so you can follow each segment from an adjacent segment:
magnified screenshot
magnified screenshot
snake-screenshot.png (8.52 KiB) Viewed 999 times
Attachments
actual size 8x8 tiles
actual size 8x8 tiles
snake.png (1.04 KiB) Viewed 999 times
User avatar
mapes
Posts: 174
Joined: Sun Feb 13, 2011 7:04 am
Location: Seattle

Re: UzeSnake game - Understanding the 'under the hood mechanics'

Post by mapes »

I have become so rusty that I don't remember all of the functions that can be called for uzebox. When using the Arduino suite, I heavily referred to the reference page to see what functions were out there and which ones I needed to write myself for various small projects I did.

Your idea makes sense, I'll try going that route. I appreciate your input.

Let's talk about video modes. This will further show my novice skills... I usually take and existing project and try to scrub it for my project. I am not versed on the many compiling flags used, so I try to go with a working AVRStudio4 project and move from there. I was thinking of using video mode 3 but currently I am using video mode 1. When I attempted video mode 3 I couldn't get the school graphic to show up. I like the idea of 8x8 pixel tiles vice 8x6, but I'll use whatever works. When I made Daleks, I used video mode 9 and used the demo as the base.

Regarding the snake and the apple, my guess is it's any easy small round food item that kids eat. If they had the graphic version of snakes dislocating their jaws I think kids would have freaked out back in the 80s and 90s. :lol:
User avatar
Artcfox
Posts: 1382
Joined: Thu Jun 04, 2015 5:35 pm
Contact:

Re: UzeSnake game - Understanding the 'under the hood mechanics'

Post by Artcfox »

mapes wrote: Mon Feb 13, 2023 5:49 am I have become so rusty that I don't remember all of the functions that can be called for uzebox. When using the Arduino suite, I heavily referred to the reference page to see what functions were out there and which ones I needed to write myself for various small projects I did.

Your idea makes sense, I'll try going that route. I appreciate your input.

Let's talk about video modes. This will further show my novice skills... I usually take and existing project and try to scrub it for my project. I am not versed on the many compiling flags used, so I try to go with a working AVRStudio4 project and move from there. I was thinking of using video mode 3 but currently I am using video mode 1. When I attempted video mode 3 I couldn't get the school graphic to show up. I like the idea of 8x8 pixel tiles vice 8x6, but I'll use whatever works. When I made Daleks, I used video mode 9 and used the demo as the base.
Hang tight, I'll see if I can get you my best Makefile that tracks all dependencies properly setup for Mode 3 non-scrolling, using most of the lastest advances in mode 3. I'll even throw 16 RAM tiles in there in case you want to blit some sprites, and a small sample project. I'm still not sure what LDFLAGS to use for mode3-non-scrolling and RT_ALIGNED=1, so I'll keep that set to 0 for now.

Here is the API reference, though with the Makefile/example I'm making you, some mode 3 functions might not be included: http://uzebox.org/wiki/API_Functions
mapes wrote: Mon Feb 13, 2023 5:49 am Regarding the snake and the apple, my guess is it's any easy small round food item that kids eat. If they had the graphic version of snakes dislocating their jaws I think kids would have freaked out back in the 80s and 90s. :lol:
HAHA
User avatar
Artcfox
Posts: 1382
Joined: Thu Jun 04, 2015 5:35 pm
Contact:

Re: UzeSnake game - Understanding the 'under the hood mechanics'

Post by Artcfox »

Attached is the whole kit and caboodle for an advanced mode 3 non-scrolling project using the "modern" blitting concept, including a Makefile that properly tracks dependencies, automatically picks up changes to tileset.png, spriteset.png, and midifile.mid. It uses the resolution extension to mode 3, so you have a width of 32 tiles, and the pixels are more square. I also configured 16 RAM tiles, in case you did want to blit sprites (you can bump that number up to 32 in the Makefile if you really want to go nuts with them)

I included a tileset built from my quick and crude drawing of a snake, and a spriteset that just includes a smiley face. The example code bounces the smiley around just so you can get an idea of how the blitter works, but I didn't finish creating all of the defines, or do any of the game mechanics, I just included examples of common things to do in a Uzebox game, like timing sections of code, debug printing characters and values to your console (use uzem or my debug-enhancements branch in my fork of cuzebox to see them printed to your console), and a bunch of other things like sound effects, button input, restarting the game, etc.

To see the whisper console debug messages with my fork of cuzebox:

Code: Select all

git clone https://github.com/artcfox/cuzebox
cd cuzebox
git checkout debug-enhancements
make
./cuzebox ~/uzebox/MY_GAMES/modern-mode3-demo/gamefile.uze
There is a README.txt inside the zip file that goes into a little more depth.

The midi file came from my Rosegarden tutorial, which is in my Uzebox playlist. If you want more of a walkthrough for setting up GIMP to do graphics with the Uzebox palette, and setup an 8x8 grid to work with, etc, and how to sequence that MIDI completely from scratch, you can watch my videos from that playlist.

If you don't already have a MY_GAMES directory under your main uzebox directory, create one and extract the project into a directory called modern-mode3-demo inside MY_GAMES (all of the paths in the Makefile expect to find things two levels up).

Feel free to ask any questions about the demo, I know I used some of the more advanced configuration flags, but they make it so nice, so I don't really think I can ever go back to the old way. :D
Attachments
modern-mode3-demo.zip
Easy to follow "modern/advanced" mode 3 non-scrolling example, with a good clean Makefile that properly tracks dependencies
(53.21 KiB) Downloaded 249 times
User avatar
Artcfox
Posts: 1382
Joined: Thu Jun 04, 2015 5:35 pm
Contact:

Re: UzeSnake game - Understanding the 'under the hood mechanics'

Post by Artcfox »

It bothered me that my example snake didn't have eyes (or a nicer looking tail, though now with an extra tapered segment it would make the logic more complicated and it would need tapered corner pieces, so don't mind me!) :lol:
snake-with-eyes.png
snake-with-eyes.png (9.49 KiB) Viewed 956 times
I envy those with amazing pixel art skills. I seem to be stuck creating "programmer" art (or relying on my wife to help me, who is an actual artist!) :mrgreen:
Last edited by Artcfox on Tue Feb 14, 2023 6:47 am, edited 1 time in total.
CunningFellow
Posts: 1445
Joined: Mon Feb 11, 2013 8:08 am
Location: Brisbane, Australia

Re: UzeSnake game - Understanding the 'under the hood mechanics'

Post by CunningFellow »

You reckon you are bad at pixel art.

I turn every game into asteroids so I can just draw stick figures and pretend like it is a hardware limitation not my poor artistic skillz.

(I can't even draw a dragon with a beefy arm)
User avatar
Artcfox
Posts: 1382
Joined: Thu Jun 04, 2015 5:35 pm
Contact:

Re: UzeSnake game - Understanding the 'under the hood mechanics'

Post by Artcfox »

CunningFellow wrote: Tue Feb 14, 2023 1:57 am You reckon you are bad at pixel art.

I turn every game into asteroids so I can just draw stick figures and pretend like it is a hardware limitation not my poor artistic skillz.

(I can't even draw a dragon with a beefy arm)
Ha! :lol: That's one of the reasons why I was so comfortable doing 8x8 graphics. You can almost just brute force things until they look good. Going to 16x16 graphics, is definitely way more challenging, even after watching tutorials on how to make pixel art!
User avatar
mapes
Posts: 174
Joined: Sun Feb 13, 2011 7:04 am
Location: Seattle

Re: UzeSnake game - Understanding the 'under the hood mechanics'

Post by mapes »

When I sat down to program the game I had an epiphany of how the mechanics would work in a different way and continued using mode 1. But onto the mechanics...

Specifically, by having the head location put the snakes length into the array and by running a for loop through the array after every movement to subtract each area by 1. This way if the snake head moves to a zero it's valid move, but it is not zero it's a wall. This way the tail would always be a 1.

I implemented a 2 player vs. version as well as a single player version but there are a few minor bugs I'll need to sort out due to my methodology.

I still need to implement sound and music. Normally I've used the patches or music from. Megatris, but wanted to do a little 'shake rattle and roll' track. It's been a while since I did midi music using anvil back in 2004.

There have been some games I've wanted to do in the past in mode 3 (Atari 2600 River raid, Intellivision Advanced Dungeons and Dragons Cloudy Mountain), which seem simple enough, but have unique characteristics that's very appealing. I've tried to do river raid but couldn't get it to generate the map properly.

Regarding art skills, I'd say mine as good as etching DAGRON into a table with a razor and wonder what consumate V's are.
User avatar
Artcfox
Posts: 1382
Joined: Thu Jun 04, 2015 5:35 pm
Contact:

Re: UzeSnake game - Understanding the 'under the hood mechanics'

Post by Artcfox »

mapes wrote: Thu Feb 16, 2023 8:03 am When I sat down to program the game I had an epiphany of how the mechanics would work in a different way and continued using mode 1. But onto the mechanics...

Specifically, by having the head location put the snakes length into the array and by running a for loop through the array after every movement to subtract each area by 1. This way if the snake head moves to a zero it's valid move, but it is not zero it's a wall. This way the tail would always be a 1.
I'm not quite sure what you mean, when you write a part of the snake to VRAM with SetTile(), you can know what you are about to move into based on the result of GetTile().
mapes wrote: Thu Feb 16, 2023 8:03 am I implemented a 2 player vs. version as well as a single player version but there are a few minor bugs I'll need to sort out due to my methodology.

I still need to implement sound and music. Normally I've used the patches or music from. Megatris, but wanted to do a little 'shake rattle and roll' track. It's been a while since I did midi music using anvil back in 2004.
Cool, two player sounds awesome! Have you ever played the Nintendo game Snake Rattle 'n' Roll? That's one of the few Nintendo games I had as a kid. Never could beat it though. It has an awesome soundtrack that would probably sound good on the Uzebox if someone knows how to port it.


mapes wrote: Thu Feb 16, 2023 8:03 am There have been some games I've wanted to do in the past in mode 3 (Atari 2600 River raid, Intellivision Advanced Dungeons and Dragons Cloudy Mountain), which seem simple enough, but have unique characteristics that's very appealing. I've tried to do river raid but couldn't get it to generate the map properly.

Regarding art skills, I'd say mine as good as etching DAGRON into a table with a razor and wonder what consumate V's are.
Right now I'm trying to figure out how to create large levels, if I want to use something like Tiled, or roll my own thing that can generate the maps. I miss the Strong Bad Emails.
Post Reply