I had already made up the LCD circuit with push buttons in a handy hobby box for another project I was working on, so all I needed was the code.
The game is very simple, you have a single digit at the top left of screen which can be any number between 0 and 9 inclusive. you can change this number by pressing either the up or down buttons. To the top right of screen is a random number (also between 0 and 9 inclusive) this number will scroll towards you (leaving a trail of the same number). You need to use the up/down arrows to change your number so that it matches the random number coming towards you.
If you match the number by the time it hits you, then you get a point. If you do not match it, then it is game over and the high score is displayed on the screen. You then press a button to play again.
Here's a couple of photos:
For those interested, here's the code:
Code: Select all
Device = 18F4685
Clock = 8 // This is used to tell the compiler what clock we are using
Config OSC = IRCIO67 // We want to use the internal oscillator.
// some LCD options...
#option LCD_DATA = PORTD.4 // Assign the LCD connections
#option LCD_EN = PORTD.3 //
#option LCD_RS = PORTD.2 //
// import library's
Include "LCD.bas"
Include "convert.bas"
// port setup
Dim button_down As PORTA.0
Dim button_up As PORTA.1
Dim button_select As PORTA.2
Dim button_menu As PORTA.3
Dim random_number As Byte
Dim random_number_saved As Byte
Dim incoming_number As Byte
Dim number_position As Word
Dim your_number As Byte
Dim score As Byte
Dim button_debounce As Byte
Dim number_speed As Byte
Dim high_score As Byte
Sub draw_screen()
LCD.WriteAt(1,(number_position),Convert.DecToStr(incoming_number,1))
LCD.WriteAt(1,1,Convert.DecToStr(your_number,1))
LCD.WriteAt(2,1,"Your Score =")
LCD.WriteAt(2,14,Convert.DecToStr(score,3))
End Sub
Sub check_buttons()
If button_down = 1 Then
random_number_saved = random_number
EndIf
If button_up = 1 Then
random_number_saved = random_number
EndIf
If button_up = 1 And button_debounce = 0 And your_number < 9 Then
Inc(your_number)
button_debounce = 50
EndIf
If button_down = 1 And button_debounce = 0 And your_number > 0 Then
Dec(your_number)
button_debounce = 50
EndIf
End Sub
Sub debounce_buttons()
If button_debounce <> 0 Then
Dec(button_debounce)
EndIf
End Sub
Sub update_incoming_number()
If number_position = 1 And your_number = incoming_number Then
If random_number_saved = incoming_number Then
Inc(random_number_saved)
EndIf
Inc(score)
LCD.Cls
incoming_number = random_number_saved
number_position = 16
EndIf
End Sub
Sub check_if_game_over()
If number_position = 1 And your_number <> incoming_number Then
If score > high_score Then
high_score = score
EndIf
LCD.WriteAt(1,1,"** GAME OVER **")
LCD.WriteAt(2,1,"High Score = ")
LCD.WriteAt(2,14,Convert.DecToStr(high_score,3))
Repeat Until button_select = 1
incoming_number = random_number
number_position = 16
score = 0
LCD.Cls
EndIf
End Sub
Sub move_number()
If number_speed = 0 And number_position > 1 Then
Dec(number_position)
number_speed = 50
EndIf
End Sub
Sub update_number_speed()
If number_speed <> 0 Then
Dec(number_speed)
EndIf
End Sub
Sub update_random_number()
Inc(random_number)
If random_number = 10 Then random_number = 0
EndIf
End Sub
// Start Of Program...
OSCCON = %01111111 // Sets the internal oscillator for 8Mhz
TRISD = %00000000 // Makes PORTD all outputs
TRISA = %00001111 // PORTA 0-3 inputs and 4-7 outputs
number_position = 16
random_number = 9
your_number = 0
incoming_number = 7
button_debounce = 50
number_speed = 50
score = 0
high_score = 0
DelayMS(150) // Let the LCD warm up
While true
draw_screen
check_buttons
debounce_buttons
update_incoming_number
check_if_game_over
move_number
update_number_speed
update_random_number
Wend