PocketMagic

PocketMagic

Where Technology meets magic


Android
45 Posts
BlackBerry
4 Posts
Electronics
68 Posts
Hardware
120 Posts
High Voltage
49 Posts
iPhone
4 Posts
Linux
2 Posts
Nuclear
20 Posts
Optics
11 Posts
Photography
7 Posts
Photoshop
3 Posts
Research
19 Posts
Reviews
18 Posts
Robotics
8 Posts
Security
7 Posts
Software
73 Posts
Symbian
2 Posts
Tubes
18 Posts
Windows
3 Posts
Windows Mobile
11 Posts

Top Articles!       See PocketMagic on Facebook


uRADMonitor - Online Radiation monitoring station | 14997 Views | Rate 70.08
uRADMonitor - Online Radiation monitoring station
Bluetooth and iOS - Use Bluetooth in your iPhone apps | 18455 Views | Rate 59.34
Bluetooth and iOS - Use Bluetooth in your iPhone apps
NMEA GPS Library for AVR Microcontrollers | 4876 Views | Rate 56.05
NMEA GPS Library for AVR Microcontrollers
Programmatically Injecting Events on Android - Part 2 | 5118 Views | Rate 44.89
Programmatically Injecting Events on Android - Part 2
Building a robot – Part 2 | 4686 Views | Rate 44.21
Building a robot – Part 2
Simple Switched power Supplies | 16303 Views | Rate 42.13
Simple Switched power Supplies
Capacitor Discharge Microspot Welder / Cutter | 11444 Views | Rate 36.56
Capacitor Discharge Microspot Welder / Cutter
Atmega8 and Nokia 5110 LCD  | 1580 Views | Rate 34.35
Atmega8 and Nokia 5110 LCD

 
  

ATMega8 LCD Game


By Radu Motisan Posted on October 17th, 2009 , 795 Views (Rate 0.61)



  




To my ATMega8 development board I've just added a few more things: a pot for ADC readings (0..255) and a few push-buttons, so I had the idea of quickly writing a simple game. This article is about games and their implementation from electronics up to the software level. As a software developer, I had to keep in mind the limited resources of the ATMega8 but those were more then enough for this application.

The hardware

I've used an ATMega8 microcontroller, connected to an 2x16 LCD and three pushbuttons one of which was the MCU reset:

The pins for Pusbhbutton 1 and Pushbutton 2 are connected to Ground through 10k resistors to keep the PIN state LOW. When you press one of these buttons, the PIN receives full VCC (5V) so the PIN state changes to HIGH.
Reset works the opposite way: its state is HIGH by default, and when the user presses the pushbutton, the reset PIN gets grounded and the microcontroller resets.
The LCD is a HD44780 based 2x16 LCD with backlight. Previous articles described the LCD in more detail.

The software

You can download the source code here

A few words on the algorithm:
To optimize the refresh, I'm using a screen buffer of 2x16 characters.
char screenbuffer[2][16];
At each microcontroller tick, I'm putting the content in this buffer then pushing the buffer to the LCD:
lcd_string2("%s\n%s", screenbuffer[0], screenbuffer[1]);
The game displays your character, a little ship represented by the "greater than" character ">" at the left of the screen.
Randomly generated enemies "@" advance from the right side towards your position. The task is to avoid them by moving "UP" to the first LCD line or "DOWN" to the second line using the pushbuttons.

int but1 = PUSHBUTTON1Status();
int but2 = PUSHBUTTON2Status();
f (but1) shipindex = 0; //move user ship
if (but2) shipindex = 1;

The enemies are generated randomly, and put in a double queue (for each row):
int enemy[2][16] = {0};
int rand1 = rand() % 10;
int rand2 = rand() % 10;
if (rand1 == 5) enemy[0][15] = 1; //add new enemy row1
else
if (rand2 == 5) enemy[1][15] = 1; //add new enemy row2
for(int j=0;j<15;j++) //add enemy to screen buffer
{
if (enemy[0][j]) screenbuffer[0][j] = '@';
if (enemy[1][j]) screenbuffer[1][j] = '@';
}


and are advancing to the left at each microcontroller tick:

for (int j=0;j<15;j++)
{
if (enemy[0][j+1]) { enemy[0][j] = 1; enemy[0][j+1] = 0; } //advance row1
if (enemy[1][j+1]) { enemy[1][j] = 1; enemy[1][j+1] = 0; } //advance row2
}

You loose when an enemy hits your ship:
f (enemy[0][0])
{
if (shipindex == 0) //you are on the first line like the enemy
{
lcd_clrscr();
lcd_string2("OUCH-GAME OVER\n press RESET ");
return 1;
}
enemy[0][0] = 0; //remove left position enemy
}
if (enemy[1][0])
{
if (shipindex == 1)
{
lcd_clrscr();
lcd_string2("OUCH-GAME OVER\n press RESET ");
return 1;
}
enemy[1][0] = 0; //remove left position enemy
}

What we're doing here is to check if the enemy coordinate matches your ship's and exit the program with a "You lost" message, or delete the enemies on the first row from the buffer:

enemy[0][0] = 0; //remove left position enemy

Each microcontroller tick is counted, and this number also represents the total frames drawn, and your score. Manage to survive until 99 points to win this game.

if (i==99)
{
lcd_clrscr();
lcd_string2(" YOU WON! \n press RESET ");
return 1;
}

There's a delay that sets the difficulty:

//some delay
delay_us(300000);

This value changes the animation speed, so the enemies will advance faster or slower towards your position. You could upgrade this game and add levels instead of terminating the program at 99 points: each new level sets a lower frame delay, so the animation is faster (and the game harder) as you advance through levels.

Enjoy!






  

More on PocketMagic:

How to set the AVR Fusebits | 1757 Views | Rate 23.74
How to set the AVR Fusebits
ATMega128 and HD44780 LCD using 3 Wires with the 74HC164 | 2094 Views | Rate 22.76
ATMega128 and HD44780 LCD using 3 Wires with the 74HC164
Repairing a Victoreen CDV-700 6B Dosimeter  | 175 Views | Rate 21.88
Repairing a Victoreen CDV-700 6B Dosimeter
Dual H-Bridge for controlling two motors | 1249 Views | Rate 21.53
Dual H-Bridge for controlling two motors
USBAsp -  AVR USB Programmer  | 8186 Views | Rate 21.15
USBAsp - AVR USB Programmer
Developing for Blackberry 10 | 97 Views | Rate 19.4
Developing for Blackberry 10

3 Responses to “ATMega8 LCD Game”

  1. 1
    china m:

    can anyone tell where exactly it is? tks

  2. 2
    avrlab:

    Where there in the source code archive Game files? I find sj many librarys but i have not interrested in that libraries. Please make archive with files dedicated only for this project. Thnx

  3. 3
    Radu Motisan:

    the code is available here:
    http://www.pocketmagic.net/wp-content/uploads/2009/10/lcd_2x16_board_game.zip

Thank you for commenting. Your comment won't show until approved, which can take some time.

Please copy the string FVIANq to the field below: