PDA

View Full Version : Stacker Arcade Game 8*8 LED Matrix



aznmango
- 27th May 2014, 04:25
Just wanted to contribute my code for everyone, this site really helped me finish my final project in electronics and without everyone's examples to this forum I would have been completely lost... As a newbie it took me quite awhile to code this but I got it working with a max7219 and 8x8 LED RGB Matrix and a PIC16F628A... I know I could've cleaned up the code by unfortunately with the amount of time I had I couldn't really dabble into it... Also I was having a problem with the sound hissing since the MAX7219 is so noisy... I used a 1000Uf capacitor to take care of the noise but it still annoys me sometimes...Enjoy the game! If you can find an improvement to the setup for the sound then more power too ya!

P.S. I'm also looking for a job, I graduated in electronics and was hoping a for a future in my field but, unfortunately I'm not having any luck... If there's any openings or interest in me please PM me or even email me a [email protected] I'll reply back with a resume if anyone's interested...


'Stacker_Game.BAS
'SET FUSES WHEN PROGRAMMING TO
'INTRC_OSC ENABLED
'CLKOUT & _MCLRE_OFF & _PWRTE_ENABLED


pause 500 'wait to power on
include "modedefs.bas" 'Adds this file to program
TRISB=0 'Sets all portb to outputs
TRISA=1 'ra.2 = input ra.1 knob led
cmcon = 7 'porta digital i/o
TRASH var byte 'needed for button command to store presses
trash = 0 'Must initialize to 0 prior to use and not used elsewhere
ADDRESS var byte 'variable address for display
DATAREG var byte 'variable data register for display
Y VAR BYTE 'Counter to load ADDRESS and DATAREG
X var byte 'Amount of Moves
Block_Lvl var byte 'What Lvl the Blocks are at
Speed var word 'Speed Variable
Block_Lives var byte 'Lives
LastLocation VAR byte[9] 'Last Location of Blocks
Current_X var byte 'Current Location of BLocks
Change var byte 'Use this variable to store change in X from LastLocation
WinOrLose var byte 'Variable used for a win or lost
MyBit var byte 'Referencing a Byte
MissBlock var byte 'Current Location of BLocks
OnRightSide var bit 'Blocks are on right side of LastLocation
Blink var byte 'When Switching Lives and Lvls uses this to and together two stages last dataregs
CurrentDATAREG var byte[9] 'Current Datareg Saves for each stage
Set_Speed var word 'Variable to Set Speed
Game_Difficulty var byte 'Difficulty
LED VAR BYTE 'LED Brightness
Grenade var byte 'For Grenade SOund


speaker con 0 'Sets Speaker to Portb.0


pot porta.1,6,LED 'Reads Knob for Brightness
pot porta.0,90,Game_Difficulty 'Reads Knob for Game Difficulty


'----- Symbol ------
SYMBOL CLK = PORTB.3
SYMBOL DAT = PORTB.1
SYMBOL LOAD = PORTB.2


'-----PIN SETUPS-----
low dat 'data pin for MAX7219 (1)
low clk 'clock pin for MAX7219 (13)
low load 'load pin for MAX7219 (12)


'-----MAX7219 SETUP-----
ADDRESS = $0C : DATAREG = $01 : gosub MaxWrite ' NO SHUTDOWN MODE, NORMAL OPERATION
ADDRESS = $09 : DATAREG = $00 : gosub MaxWrite ' COMBINATION BINARY, NO DECODE
ADDRESS = $0A : DATAREG = LED : gosub MaxWrite ' INTENSITY OF LEDS
ADDRESS = $0B : DATAREG = $07 : gosub MaxWrite ' SCAN LIMIT DIGIT NUMBERS
ADDRESS = $0F : DATAREG = $00 : gosub MaxWrite ' NORMAL OPERATION INSTEAD OF TEST
GOSUB Clean


WinOrLose = 1 'First turned on will show a smile face before first game initiated


Main: 'Game Initiate
if WinOrLose = 1 then
Gosub GameState_Winning
else
gosub GameState_Losing
endif
button porta.2,0,255,255,Trash,1,Blocks_Moving
x = 0 'Set X for Datareg
Block_Lvl = 8
Speed = 0
Block_Lives = 3
pot porta.1,6,LED 'Reads Knob for Brightness
pot porta.0,90,Game_Difficulty 'Reads Knob for Game Difficulty
gosub Brightness
goto Main


