Anda di halaman 1dari 8

const

//Pin
const
//Pin
const

int led_latch = 6;
connected to SH_CP of 74HC595
int led_clock = 5;
connected to DS of 74HC595
int led_data = 7;

const int joy_h = 0; // analog


const int joy_v = 1; // analog
//const int SEL = 2; // digital
void setup() {
//pinMode(SEL, INPUT);
//digitalWrite(SEL, HIGH);
pinMode(led_latch, OUTPUT);
pinMode(led_clock, OUTPUT);
pinMode(led_data, OUTPUT);
Serial.begin(9600);
}
int curry = 0, currx = 4; // current piece position !top corner
int movy = 0, movx = 0;
int joy_input_x, joy_input_y;
int tetrismap[8][8];
void loop() {
printMap();
// check if score changes
if (score()) {
advanceMap();
//
advanceMap();
}
// reset movement delta
movy = 0;
movx = 0;
// set joystick input read
joy_input_y = analogRead(joy_h);
joy_input_x = analogRead(joy_v);
// generating movement on x
if (joy_input_x < 250 && currx < 7)
movx++;
else if (joy_input_x > 750 && currx > 0)
movx--;
// generate movement on y
if (curry < 7 && tetrismap[currx][curry + 1] == 0)
movy++;

// validate/commit movement
if (tetrismap[currx][curry + 1] == 1 || curry == 7)
newPiece();
else
movePiece(movx, movy);

// draw
drawMap();
delay(500);
}
void newPiece() {
curry = 0;
currx = 4;
}
void movePiece(int delx, int dely) {
tetrismap[currx][curry] = 0;
tetrismap[currx + delx][curry + dely] = 1;
currx = currx + delx;
curry = curry + dely;
}
void drawLed(int y, int x) {
byte rowBitsToSend = 0;
byte columnBitsToSend = 0;
digitalWrite(led_latch, LOW);
bitWrite(columnBitsToSend, y, HIGH); // Gives us 000000001
shiftOut(led_data, led_clock, MSBFIRST, columnBitsToSend); // Send the byte to
the shift register which passes it to the second shift register
bitWrite(rowBitsToSend, x, HIGH); // Gives us 00000001
shiftOut(led_data, led_clock, MSBFIRST, rowBitsToSend); // Send the byte to th
e shift register
digitalWrite(led_latch, HIGH); // Tell the shift register we are done sending
data so it can enable the outputs
}
void clearLed(int y, int x) {
byte rowBitsToSend = 0;
byte columnBitsToSend = 0;
digitalWrite(led_latch, LOW);
bitWrite(columnBitsToSend, y, LOW); // Gives us 000000001
shiftOut(led_data, led_clock, MSBFIRST, columnBitsToSend); // Send the byte to
the shift register which passes it to the second shift register
bitWrite(rowBitsToSend, x, LOW); // Gives us 00000001
shiftOut(led_data, led_clock, MSBFIRST, rowBitsToSend); // Send the byte to th
e shift register
digitalWrite(led_latch, HIGH); // Tell the shift register we are done sending
data so it can enable the outputs
}
void drawMap() {
int i, j;
for (i = 0; i < 8; i++)
for (j = 0; j < 8; j++) {
if (tetrismap[i][j] == 1)
drawLed(i, j);
//else if (tetrismap[i][j] == 0)
//clearLed(i,j);
}
}
void printMap() {
int i, j;

Serial.print("\n");
for (i = 0; i < 8; i++) {
Serial.print("\ni");
for (j = 0; j < 8; j++) {
Serial.print(tetrismap[i][j], DEC);
Serial.print(" ");
}
}
Serial.print("\n");
}
void advanceMap() {
int i, j;
//Serial.print("Advancing map down...");
for (i = 0; i < 8; i++){
printMap();
for (j = 6; j > 0; j--)
tetrismap[i][j + 1] = tetrismap[i][j]; // move above lines down
}
//Serial.print("...done!");
}
int score() {
int i, j;
for (i = 0; i < 8; i++)
for (j = 7; j < 8; j++)
if (tetrismap[i][j] == 0)
return 0;
//Serial.print("Score! ");
return 1;
}
clone
const
//Pin
const
//Pin
const

this paste RAW Paste Data


int led_latch = 6;
connected to SH_CP of 74HC595
int led_clock = 5;
connected to DS of 74HC595
int led_data = 7;

