[Home]Snake Game

HomePage | RecentChanges | Preferences | My Website home page

snake game

This was popular on Nokia mobile phones.

The AstroPi Shield had an example snake.c using "Raspberry Pi Sense HAT Joystick"

It shows how to poll the little joystick on the AstroPi Shield.

http://www.dougrice.plus.com/dev/PiStuff/snake/ has some reworks.

I tried to get it to poll a usb games controller with red, green, yellow, blue buttons.

This could be useful for using games controllers as inputs for my model railway projects.

zetcode.com - many snake examples in various languages

https://zetcode.com/ provided some really good examples which are well worth a read.

I seached for snake and found 4 repository results on Jan Bodnar's GitHub.

https://github.com/janbodnar/Java-Snake-Game

https://github.com/janbodnar/JavaScript-Snake-Game

and a modified copy: http://www.dougrice.plus.com/dev/PiStuff/snake/js/

This is a Snake clone in Python and Tkinter. http://zetcode.com/gui/tkinter/snake/

Adaptation of the snake.c from Raspberry Pi

http://www.dougrice.plus.com/dev/PiStuff/snake/snake_orig.c uses the little joy stick.

http://www.dougrice.plus.com/dev/PiStuff/snake/snake1.c - was modified to see if I could use a USB game controller off eBay

http://www.dougrice.plus.com/dev/PiStuff/snake/snake4.c - polls controller and does escape sequence to colour background as an example.

http://www.dougrice.plus.com/dev/PiStuff/snake/snake2.c - sets LEDs on shield as a dabble.

http://www.dougrice.plus.com/dev/PiStuff/snake/snake5.c - polls controller /dev/input/js0 and js1 and keyboard. It does poll() on multiple file descriptors and does escape sequence to colour background as an example.

http://www.dougrice.plus.com/dev/PiStuff/snake/JoystickDigiSparkDHR/JoystickDigiSparkDHR.ino is HID example for the DigiSpark modified to sequence each button in turn.

Events, states Action !

Multi Process code on a Pic

It may seem that running multiple processes on such a small chip is a bit of an ordeal.

Why do it? Here is a simple way of doing co-operative multi tasking.

A way to simplify a problem is to write a state machine with carefully choosen events and states that best fit the problem. e.g. sports Timer.

Other processes or tasks are used to map the signals, conditions and inputs to the carefully choosen events.

If your code needs to respond to more than one input, the UNIX Client / Server model does not work very well.

UNIX champions the remote procedure call. If these block, they need to return quickly.

The client sends a request to the server, and the client blocks waiting for a response from the server.

The server blocks waiting for connect requests and spawns a new process to handle the connect.

Why split up the problem into separate processes? What is a process?

Process B has to listen to Process A, Process C, the Hardware and respond and report to management.

If you need to repond to multiple sources, you need to poll each source and service any event or significant change of condition or inputs.

	Loop:
	  Poll for a change in condition1, and if found send a message to a process.
	  Poll for a change in condition2, and if found send a message to a process.

	  For each process.
		Is there a message to process? if so process it.
	  sleep 

        goto Loop.

The loop above could be run at full speed or OnTimerTick() to save power.

You do not need to poll a keyboard switch at 1000,000,000 times a second!

Poll 20 times a second, and let the processor sleep or do something else.

Make it adaptive if needs must.

Normally it is bad to loop and poll in a Windows console program as it hogs the processor e.g

  while( true ){
     // wait for character then echo it. 
     // this works well if there is only one input.

     putc( getc() );

  }  

On UNIX and other systems you can use select() to sleep the program waiting for an event on I/O or timeout.

  Loop:

        On Timer Event  
		Poll for a change in condition1, and if found send a message to a process.
		Poll for a change in condition2, and if found send a message to a process.

	For each process.
		Is there a message to process? if so process it.

        sleep, waiting for any event from a list.
        // The Micro controller sleeps can be woken by the peripherals. 
        // wait for timer to periodically run some housekeeping code.
        //
        Awake on any event being waited for 

  goto Loop.

snake.c uses poll()

 reset();
 while (running) {
   while (poll(&evpoll, 1, 0) > 0)
     handle_events(evpoll.fd);
     game_logic();
     if (check_collision(0)) {
       reset();
     }
     render();
     // This sleep is essential!
     usleep (300000);
 }

MK14 using SC/MP running in a browser

http://www.dougrice.plus.com/mk14/seg_mk14.htm - runs an sc/mp on a web browser

LISP engine

https://lisperator.net/slip/impl

Emulators written in JavaScript

Here is a website with a big list of emulators well worth a visit.

https://github.com/fcambus/jsemu

There is even a Turbo pascal compiler https://github.com/lkesteloot/turbopascal

and finally

https://astro-pi.org/ - original use of the sheild

https://www.youtube.com/watch?v=GEoDUXl5WCM - YouTube I found.


HomePage | RecentChanges | Preferences | My Website home page
This page is read-only | View other revisions
Last edited May 1, 2022 11:57 am by dougrice.plus.com
Search: