Hardware components | ||||||
![]() |
| × | 1 | |||
| × | 2 | ||||
| × | 1 | ||||
| × | 1 | ||||
Software apps and online services | ||||||
![]() |
| |||||
Hand tools and fabrication machines | ||||||
![]() |
|
This is a game on a LCD display. The goal is to hit a target with arrows using a joystick, but the target is moving across the screen at a pace. You earn more points for hitting the target from farther back, but if you miss, the other player gets a point.
Or you can play against the computers artificial intelligence. In fact, the computer is hard to beat at level 4 of 10, if you’re an amateur like me.
I have been interested in LCD Display games before, as they have a lot of room for creating characters, numbers, and letters of all kinds. After enjoying making a project with a regular 16x2 display, I received a bigger display with an added bonus: an I2C module that allows me to control the LCD with just two pins. With the LiquidCrystal_I2C library by Marco Schwartz and Wire, the I2C library, I didn't have to move away from the LiquidCrystal functions that I already knew.
I implemented some concepts from my previous project, and I created a more sophisticated sketch for my first version. I think I called almost 50 global variables to make it work how I wanted. This code is my third version.
When you power up the arduino, it will ask you to play one player or two player. Follow the instructions on screen. Eventually, you will reach a screen that will allow you to move around using the joystick. Click your button to enter play. Once play starts, move using your joystick, and hit your button to fire. You will need to fire before the target is above you. It's three points for the bottom row, two points for the middle row, and one point for the top row. You starting position will alternate every round.
The artificial intelligence works like this: The computer will move every so often, to the right if it can, but up if you are to the right of it. When the target gets within range (i.e. the target will be either hit or missed by one) a random number is generated (based on the nearly 100% random millis() value), and the function returns a 1 or 0: a 1 will cause a hit, a 0 will cause a miss. So, the computer intelligence isn’t really that complicated.
If you happen to own a 20x4 display without an I2C module, just use the regular LiquidCrystal library, and initialize the display that way. I should also note that the address of your display is likely 0x27, but if it isn’t, it is probably 0x3F. There are plenty of places to find your I2C address, like this very helpful project by akshayjoseph666. Chapter 12 of the book, “Exploring Arduino,” by Jeremy Blum, was super helpful; it can be found online here.
I made an enclosure using a cardboard box, notecards, and duct tape:
As you can see, I also used a prototype shield so that I didn’t need to use a breadboard.
Have fun playing!
/*Targetry by TEWIS
*Started on 12/31/21
*Last modified last on 1/13/22
*Third version
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 20, 4);
const int jsx = A0;
const int jsy = A1;
const int jsb = 9;
const int jsx2 = A2;
const int jsy2 = A3;
const int jsb2 = 10;
const int module1 = 1;
const int module2 = 3;
const int target = 2;
const int arrow = 4;
const int hit = 5;
const int computer = 6;
int m1px = 9;
int m1py = 3;
int m2px = 10;
int m2py = 3;
int mcpx = 10;
int mcpy = 3;
int go1 = 1;
int go2 = 1;
int go3 = 1;
int go4 = 1;
int go5 = 1;
int go6 = 1;
int go7 = 1;
int go8 = 1;
int targpos1 = 20;
int p1ready = 0;
int p2ready = 0;
int play = 1;
int p1score = 0;
int p2score = 0;
int pcscore = 0;
int addto1;
int addto2;
int playto = 11;
int cpt = 1;
int cps = 1;
int ccs = 0;
int fspd = 6;
int spd;
bool rnd = false;
int switchspots = 0;
bool playmode = false;
int pmc = 0;
int comps = 5;
int compc = 0;
int compr;
int compd;
int fire, fireLater, movexp, movexm, moveyp, moveym;
int go = 0;
/*JOYSTICK DIAGRAM
* ________
* / 1 \ ^
* 5V- / 0 \ | 0
* GND- | 2 | |
* y- | 0 y 3 | | x
* x- | ------> | |
* button- \ / | 1023
* \________/
*/
/*To understand the joysticks, upload this code:
lcd.setCursor(0, 0);
lcd.print("x: ");
lcd.print(analogRead(jsx));
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print("y: ");
lcd.print(analogRead(jsy));
lcd.print(" ");
lcd.setCursor(0, 2);
lcd.print("button: ");
lcd.print(digitalRead(jsb));
lcd.print(" ");
*/
byte Module1[8] = {
B00100,
B01010,
B10001,
B01100,
B00100,
B00100,
B00100,
B01110,
};
byte Module2[8] = {
B00100,
B01010,
B10001,
B01100,
B00010,
B00100,
B01000,
B01110,
};
byte Computer[8] = {
B00100,
B01010,
B10001,
B00110,
B01000,
B01000,
B01000,
B00110,
};
byte Target[8] = {
B00000,
B00000,
B11111,
B10001,
B10101,
B10001,
B11111,
B00000,
};
byte Arrow[8] = {
B00100,
B01110,
B10101,
B00100,
B00100,
B01110,
B01010,
B01010,
};
byte Hit[8] = {
B11111,
B10001,
B10101,
B10101,
B10101,
B01110,
B01010,
B01010,
};
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.begin(20, 4);
pinMode(jsx, INPUT);
pinMode(jsy, INPUT);
pinMode(jsb, INPUT);
pinMode(jsx2, INPUT);
pinMode(jsy2, INPUT_PULLUP);
pinMode(jsb2, INPUT_PULLUP);
lcd.createChar(1, Module1);
lcd.createChar(2, Target);
lcd.createChar(3, Module2);
lcd.createChar(4, Arrow);
lcd.createChar(5, Hit);
lcd.createChar(6, Computer);
lcd.setCursor(0, 0);
lcd.print("\"Targetry\" by TEWIS");
lcd.setCursor(0, 1);
lcd.print("Created on 12/30/21.");
lcd.setCursor(0, 2)
lcd.print("Modified on 1/13/22")
lcd.setCursor(3, 3);
lcd.print("Third version");
delay(3000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Orient your joystick");
lcd.setCursor(0, 1);
lcd.print("so that the wires");
lcd.setCursor(0, 2);
lcd.print("are on the top.");
delay(5000);
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("Play with");
while(pmc == 0)
{
if(analogRead(jsy) > 923 || analogRead(jsy) < 100)
{
while(analogRead(jsy) > 923 || analogRead(jsy) < 100);
playmode = !playmode;
}
if(playmode == false)
{
lcd.setCursor(0, 2);
lcd.print("a friend ");
}
else if(playmode == true)
{
lcd.setCursor(0, 2);
lcd.print("the computer");
}
if(digitalRead(jsb) == 0)
{
lcd.setCursor(0, 3);
lcd.print("SELECTED");
pmc++;
}
}
delay(1000);
lcd.clear();
if(playmode == false)
{
lcd.setCursor(0, 0);
lcd.print("P1 is on the left.");
lcd.setCursor(0, 1);
lcd.print("P2 is on the right.");
lcd.setCursor(0, 2);
lcd.print("P1 character: ");
lcd.write(module1);
lcd.setCursor(0, 3);
lcd.print("P2 character: ");
lcd.write(module2);
delay(3000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("P1: ");
}
else if(playmode == true)
{
lcd.setCursor(0, 0);
lcd.print("Play with left");
lcd.setCursor(0, 1);
lcd.print("joystick");
lcd.setCursor(0, 2);
lcd.print("Your character: ");
lcd.write(module1);
lcd.setCursor(0, 3);
lcd.print("Computer character:");
lcd.write(computer);
delay(3000);
lcd.clear();
}
lcd.setCursor(4, 0);
lcd.print("Toggle settings");
lcd.setCursor(0, 1);
lcd.print("with joystick and");
lcd.setCursor(0, 2);
lcd.print("press down to select");
delay(5000);
lcd.clear();
while(cpt == 1)
{
lcd.setCursor(0, 0);
lcd.print("Play to: ");
lcd.print(playto);
lcd.print(" ");
if(analogRead(jsy) < 100 && playto < 99)
{
playto++;
while(analogRead(jsy) < 100);
}
else if(analogRead(jsy) > 923 && playto > 1)
{
playto--;
while(analogRead(jsy) > 923);
}
if(digitalRead(jsb) == 0)
{
cpt++;
}
}
lcd.setCursor(0, 1);
lcd.print("SELECTED");
delay(1000);
lcd.clear();
while(cps == 1)
{
spd = map(fspd, 1, 10, 10, 1);
lcd.setCursor(0, 0);
lcd.print("Play speed: ");
lcd.print(fspd);
lcd.print(" ");
lcd.setCursor(15, 0);
lcd.print(spd * 50);
lcd.print("ms ");
if(analogRead(jsy) < 100 && fspd < 10)
{
fspd++;
while(analogRead(jsy) < 100);
}
else if(analogRead(jsy) > 923 && fspd > 1)
{
fspd--;
while(analogRead(jsy) > 923);
}
if(digitalRead(jsb) == 0)
{
cps++;
}
}
spd = map(fspd, 1, 10, 10, 1);
lcd.setCursor(0, 1);
lcd.print("SELECTED");
delay(1000);
lcd.clear();
if(playmode == true)
{
while(ccs == 0)
{
lcd.setCursor(0, 0);
lcd.print("Comp. strength: ");
lcd.print(comps);
lcd.print(" ");
if(analogRead(jsy) < 100 && comps < 10)
{
while(analogRead(jsy) < 100);
comps++;
}
else if(analogRead(jsy) > 923 && comps > 1)
{
while(analogRead(jsy) > 923);
comps--;
}
if(digitalRead(jsb) == 0)
{
ccs++;
}
}
lcd.setCursor(0, 1);
lcd.print("SELECTED");
delay(1000);
lcd.clear();
int fcomps = map(comps, 1, 10, 2, 1);
compd = round((fspd * fcomps) * 0.10);
constrain(compd, 1, 30);
}
lcd.setCursor(0, 0);
lcd.print("Playing to ");
lcd.print(playto);
lcd.print(",");
lcd.setCursor(0, 1);
lcd.print("Play speed at ");
lcd.print(fspd);
lcd.setCursor(1, 2);
lcd.print("Confirm with press");
while(p1ready != 1 || p2ready != 1)
{
if(digitalRead(jsb) == 0)
{
lcd.setCursor(0, 3);
lcd.print("CONFIRMED");
p1ready = 1;
}
if(playmode == false)
{
if(digitalRead(jsb2) == 0)
{
lcd.setCursor(11, 3);
lcd.print("CONFIRMED");
p2ready = 1;
}
}
else if(playmode == true)
{
p2ready = 1;
}
}
p1ready = 0;
p2ready = 0;
delay(1000);
}
void loop() {
while(playmode == true)
{
lcd.clear();
if(p1score >= playto)
{
lcd.setCursor(5, 1);
lcd.print("YOU WIN!!!");
while(1);
}
if(pcscore >= playto)
{
lcd.setCursor(2, 1);
lcd.print("COMPUTER WINS!!!");
while(1);
}
if(switchspots == 0)
{
lcd.setCursor(0, 0);
lcd.print("Practice using your");
lcd.setCursor(0, 1);
lcd.print("joystick to move.");
lcd.setCursor(0, 2);
lcd.print("Press button to");
lcd.setCursor(0, 3);
lcd.print("continue.");
while(digitalRead(jsb) == 1);
switchspots = 1;
}
else if(1 == 0){}
else
{
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("Switch starting");
lcd.setCursor(6, 1);
lcd.print("points");
rnd = !rnd;
}
if(rnd == true)
{
lcd.setCursor(0, 2);
lcd.print("!!YOUR CHARACTER IS");
lcd.setCursor(1, 3);
lcd.print("NOW OPPOSITE YOU!!");
}
if(rnd == false)
{
m1px = 9;
mcpx = 10;
m1py = 3;
mcpy = 3;
}
else if(rnd == true)
{
m1px = 10;
mcpx = 9;
m1py = 3;
mcpy = 3;
}
delay(3000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("P1");
lcd.setCursor(15, 0);
lcd.print("COMP.");
lcd.setCursor(7, 0);
lcd.print(p1score);
lcd.setCursor(11, 0);
lcd.print(pcscore);
lcd.setCursor(m1px, m1py);
lcd.write(module1);
lcd.setCursor(mcpx, mcpy);
lcd.write(computer);
while(p1ready != 1)
{
if(digitalRead(jsb) == 0)
{
lcd.setCursor(0, 0);
lcd.print("READY");
p1ready = 1;
}
if(analogRead(jsx) > 923 && m1px != 0)
{
if(go1 == 1)
{
if(m1px - mcpx == 1 && m1py == mcpy){}
else if(1 == 0){}
else
{
lcd.setCursor(m1px, m1py);
lcd.print(" ");
m1px--;
lcd.setCursor(m1px, m1py);
lcd.write(module1);
go1 = 0;
}
}
}
else if(analogRead(jsx) < 100 && m1px != 19)
{
if(go2 == 1)
{
if(m1px - mcpx == -1 && m1py == mcpy){}
else if(1 == 0){}
else
{
lcd.setCursor(m1px, m1py);
lcd.print(" ");
m1px++;
lcd.setCursor(m1px, m1py);
lcd.write(module1);
go2 = 0;
}
}
}
else
{
go1 = 1;
go2 = 1;
}
if(analogRead(jsy) > 923 && m1py != 3)
{
if(go3 == 1)
{
if(m1py - mcpy == -1 && m1px == mcpx){}
else if(1 == 0){}
else
{
lcd.setCursor(m1px, m1py);
lcd.print(" ");
m1py++;
lcd.setCursor(m1px, m1py);
lcd.write(module1);
go3 = 0;
}
}
}
else if(analogRead(jsy) < 100 && m1py != 1)
{
if(go4 == 1)
{
if(m1py - mcpy == 1 && m1px == mcpx){}
else if(1 == 0){}
else
{
lcd.setCursor(m1px, m1py);
lcd.print(" ");
m1py--;
lcd.setCursor(m1px, m1py);
lcd.write(module1);
go4 = 0;
}
}
}
else
{
go3 = 1;
go4 = 1;
}
}
delay(1000);
lcd.clear();
p1ready = 0;
delay(1000);
lcd.setCursor(0, 3);
lcd.print(" ");
lcd.setCursor(0, 2);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(" ");
if(rnd == false)
{
m1px = 9;
mcpx = 10;
m1py = 3;
mcpy = 3;
}
else if(rnd == true)
{
m1px = 10;
mcpx = 9;
m1py = 3;
mcpy = 3;
}
lcd.setCursor(m1px, m1py);
lcd.write(module1);
lcd.setCursor(mcpx, mcpy);
lcd.write(computer);
play = 1;
while(play == 1)
{
for(int i = 19; i >= 0; i--)
{
targ1(i);
if(go == 0)
{
go = compd;
}
else if(go != 0)
{
go--;
}
if(go == 0)
{
if(mcpx - m1px == -1 && mcpy == m1py && mcpy != 1)
{
moveym = 1;
}
else if(mcpx != 19)
{
movexp = 1;
}
}
for(int j = 4; j >= 0; j--)
{
fire = 0;
if(mcpx == targpos1 - mcpy - 1)
{
if(comp(comps) == 0)
{
if(comp(comps) == 1)
{
fire = 1;
}
else if(comp(comps) == 0)
{
fireLater = 1;
}
}
}
else if(mcpx == targpos1 - mcpy && fireLater == 0)
{
if(comp(comps) == 1)
{
fire = 1;
}
}
if(mcpx == targpos1 - mcpy + 1 && fireLater == 1)
{
fire = 1;
}
if(digitalRead(jsb) == 0)
{
fire = 0;
fireLater = 0;
if(m1px == targpos1 - m1py)
{
if(m1py == 1)
{
p1score++;
addto1 = 1;
delay(spd * 12.5 * j);
lcd.setCursor(i, 0);
lcd.print(" ");
lcd.setCursor(m1px, m1py - 1);
lcd.write(hit);
delay(1000);
}
else if(m1py == 2)
{
p1score++;
p1score++;
addto1 = 2;
delay(spd * 12.5 * j);
i--;
targ1(i);
lcd.setCursor(m1px, m1py - 1);
lcd.write(arrow);
delay(spd * 50);
lcd.setCursor(i, 0);
lcd.print(" ");
lcd.setCursor(m1px, m1py - 1);
lcd.print(" ");
lcd.setCursor(m1px, m1py - 2);
lcd.write(hit);
delay(1000);
}
else if(m1py == 3)
{
p1score++;
p1score++;
p1score++;
addto1 = 3;
delay(spd * 12.5 * j);
i--;
targ1(i);
lcd.setCursor(m1px, m1py - 1);
lcd.write(arrow);
delay(spd * 50);
i--;
targ1(i);
lcd.setCursor(m1px, m1py - 1);
lcd.print(" ");
lcd.setCursor(m1px, m1py - 2);
lcd.write(arrow);
delay(spd * 50);
lcd.setCursor(i, 0);
lcd.print(" ");
lcd.setCursor(m1px, m1py - 2);
lcd.print(" ");
lcd.setCursor(m1px, m1py - 3);
lcd.write(hit);
delay(1000);
}
lcd.clear();
lcd.setCursor(5, 1);
lcd.print("YOU get +");
lcd.print(addto1);
addto1 = 0;
delay(3000);
lcd.clear();
i = 0;
j = 0;
play = 0;
}
else if(1 == 0){}
else
{
if(m1py == 1)
{
delay(spd * 12.5 * j);
if(i != 0)
{
i--;
}
else if(i == 0)
{
i = 19;
}
targ1(i);
lcd.setCursor(m1px, m1py - 1);
lcd.write(arrow);
lcd.setCursor(0, 3);
lcd.print("MISSED!");
delay(1000);
}
else if(m1py == 2)
{
delay(spd * 12.5 * j);
if(i != 0)
{
i--;
}
else if(i == 0)
{
i = 19;
}
targ1(i);
lcd.setCursor(m1px, m1py - 1);
lcd.write(arrow);
delay(spd * 50);
if(i != 0)
{
i--;
}
else if(i == 0)
{
i = 19;
}
targ1(i);
lcd.setCursor(m1px, m1py - 1);
lcd.print(" ");
lcd.setCursor(m1px, m1py - 2);
lcd.write(arrow);
lcd.setCursor(0, 3);
lcd.print("MISSED!");
delay(1000);
}
else if(m1py == 3)
{
delay(spd * 12.5 * j);
if(i != 0)
{
i--;
}
else if(i == 0)
{
i = 19;
}
targ1(i);
lcd.setCursor(m1px, m1py - 1);
lcd.write(arrow);
delay(spd * 50);
if(i != 0)
{
i--;
}
else if(i == 0)
{
i = 19;
}
targ1(i);
lcd.setCursor(m1px, m1py - 1);
lcd.print(" ");
lcd.setCursor(m1px, m1py - 2);
lcd.write(arrow);
delay(spd * 50);
if(i != 0)
{
i--;
}
else if(i == 0)
{
i = 19;
}
targ1(i);
lcd.setCursor(m1px, m1py - 2);
lcd.print(" ");
lcd.setCursor(m1px, m1py - 3);
lcd.write(arrow);
lcd.setCursor(0, 3);
lcd.print("MISSED!");
delay(1000);
}
lcd.clear();
lcd.setCursor(2, 1);
lcd.print("COMPUTER gets +1");
delay(3000);
lcd.clear();
pcscore++;
i = 0;
j = 0;
play = 0;
}
}
if(fire == 1)
{
if(mcpx == targpos1 - mcpy)
{
if(mcpy == 1)
{
pcscore++;
addto2 = 1;
delay(spd * 12.5 * j);
lcd.setCursor(i, 0);
lcd.print(" ");
lcd.setCursor(mcpx, mcpy - 1);
lcd.write(hit);
delay(1000);
}
else if(mcpy == 2)
{
pcscore++;
pcscore++;
addto2 = 2;
delay(spd * 12.5 * j);
i--;
targ1(i);
lcd.setCursor(mcpx, mcpy - 1);
lcd.write(arrow);
delay(spd * 50);
lcd.setCursor(i, 0);
lcd.print(" ");
lcd.setCursor(mcpx, mcpy - 1);
lcd.print(" ");
lcd.setCursor(mcpx, mcpy - 2);
lcd.write(hit);
delay(1000);
}
else if(mcpy == 3)
{
pcscore++;
pcscore++;
pcscore++;
addto2 = 3;
delay(spd * 12.5 * j);
i--;
targ1(i);
lcd.setCursor(mcpx, mcpy - 1);
lcd.write(arrow);
delay(spd * 50);
i--;
targ1(i);
lcd.setCursor(mcpx, mcpy - 1);
lcd.print(" ");
lcd.setCursor(mcpx, mcpy - 2);
lcd.write(arrow);
delay(spd * 50);
lcd.setCursor(i, 0);
lcd.print(" ");
lcd.setCursor(mcpx, mcpy - 2);
lcd.print(" ");
lcd.setCursor(mcpx, mcpy - 3);
lcd.write(hit);
delay(1000);
}
lcd.clear();
lcd.setCursor(2, 1);
lcd.print("COMPUTER gets +");
lcd.print(addto2);
addto2 = 0;
delay(3000);
lcd.clear();
i = 0;
j = 0;
play = 0;
}
else if(1 == 0){}
else
{
if(mcpy == 1)
{
delay(spd * 12.5 * j);
if(i != 0)
{
i--;
}
else if(i == 0)
{
i = 19;
}
targ1(i);
lcd.setCursor(mcpx, mcpy - 1);
lcd.write(arrow);
lcd.setCursor(0, 3);
lcd.print("MISSED!");
delay(1000);
}
else if(mcpy == 2)
{
delay(spd * 12.5 * j);
if(i != 0)
{
i--;
}
else if(i == 0)
{
i = 19;
}
targ1(i);
lcd.setCursor(mcpx, mcpy - 1);
lcd.write(arrow);
delay(spd * 50);
if(i != 0)
{
i--;
}
else if(i == 0)
{
i = 19;
}
targ1(i);
lcd.setCursor(mcpx, mcpy - 1);
lcd.print(" ");
lcd.setCursor(mcpx, mcpy - 2);
lcd.write(arrow);
lcd.setCursor(0, 3);
lcd.print("MISSED!");
delay(1000);
}
else if(mcpy == 3)
{
delay(spd * 12.5 * j);
if(i != 0)
{
i--;
}
else if(i == 0)
{
i = 19;
}
targ1(i);
lcd.setCursor(mcpx, mcpy - 1);
lcd.write(arrow);
delay(spd * 50);
if(i != 0)
{
i--;
}
else if(i == 0)
{
i = 19;
}
targ1(i);
lcd.setCursor(mcpx, mcpy - 1);
lcd.print(" ");
lcd.setCursor(mcpx, mcpy - 2);
lcd.write(arrow);
delay(spd * 50);
if(i != 0)
{
i--;
}
else if(i == 0)
{
i = 19;
}
targ1(i);
lcd.setCursor(mcpx, mcpy - 2);
lcd.print(" ");
lcd.setCursor(mcpx, mcpy - 3);
lcd.write(arrow);
lcd.setCursor(0, 3);
lcd.print("MISSED!");
delay(1000);
}
lcd.clear();
lcd.setCursor(5, 1);
lcd.print("YOU get +1");
delay(3000);
lcd.clear();
p1score++;
i = 0;
j = 0;
play = 0;
}
}
if(analogRead(jsx) > 923 && m1px != 0)
{
if(go1 == 1)
{
if(m1px - mcpx == 1 && m1py == mcpy){}
else if(1 == 0){}
else
{
lcd.setCursor(m1px, m1py);
lcd.print(" ");
m1px--;
lcd.setCursor(m1px, m1py);
lcd.write(module1);
go1 = 0;
}
}
}
else if(analogRead(jsx) < 100 && m1px != 19)
{
if(go2 == 1)
{
if(m1px - mcpx == -1 && m1py == mcpy){}
else if(1 == 0){}
else
{
lcd.setCursor(m1px, m1py);
lcd.print(" ");
m1px++;
lcd.setCursor(m1px, m1py);
lcd.write(module1);
go2 = 0;
}
}
}
else
{
go1 = 1;
go2 = 1;
}
if(movexm == 1)
{
if(mcpx - m1px == 1 && mcpy == m1py){}
else if(1 == 0){}
else
{
lcd.setCursor(mcpx, mcpy);
lcd.print(" ");
mcpx--;
lcd.setCursor(mcpx, mcpy);
lcd.write(computer);
}
}
else if(movexp == 1)
{
if(mcpx - m1px == -1 && mcpy == m1py){}
else if(1 == 0){}
else
{
lcd.setCursor(mcpx, mcpy);
lcd.print(" ");
mcpx++;
lcd.setCursor(mcpx, mcpy);
lcd.write(computer);
}
}
if(analogRead(jsy) > 923 && m1py != 3)
{
if(go3 == 1)
{
if(m1py - mcpy == -1 && m1px == mcpx){}
else if(1 == 0){}
else
{
lcd.setCursor(m1px, m1py);
lcd.print(" ");
m1py++;
lcd.setCursor(m1px, m1py);
lcd.write(module1);
go3 = 0;
}
}
}
else if(analogRead(jsy) < 100 && m1py != 1)
{
if(go4 == 1)
{
if(m1py - mcpy == 1 && m1px == mcpx){}
else if(1 == 0){}
else
{
lcd.setCursor(m1px, m1py);
lcd.print(" ");
m1py--;
lcd.setCursor(m1px, m1py);
lcd.write(module1);
go4 = 0;
}
}
}
else
{
go3 = 1;
go4 = 1;
}
if(moveyp == 1)
{
if(mcpy - m1py == -1 && mcpx == m1px){}
else if(1 == 0){}
else
{
lcd.setCursor(mcpx, mcpy);
lcd.print(" ");
mcpy++;
lcd.setCursor(mcpx, mcpy);
lcd.write(computer);
}
}
else if(moveym)
{
if(mcpy - m1py == 1 && mcpx == m1px){}
else if(1 == 0){}
else
{
lcd.setCursor(mcpx, mcpy);
lcd.print(" ");
mcpy--;
lcd.setCursor(mcpx, mcpy);
lcd.write(computer);
}
}
delay(spd * 12.5);
movexp = 0;
movexm = 0;
moveyp = 0;
moveym = 0;
}
}
}
}
while(playmode == false)
{
lcd.clear();
if(p1score >= playto)
{
lcd.setCursor(5, 1);
lcd.print("P1 WINS!!!");
while(1);
}
if(p2score >= playto)
{
lcd.setCursor(5, 1);
lcd.print("P2 WINS!!!");
while(1);
}
if(switchspots == 0)
{
lcd.setCursor(0, 0);
lcd.print("Practice using your");
lcd.setCursor(0, 1);
lcd.print("joystick to move.");
lcd.setCursor(0, 2);
lcd.print("Press button to");
lcd.setCursor(0, 3);
lcd.print("continue.");
delay(3000);
switchspots = 1;
}
else if(1 == 0){}
else
{
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("Switch starting");
lcd.setCursor(6, 1);
lcd.print("points");
rnd = !rnd;
}
if(rnd == true)
{
lcd.setCursor(0, 2);
lcd.print("!!YOUR CHARACTER IS");
lcd.setCursor(1, 3);
lcd.print("NOW OPPOSITE YOU!!");
}
if(rnd == false)
{
m1px = 9;
m2px = 10;
m1py = 3;
m2py = 3;
}
else if(rnd == true)
{
m1px = 10;
m2px = 9;
m1py = 3;
m2py = 3;
}
delay(3000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("P1");
lcd.setCursor(18, 0);
lcd.print("P2");
lcd.setCursor(7, 0);
lcd.print(p1score);
lcd.setCursor(11, 0);
lcd.print(p2score);
lcd.setCursor(m1px, m1py);
lcd.write(module1);
lcd.setCursor(m2px, m2py);
lcd.write(module2);
while(p1ready != 1 || p2ready != 1)
{
if(digitalRead(jsb) == 0)
{
lcd.setCursor(0, 0);
lcd.print("READY");
p1ready = 1;
}
if(digitalRead(jsb2) == 0)
{
lcd.setCursor(15, 0);
lcd.print("READY");
p2ready = 1;
}
if(analogRead(jsx) > 923 && m1px != 0)
{
if(go1 == 1)
{
if(m1px - m2px == 1 && m1py == m2py){}
else if(1 == 0){}
else
{
lcd.setCursor(m1px, m1py);
lcd.print(" ");
m1px--;
lcd.setCursor(m1px, m1py);
lcd.write(module1);
go1 = 0;
}
}
}
else if(analogRead(jsx) < 100 && m1px != 19)
{
if(go2 == 1)
{
if(m1px - m2px == -1 && m1py == m2py){}
else if(1 == 0){}
else
{
lcd.setCursor(m1px, m1py);
lcd.print(" ");
m1px++;
lcd.setCursor(m1px, m1py);
lcd.write(module1);
go2 = 0;
}
}
}
else
{
go1 = 1;
go2 = 1;
}
if(analogRead(jsx2) > 923 && m2px != 0)
{
if(go3 == 1)
{
if(m2px - m1px == 1 && m2py == m1py){}
else if(1 == 0){}
else
{
lcd.setCursor(m2px, m2py);
lcd.print(" ");
m2px--;
lcd.setCursor(m2px, m2py);
lcd.write(module2);
go3 = 0;
}
}
}
else if(analogRead(jsx2) < 100 && m2px != 19)
{
if(go4 == 1)
{
if(m2px - m1px == -1 && m2py == m1py){}
else if(1 == 0){}
else
{
lcd.setCursor(m2px, m2py);
lcd.print(" ");
m2px++;
lcd.setCursor(m2px, m2py);
lcd.write(module2);
go4 = 0;
}
}
}
else
{
go3 = 1;
go4 = 1;
}
if(analogRead(jsy) > 923 && m1py != 3)
{
if(go5 == 1)
{
if(m1py - m2py == -1 && m1px == m2px){}
else if(1 == 0){}
else
{
lcd.setCursor(m1px, m1py);
lcd.print(" ");
m1py++;
lcd.setCursor(m1px, m1py);
lcd.write(module1);
go5 = 0;
}
}
}
else if(analogRead(jsy) < 100 && m1py != 1)
{
if(go6 == 1)
{
if(m1py - m2py == 1 && m1px == m2px){}
else if(1 == 0){}
else
{
lcd.setCursor(m1px, m1py);
lcd.print(" ");
m1py--;
lcd.setCursor(m1px, m1py);
lcd.write(module1);
go6 = 0;
}
}
}
else
{
go5 = 1;
go6 = 1;
}
if(analogRead(jsy2) > 923 && m2py != 3)
{
if(go7 == 1)
{
if(m2py - m1py == -1 && m2px == m1px){}
else if(1 == 0){}
else
{
lcd.setCursor(m2px, m2py);
lcd.print(" ");
m2py++;
lcd.setCursor(m2px, m2py);
lcd.write(module2);
go7 = 0;
}
}
}
else if(analogRead(jsy2) < 100 && m2py != 1)
{
if(go8 == 1)
{
if(m2py - m1py == 1 && m2px == m1px){}
else if(1 == 0){}
else
{
lcd.setCursor(m2px, m2py);
lcd.print(" ");
m2py--;
lcd.setCursor(m2px, m2py);
lcd.write(module2);
go8 = 0;
}
}
}
else
{
go7 = 1;
go8 = 1;
}
}
delay(1000);
lcd.clear();
p1ready = 0;
p2ready = 0;
delay(1000);
lcd.setCursor(0, 3);
lcd.print(" ");
lcd.setCursor(0, 2);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(" ");
if(rnd == false)
{
m1px = 9;
m2px = 10;
m1py = 3;
m2py = 3;
}
else if(rnd == true)
{
m1px = 10;
m2px = 9;
m1py = 3;
m2py = 3;
}
lcd.setCursor(m1px, m1py);
lcd.write(module1);
lcd.setCursor(m2px, m2py);
lcd.write(module2);
play = 1;
while(play == 1)
{
for(int i = 19; i >= 0; i--)
{
targ1(i);
for(int j = 4; j >= 0; j--)
{
if(digitalRead(jsb) == 0)
{
if(m1px == targpos1 - m1py)
{
if(m1py == 1)
{
p1score++;
addto1 = 1;
delay(spd * 12.5 * j);
lcd.setCursor(i, 0);
lcd.print(" ");
lcd.setCursor(m1px, m1py - 1);
lcd.write(hit);
delay(1000);
}
else if(m1py == 2)
{
p1score++;
p1score++;
addto1 = 2;
delay(spd * 12.5 * j);
i--;
targ1(i);
lcd.setCursor(m1px, m1py - 1);
lcd.write(arrow);
delay(spd * 50);
lcd.setCursor(i, 0);
lcd.print(" ");
lcd.setCursor(m1px, m1py - 1);
lcd.print(" ");
lcd.setCursor(m1px, m1py - 2);
lcd.write(hit);
delay(1000);
}
else if(m1py == 3)
{
p1score++;
p1score++;
p1score++;
addto1 = 3;
delay(spd * 12.5 * j);
i--;
targ1(i);
lcd.setCursor(m1px, m1py - 1);
lcd.write(arrow);
delay(spd * 50);
i--;
targ1(i);
lcd.setCursor(m1px, m1py - 1);
lcd.print(" ");
lcd.setCursor(m1px, m1py - 2);
lcd.write(arrow);
delay(spd * 50);
lcd.setCursor(i, 0);
lcd.print(" ");
lcd.setCursor(m1px, m1py - 2);
lcd.print(" ");
lcd.setCursor(m1px, m1py - 3);
lcd.write(hit);
delay(1000);
}
if(m1px == m2px && m1px > m2px)
{
addto1 = addto1 + 5;
p1score = p1score + 5;
}
lcd.clear();
lcd.setCursor(5, 1);
lcd.print("P1 gets +");
lcd.print(addto1);
addto1 = 0;
delay(3000);
lcd.clear();
i = 0;
j = 0;
m2px = 9;
m2py = 3;
m2px = 10;
m2py = 3;
play = 0;
}
else if(1 == 0){}
else
{
if(m1py == 1)
{
delay(spd * 12.5 * j);
if(i != 0)
{
i--;
}
else if(i == 0)
{
i = 19;
}
targ1(i);
lcd.setCursor(m1px, m1py - 1);
lcd.write(arrow);
lcd.setCursor(0, 3);
lcd.print("MISSED!");
delay(1000);
}
else if(m1py == 2)
{
delay(spd * 12.5 * j);
if(i != 0)
{
i--;
}
else if(i == 0)
{
i = 19;
}
targ1(i);
lcd.setCursor(m1px, m1py - 1);
lcd.write(arrow);
delay(spd * 50);
if(i != 0)
{
i--;
}
else if(i == 0)
{
i = 19;
}
targ1(i);
lcd.setCursor(m1px, m1py - 1);
lcd.print(" ");
lcd.setCursor(m1px, m1py - 2);
lcd.write(arrow);
lcd.setCursor(0, 3);
lcd.print("MISSED!");
delay(1000);
}
else if(m1py == 3)
{
delay(spd * 12.5 * j);
if(i != 0)
{
i--;
}
else if(i == 0)
{
i = 19;
}
targ1(i);
lcd.setCursor(m1px, m1py - 1);
lcd.write(arrow);
delay(spd * 50);
if(i != 0)
{
i--;
}
else if(i == 0)
{
i = 19;
}
targ1(i);
lcd.setCursor(m1px, m1py - 1);
lcd.print(" ");
lcd.setCursor(m1px, m1py - 2);
lcd.write(arrow);
delay(spd * 50);
if(i != 0)
{
i--;
}
else if(i == 0)
{
i = 19;
}
targ1(i);
lcd.setCursor(m1px, m1py - 2);
lcd.print(" ");
lcd.setCursor(m1px, m1py - 3);
lcd.write(arrow);
lcd.setCursor(0, 3);
lcd.print("MISSED!");
delay(1000);
}
lcd.clear();
lcd.setCursor(5, 1);
lcd.print("P2 gets +1");
delay(3000);
lcd.clear();
p2score++;
i = 0;
j = 0;
m2px = 9;
m2py = 3;
m2px = 10;
m2py = 3;
play = 0;
}
}
if(digitalRead(jsb2) == 0)
{
if(m2px == targpos1 - m2py)
{
if(m2py == 1)
{
p2score++;
addto2 = 1;
delay(spd * 12.5 * j);
lcd.setCursor(i, 0);
lcd.print(" ");
lcd.setCursor(m2px, m2py - 1);
lcd.write(hit);
delay(1000);
}
else if(m2py == 2)
{
p2score++;
p2score++;
addto2 = 2;
delay(spd * 12.5 * j);
i--;
targ1(i);
lcd.setCursor(m2px, m2py - 1);
lcd.write(arrow);
delay(spd * 50);
lcd.setCursor(i, 0);
lcd.print(" ");
lcd.setCursor(m2px, m2py - 1);
lcd.print(" ");
lcd.setCursor(m2px, m2py - 2);
lcd.write(hit);
delay(1000);
}
else if(m2py == 3)
{
p2score++;
p2score++;
p2score++;
addto2 = 3;
delay(spd * 12.5 * j);
i--;
targ1(i);
lcd.setCursor(m2px, m2py - 1);
lcd.write(arrow);
delay(spd * 50);
i--;
targ1(i);
lcd.setCursor(m2px, m2py - 1);
lcd.print(" ");
lcd.setCursor(m2px, m2py - 2);
lcd.write(arrow);
delay(spd * 50);
lcd.setCursor(i, 0);
lcd.print(" ");
lcd.setCursor(m2px, m2py - 2);
lcd.print(" ");
lcd.setCursor(m2px, m2py - 3);
lcd.write(hit);
delay(1000);
}
if(m1px == m2px && m2px > m1px)
{
addto2 = addto2 + 5;
p2score = p2score + 5;
}
lcd.clear();
lcd.setCursor(5, 1);
lcd.print("P2 gets +");
lcd.print(addto2);
addto2 = 0;
delay(3000);
lcd.clear();
i = 0;
j = 0;
m2px = 9;
m2py = 3;
m2px = 10;
m2py = 3;
play = 0;
}
else if(1 == 0){}
else
{
if(m2py == 1)
{
delay(spd * 12.5 * j);
if(i != 0)
{
i--;
}
else if(i == 0)
{
i = 19;
}
targ1(i);
lcd.setCursor(m2px, m2py - 1);
lcd.write(arrow);
lcd.setCursor(0, 3);
lcd.print("MISSED!");
delay(1000);
}
else if(m2py == 2)
{
delay(spd * 12.5 * j);
if(i != 0)
{
i--;
}
else if(i == 0)
{
i = 19;
}
targ1(i);
lcd.setCursor(m2px, m2py - 1);
lcd.write(arrow);
delay(spd * 50);
if(i != 0)
{
i--;
}
else if(i == 0)
{
i = 19;
}
targ1(i);
lcd.setCursor(m2px, m2py - 1);
lcd.print(" ");
lcd.setCursor(m2px, m2py - 2);
lcd.write(arrow);
lcd.setCursor(0, 3);
lcd.print("MISSED!");
delay(1000);
}
else if(m2py == 3)
{
delay(spd * 12.5 * j);
if(i != 0)
{
i--;
}
else if(i == 0)
{
i = 19;
}
targ1(i);
lcd.setCursor(m2px, m2py - 1);
lcd.write(arrow);
delay(spd * 50);
if(i != 0)
{
i--;
}
else if(i == 0)
{
i = 19;
}
targ1(i);
lcd.setCursor(m2px, m2py - 1);
lcd.print(" ");
lcd.setCursor(m2px, m2py - 2);
lcd.write(arrow);
delay(spd * 50);
if(i != 0)
{
i--;
}
else if(i == 0)
{
i = 19;
}
targ1(i);
lcd.setCursor(m2px, m2py - 2);
lcd.print(" ");
lcd.setCursor(m2px, m2py - 3);
lcd.write(arrow);
lcd.setCursor(0, 3);
lcd.print("MISSED!");
delay(1000);
}
lcd.clear();
lcd.setCursor(5, 1);
lcd.print("P1 gets +1");
delay(3000);
lcd.clear();
p1score++;
i = 0;
j = 0;
m2px = 9;
m2py = 3;
m2px = 10;
m2py = 3;
play = 0;
}
}
if(analogRead(jsx) > 923 && m1px != 0)
{
if(go1 == 1)
{
if(m1px - m2px == 1 && m1py == m2py){}
else if(1 == 0){}
else
{
lcd.setCursor(m1px, m1py);
lcd.print(" ");
m1px--;
lcd.setCursor(m1px, m1py);
lcd.write(module1);
go1 = 0;
}
}
}
else if(analogRead(jsx) < 100 && m1px != 19)
{
if(go2 == 1)
{
if(m1px - m2px == -1 && m1py == m2py){}
else if(1 == 0){}
else
{
lcd.setCursor(m1px, m1py);
lcd.print(" ");
m1px++;
lcd.setCursor(m1px, m1py);
lcd.write(module1);
go2 = 0;
}
}
}
else
{
go1 = 1;
go2 = 1;
}
if(analogRead(jsx2) > 923 && m2px != 0)
{
if(go3 == 1)
{
if(m2px - m1px == 1 && m2py == m1py){}
else if(1 == 0){}
else
{
lcd.setCursor(m2px, m2py);
lcd.print(" ");
m2px--;
lcd.setCursor(m2px, m2py);
lcd.write(module2);
go3 = 0;
}
}
}
else if(analogRead(jsx2) < 100 && m2px != 19)
{
if(go4 == 1)
{
if(m2px - m1px == -1 && m2py == m1py){}
else if(1 == 0){}
else
{
lcd.setCursor(m2px, m2py);
lcd.print(" ");
m2px++;
lcd.setCursor(m2px, m2py);
lcd.write(module2);
go4 = 0;
}
}
}
else
{
go3 = 1;
go4 = 1;
}
if(analogRead(jsy) > 923 && m1py != 3)
{
if(go5 == 1)
{
if(m1py - m2py == -1 && m1px == m2px){}
else if(1 == 0){}
else
{
lcd.setCursor(m1px, m1py);
lcd.print(" ");
m1py++;
lcd.setCursor(m1px, m1py);
lcd.write(module1);
go5 = 0;
}
}
}
else if(analogRead(jsy) < 100 && m1py != 1)
{
if(go6 == 1)
{
if(m1py - m2py == 1 && m1px == m2px){}
else if(1 == 0){}
else
{
lcd.setCursor(m1px, m1py);
lcd.print(" ");
m1py--;
lcd.setCursor(m1px, m1py);
lcd.write(module1);
go6 = 0;
}
}
}
else
{
go5 = 1;
go6 = 1;
}
if(analogRead(jsy2) > 923 && m2py != 3)
{
if(go7 == 1)
{
if(m2py - m1py == -1 && m2px == m1px){}
else if(1 == 0){}
else
{
lcd.setCursor(m2px, m2py);
lcd.print(" ");
m2py++;
lcd.setCursor(m2px, m2py);
lcd.write(module2);
go7 = 0;
}
}
}
else if(analogRead(jsy2) < 100 && m2py != 1)
{
if(go8 == 1)
{
if(m2py - m1py == 1 && m2px == m1px){}
else if(1 == 0){}
else
{
lcd.setCursor(m2px, m2py);
lcd.print(" ");
m2py--;
lcd.setCursor(m2px, m2py);
lcd.write(module2);
go8 = 0;
}
}
}
else
{
go7 = 1;
go8 = 1;
}
delay(spd * 12.5);
}
}
}
}
}
void targ1(int x)
{
lcd.setCursor(x, 0);
lcd.write(target);
lcd.print(" ");
targpos1 = x;
if(x == 19)
{
lcd.setCursor(0, 0);
lcd.print(" ");
}
}
int comp(int f)
{
randomSeed(millis());
if(random(1, 11) <= comps)
{
return 1;
}
else if(1 == 0){}
else
{
return 0;
}
}
Comments