const int joy_h = 0; // analog


const int joy_v = 1; // analog
//const int SEL = 2; // digital
void setup() {
//pinMode(SEL, INPUT);
//digitalWrite(SEL, HIGH);
pinMode(led_latch, OUTPUT);
pinMode(led_clock, OUTPUT);
pinMode(led_data, OUTPUT);
Serial.begin(9600);
}
int curry = 0, currx = 4; // current piece position !top corner
int movy = 0, movx = 0;
int joy_input_x, joy_input_y;
int tetrismap[8][8];

void loop() {
printMap();
// check if score changes
if (score()) {
advanceMap();
//
advanceMap();
}
// reset movement delta
movy = 0;
movx = 0;
// set joystick input read
joy_input_y = analogRead(joy_h);
joy_input_x = analogRead(joy_v);
// generating movement on x
if (joy_input_x < 250 && currx < 7)
movx++;
else if (joy_input_x > 750 && currx > 0)
movx--;
// generate movement on y
if (curry < 7 && tetrismap[currx][curry + 1] == 0)
movy++;

// validate/commit movement
if (tetrismap[currx][curry + 1] == 1 || curry == 7)
newPiece();
else
movePiece(movx, movy);
// draw
drawMap();
delay(500);
}
void newPiece() {
curry = 0;
currx = 4;
}
void movePiece(int delx, int dely) {
tetrismap[currx][curry] = 0;
tetrismap[currx + delx][curry + dely] = 1;
currx = currx + delx;
curry = curry + dely;
}
void drawLed(int y, int x) {
byte rowBitsToSend = 0;
byte columnBitsToSend = 0;
digitalWrite(led_latch, LOW);
bitWrite(columnBitsToSend, y, HIGH); // Gives us 000000001
shiftOut(led_data, led_clock, MSBFIRST, columnBitsToSend); // Send the byte to
the shift register which passes it to the second shift register

bitWrite(rowBitsToSend, x, HIGH); // Gives us 00000001


shiftOut(led_data, led_clock, MSBFIRST, rowBitsToSend); // Send the byte to th
e shift register
digitalWrite(led_latch, HIGH); // Tell the shift register we are done sending
data so it can enable the outputs
}
void clearLed(int y, int x) {
byte rowBitsToSend = 0;
byte columnBitsToSend = 0;
digitalWrite(led_latch, LOW);
bitWrite(columnBitsToSend, y, LOW); // Gives us 000000001
shiftOut(led_data, led_clock, MSBFIRST, columnBitsToSend); // Send the byte to
the shift register which passes it to the second shift register
bitWrite(rowBitsToSend, x, LOW); // Gives us 00000001
shiftOut(led_data, led_clock, MSBFIRST, rowBitsToSend); // Send the byte to th
e shift register
digitalWrite(led_latch, HIGH); // Tell the shift register we are done sending
data so it can enable the outputs
}
void drawMap() {
int i, j;
for (i = 0; i < 8; i++)
for (j = 0; j < 8; j++) {
if (tetrismap[i][j] == 1)
drawLed(i, j);
//else if (tetrismap[i][j] == 0)
//clearLed(i,j);
}
}
void printMap() {
int i, j;
Serial.print("\n");
for (i = 0; i < 8; i++) {
Serial.print("\ni");
for (j = 0; j < 8; j++) {
Serial.print(tetrismap[i][j], DEC);
Serial.print(" ");
}
}
Serial.print("\n");
}
void advanceMap() {
int i, j;
//Serial.print("Advancing map down...");
for (i = 0; i < 8; i++){
printMap();
for (j = 6; j > 0; j--)
tetrismap[i][j + 1] = tetrismap[i][j]; // move above lines down
}
//Serial.print("...done!");
}
int score() {
int i, j;

for (i = 0; i < 8; i++)


for (j = 7; j < 8; j++)
if (tetrismap[i][j] == 0)
return 0;
//Serial.print("Score! ");
return 1;
}
Pastebin.com Tools & Applications
iPhone/iPad Windows Firefox Chrome WebOS Android Mac Opera Click.to UNIX WinPhon
e
create new paste | api | trends | syntax languages | faq | tools | p
rivacy | cookies | contact | dmca | advertise on pastebin | go pro
Follow us: pastebin on facebook | pastebin on twitter | pastebin in the news
Dedicated Server Hosting by Steadfast
Pastebin v3.11 rendered in: 0.008 seconds
const
//Pin
const
//Pin
const

int led_latch = 6;
connected to SH_CP of 74HC595
int led_clock = 5;
connected to DS of 74HC595
int led_data = 7;

const int joy_h = 0; // analog


const int joy_v = 1; // analog
//const int SEL = 2; // digital
void setup() {
//pinMode(SEL, INPUT);
//digitalWrite(SEL, HIGH);
pinMode(led_latch, OUTPUT);
pinMode(led_clock, OUTPUT);
pinMode(led_data, OUTPUT);
Serial.begin(9600);
}
int curry = 0, currx = 4; // current piece position !top corner
int movy = 0, movx = 0;
int joy_input_x, joy_input_y;
int tetrismap[8][8];
void loop() {
printMap();
// check if score changes
if (score()) {
advanceMap();
//
advanceMap();
}
// reset movement delta
movy = 0;
movx = 0;
// set joystick input read
joy_input_y = analogRead(joy_h);
joy_input_x = analogRead(joy_v);
// generating movement on x

if (joy_input_x < 250 && currx < 7)


movx++;
else if (joy_input_x > 750 && currx > 0)
movx--;
// generate movement on y
if (curry < 7 && tetrismap[currx][curry + 1] == 0)
movy++;

// validate/commit movement
if (tetrismap[currx][curry + 1] == 1 || curry == 7)
newPiece();
else
movePiece(movx, movy);
// draw
drawMap();
delay(500);
}
void newPiece() {
curry = 0;
currx = 4;
}
void movePiece(int delx, int dely) {
tetrismap[currx][curry] = 0;
tetrismap[currx + delx][curry + dely] = 1;
currx = currx + delx;
curry = curry + dely;
}
void drawLed(int y, int x) {
byte rowBitsToSend = 0;
byte columnBitsToSend = 0;
digitalWrite(led_latch, LOW);
bitWrite(columnBitsToSend, y, HIGH); // Gives us 000000001
shiftOut(led_data, led_clock, MSBFIRST, columnBitsToSend); // Send the byte to
the shift register which passes it to the second shift register
bitWrite(rowBitsToSend, x, HIGH); // Gives us 00000001
shiftOut(led_data, led_clock, MSBFIRST, rowBitsToSend); // Send the byte to th
e shift register
digitalWrite(led_latch, HIGH); // Tell the shift register we are done sending
data so it can enable the outputs
}
void clearLed(int y, int x) {
byte rowBitsToSend = 0;
byte columnBitsToSend = 0;
digitalWrite(led_latch, LOW);
bitWrite(columnBitsToSend, y, LOW); // Gives us 000000001
shiftOut(led_data, led_clock, MSBFIRST, columnBitsToSend); // Send the byte to
the shift register which passes it to the second shift register
bitWrite(rowBitsToSend, x, LOW); // Gives us 00000001
shiftOut(led_data, led_clock, MSBFIRST, rowBitsToSend); // Send the byte to th
e shift register
digitalWrite(led_latch, HIGH); // Tell the shift register we are done sending
data so it can enable the outputs

}
void drawMap() {
int i, j;
for (i = 0; i < 8; i++)
for (j = 0; j < 8; j++) {
if (tetrismap[i][j] == 1)
drawLed(i, j);
//else if (tetrismap[i][j] == 0)
//clearLed(i,j);
}
}
void printMap() {
int i, j;
Serial.print("\n");
for (i = 0; i < 8; i++) {
Serial.print("\ni");
for (j = 0; j < 8; j++) {
Serial.print(tetrismap[i][j], DEC);
Serial.print(" ");
}
}
Serial.print("\n");
}
void advanceMap() {
int i, j;
//Serial.print("Advancing map down...");
for (i = 0; i < 8; i++){
printMap();
for (j = 6; j > 0; j--)
tetrismap[i][j + 1] = tetrismap[i][j]; // move above lines down
}
//Serial.print("...done!");
}
int score() {
int i, j;
for (i = 0; i < 8; i++)
for (j = 7; j < 8; j++)
if (tetrismap[i][j] == 0)
return 0;
//Serial.print("Score! ");
return 1;
}

Anda mungkin juga menyukai