BrightNess:
ADDRESS = $0A : DATAREG = LED : gosub MaxWrite ' INTENSITY OF LEDS
Return


Blocks_Moving: 'Moving Blocks
pot porta.1,6,LED 'Reads Knob for Brightness
pot porta.0,90,Game_Difficulty 'Reads Knob for Game Difficulty
gosub Brightness
gosub Clean_Lvl
gosub Game_Speed
Gosub Blocks_Animation
If Block_Lvl > 0 then 'Block_Lvl is greater than this amount then Next_Lvl is applicable
button porta.2,0,255,255,Trash,1,Next_lvl
endif
goto Blocks_Moving


'-----Sounds-----


SoundFx:
FOR Grenade = $8F TO $20 STEP -3
SOUND speaker,[Grenade,1]
NEXT Grenade
RETURN


MissedSoundFx:
SOUND speaker,[14,16,7,16]'missing a block
RETURN


WinningSoundFx:
SOUND speaker,[116,16,119,16,120,16,122,16]
return


LosingSoundFx:
SOUND speaker,[114,16,112,16,116,16,111,16]
return


'-----Animations-----


Blocks_Animation:
select case Block_Lives
Case 3
lookup X,[56,112,224,112,56,28,14,7,14,28],datareg 'Animation for 3 blocks
lookup X,[3,2,1,2,3,4,5,6,5,4],Current_X 'Current Block Location on Grid
address = Block_Lvl : datareg = Datareg
gosub maxwrite
While Speed <= Set_Speed
Speed = Speed + 1
If Speed = Set_Speed then
X = X + 1
If X = 10 then
X = 0
endif
endif
wend
Speed = 0
case 2
lookup X,[24,48,96,192,96,48,24,12,6,3,6,12],datareg 'Animation for 2 blocks
lookup X,[4,3,2,1,2,3,4,5,6,7,6,5],Current_X 'Current Block Location on Grid
address = Block_Lvl : datareg = Datareg
gosub maxwrite
While Speed <= Set_Speed
Speed = Speed + 1
If Speed = Set_Speed then
X = X + 1
If X = 12 then
X = 0
endif
endif
wend
Speed = 0
Case 1
lookup X,[8,16,32,64,128,64,32,16,8,4,2,1,2,4],datareg 'Animation for 1 blocks
lookup X,[5,4,3,2,1,2,3,4,5,6,7,8,7,6],Current_X 'Current Block Location on Grid
address = Block_Lvl : datareg = Datareg
gosub maxwrite
While Speed <= Set_Speed
Speed = Speed + 1
If Speed = Set_Speed then
X = X + 1
If X = 14 then
X = 0
endif
endif
wend
Speed = 0
case 0
gosub LosingSoundFx
WinOrLose = 0
goto Main
end select 'This will end the select case program
return


Game_Speed: 'This sets Speed for each Level
select case Block_Lvl 'This will Set the Speed for each Lvl
case 8
Set_Speed = 1563 - (Game_Difficulty * 10) '400 Ms
Case 7
Set_Speed = 1563 - (Game_Difficulty * 10)'400 Ms
Case 6
Set_Speed = 1367 - (Game_Difficulty * 10)'350 Ms
Case 5
Set_Speed = 1367 - (Game_Difficulty * 10)'350 Ms
Case 4
Set_Speed = 1269 - (Game_Difficulty * 10)'325 Ms
Case 3
Set_Speed = 1172 - (Game_Difficulty * 10)'300 Ms
Case 2
Set_Speed = 1074 - (Game_Difficulty * 10)'275 Ms
Case 1
Set_Speed = 977 - (Game_Difficulty * 10)'250 Ms
end select
return


Next_Lvl:
select case Block_Lvl 'This will Select the case for each Lvl
case 8
onrightside = 0
LastLocation[8] = Current_X
Currentdatareg[8] = datareg
Block_lvl = Block_lvl - 1
gosub soundfx
PauseUS 1000 'Pause for 1 millisecond
Case 7
LastLocation[7] = Current_X
Currentdatareg[7] = datareg
Change = abs (LastLocation[8] - Current_X) 'Amount of change from stored location
if lastlocation[8] > lastlocation[7] then
Lastlocation[7] = lastlocation[7] + Change
endif
if lastlocation[8] < lastlocation[7] then
onrightside = 1
endif
Gosub Missed
Block_lvl = Block_lvl - 1
PauseUS 1000 'Pause for 1 millisecond
Case 6
onrightside = 0
LastLocation[6] = Current_X
Change = abs (LastLocation[7] - Current_X) 'Amount of change from stored location
if lastlocation[7] > lastlocation[6] then
Lastlocation[6] = lastlocation[6] + Change
endif
if lastlocation[7] < lastlocation[6] then
onrightside = 1
endif
Gosub Missed
Currentdatareg[6] = datareg
If Block_Lives > 2 then
BLock_Lives = Block_Lives - 1
endif
Block_lvl = Block_lvl - 1
PauseUS 1000 'Pause for 1 millisecond
Case 5
onrightside = 0
LastLocation[5] = Current_X
Currentdatareg[5] = datareg
Change = abs (LastLocation[6] - Current_X) 'Amount of change from stored location
if lastlocation[6] > lastlocation[5] then
Lastlocation[5] = lastlocation[5] + Change
endif
if lastlocation[6] < lastlocation[5] then
onrightside = 1
endif
if Change = 2 and lastlocation[6] < lastlocation[5] then
Change = 1
endif
Blink = Currentdatareg[5] & currentdatareg[6]
if Blink = Currentdatareg[5] then
Block_Lvl = Block_Lvl - 1
gosub soundfx
else
Gosub Missed
Block_lvl = Block_lvl - 1
PauseUS 1000 'Pause for 1 millisecond
endif
Case 4
onrightside = 0
LastLocation[4] = Current_X
Change = abs (LastLocation[5] - Current_X) 'Amount of change from stored location
if lastlocation[5] > lastlocation[4] then
Lastlocation[4] = lastlocation[4] + Change
endif
if lastlocation[5] < lastlocation[4] then
onrightside = 1
endif
Gosub Missed
Currentdatareg[4] = datareg
If Block_Lives > 1 then
BLock_Lives = Block_Lives - 1
endif
Block_lvl = Block_lvl - 1
PauseUS 1000 'Pause for 1 millisecond
Case 3
onrightside = 0
LastLocation[3] = Current_X
Currentdatareg[3] = datareg
Change = abs (LastLocation[4] - Current_X) 'Amount of change from stored location
if lastlocation[4] > lastlocation[3] then
Lastlocation[3] = lastlocation[3] + Change
endif
if lastlocation[4] < lastlocation[3] then
onrightside = 1
endif
Blink = Currentdatareg[3] & currentdatareg[4]
if Blink = Currentdatareg[3] then
Block_Lvl = Block_Lvl - 1
gosub soundfx
else
Gosub Missed
Block_lvl = Block_lvl - 1
PauseUS 1000 'Pause for 1 millisecond
endif
Case 2
onrightside = 0
LastLocation[2] = Current_X
Currentdatareg[2] = datareg
Change = abs (LastLocation[3] - Current_X) 'Amount of change from stored location
if lastlocation[3] > lastlocation[2] then
Lastlocation[2] = lastlocation[2] + Change
endif
if lastlocation[3] < lastlocation[2] then
onrightside = 1
endif
Gosub Missed
Block_lvl = Block_lvl - 1
PauseUS 1000 'Pause for 1 millisecond
Case 1
onrightside = 0
lastlocation[1] = Current_X
Currentdatareg[1] = datareg
Change = abs (LastLocation[2] - Current_X) 'Amount of change from stored location
if lastlocation[2] < lastlocation[1] then
onrightside = 1
endif
Gosub Missed
If Block_Lives >= 1 then
Gosub WinningSoundFx
Winorlose = 1
goto Main
endif
PauseUS 1000 'Pause for 1 millisecond
end select 'This will end the select case program
goto Blocks_Moving



Missed:
if change = 0 then
gosub soundfx
endif
if Change = 1 then
gosub MissedSoundFx
gosub Missed_Animation
Block_Lives = Block_Lives - 1
endif
if Change = 2 then
gosub MissedSoundFx
Gosub Missed_Animation
if Block_Lives = 1 then
Block_Lives = Block_Lives - 1
else
Block_Lives = Block_Lives - 2
endif
endif
if Change > 2 then
gosub MissedSoundFx
Gosub Missed_Animation
Block_Lives = 0
Winorlose = 0
goto Blocks_Animation
endif
return


Missed_Animation:
missblock = ncd datareg
if onrightside = 1 then
if Block_Lives > 2 then
if Change = 1 then
MissBlock = MissBlock - 2
endif
if change = 2 then
MissBlock = MissBlock - 1
endif
endif
if Block_Lives = 2 then
if change = 1 then
MissBlock = MissBlock - 1
endif
endif
endif
if Block_Lives > 2 then
lookup MissBlock,[0,1,2,4,8,16,32,64,128],mybit 'One Block Blinking
if change = 2 then
lookup MissBlock,[0,0,3,6,12,24,48,96,192],mybit 'Two Blocks Blinking
endif
if change > 2 then
lookup MissBlock,[0,0,0,7,14,28,56,112,224],mybit 'Three Blocks Blinking
endif
endif
if Block_Lives = 2 then
lookup MissBlock,[0,1,2,4,8,16,32,64,128],mybit 'One Block Blinking
if change = 2 then
lookup MissBlock,[0,0,3,6,12,24,48,96,192],mybit 'Two Blocks Blinking
endif
if change > 2 then
lookup MissBlock,[0,0,3,6,12,24,48,96,192],mybit 'Two Blocks Blinking
endif
endif
If Block_Lives = 1 then
lookup MissBlock,[0,1,2,4,8,16,32,64,128],mybit 'One Block Blinking
endif
address = block_lvl : datareg = datareg ^ mybit
gosub maxwrite
Pause 1000
address = block_lvl : datareg = datareg ^ mybit
gosub maxwrite
pause 1000
address = block_lvl : datareg = datareg ^ mybit
gosub maxwrite
Pause 1000
address = block_lvl : datareg = datareg ^ mybit
gosub maxwrite
Pause 1000
address = block_lvl : datareg = datareg ^ mybit
gosub maxwrite
return


MaxWrite:
shiftout DAT,CLK,MSBFIRST,[ADDRESS,DATAREG] 'Shift out the data to the 'MAX7219 'first the address, then data.
pulsout LOAD,1 'load the data into the MAX7219
return


Clean:
for Y = 1 to 8 'write $0F (blank) to all digits
ADDRESS = Y : DATAREG = $00 : gosub MaxWrite
next Y
return


Clean_Lvl: 'write $0F (blank) to certain Lvl
Y = 1
While Y < Block_Lvl
ADDRESS = Y : DATAREG = $00 : gosub MaxWrite
Y = Y + 1
wend
return


'----- Graphics ------


GameState_Winning: 'Displays a Won
for y = 0 to 7
lookup y,[0,36,36,0,0,66,60,0],datareg ' "Smile Face" Game Winning Image
address = y+1 : datareg = datareg
gosub maxwrite
next y
Return


GameState_Losing: 'Displays a Lost
for y = 0 to 7
lookup y,[0,36,36,0,0,60,66,0],datareg ' "Sad Face" Game Losing Image
address = y+1 : datareg = datareg
gosub maxwrite
next y
Return

Jerson
- 27th May 2014, 05:26
It would help you a lot if you posted a small video of your working project here.

aznmango
- 27th May 2014, 07:41
https://www.youtube.com/watch?v=qG_57GKHEQY

Jerson
- 27th May 2014, 08:40
Very nice.

aron11
- 22nd October 2014, 07:26
Thanks for share…

Ioannis
- 22nd October 2014, 11:07
Very nice project!

Ioannis

aznmango
- 23rd October 2014, 06:17
thanks... I'm in my last year for biomedical, so hopefully i can land a good job and start my electronics hobby again...

aznmango
- 25th March 2015, 19:57
Realized one day while working on a controller, if I added an opto-isolator to the schematic it would have gotten rid of the noise... but just a theory I had one day... graduating in may w00t!