Arduboy贪吃蛇
本帖最后由 Rainer 于 2017-9-22 09:02 编辑从来没写过游戏,刚买了个Arduboy,写个贪吃蛇DEMO
#include "Arduboy2.h"
Arduboy2 arduboy;
#define POINT_SIZE 8
#define SCREEN_WIDTH 16
#define SCREEN_HIGHT 8
int snake;
int head;
int head_x;
int head_y;
int direct;
bool gameover = false;
int food_x;
int food_y;
int food_rand;
int food_i;
int prev;
int flash_x;
int flash_y;
int flash_time;
int prevPoint;
int button;
const unsigned char shit[] PROGMEM= {
0x20, 0x58, 0x54, 0x52, 0x45, 0x48, 0x70, 0x40,
};
void setup() {
arduboy.begin();
arduboy.clear();
arduboy.setFrameRate(10);
initgame();
}
void loop() {
if (arduboy.pressed(UP_BUTTON)) {
button = UP_BUTTON;
} else if (arduboy.pressed(DOWN_BUTTON)) {
button = DOWN_BUTTON;
} else if (arduboy.pressed(LEFT_BUTTON)) {
button = LEFT_BUTTON;
} else if (arduboy.pressed(RIGHT_BUTTON)) {
button = RIGHT_BUTTON;
}
if (!(arduboy.nextFrame()))
return;
arduboy.setCursor(0, 0);
if (gameover) {
if (arduboy.pressed(A_BUTTON)) {
initgame();
}
arduboy.setCursor(40, 30);
arduboy.print("GAME OVER\n");
arduboy.setCursor(40, 40);
arduboy.print("Score: ");
arduboy.print(head - 3);
arduboy.print("\n");
arduboy.display();
return;
} else {
arduboy.clear();
}
head = 0;
head_x = 0;
head_y = 0;
prevPoint = 0;
for (int x = 0; x < SCREEN_WIDTH; x++) {
for (int y = 0; y < SCREEN_HIGHT; y++) {
if (snake > 0) {
drawPoint(x, y);
if (prevPoint > 0 && abs(snake - prevPoint) == 1 ) {
drawBody(x, y - 1, x, y);
}
if (snake > head) {
head = snake;
head_x = x;
head_y = y;
}
}
prevPoint = snake;
}
}
prevPoint = 0;
for (int y = 0; y < SCREEN_HIGHT; y++) {
for (int x = 0; x < SCREEN_WIDTH; x++) {
if (snake > 0 && prevPoint > 0 && abs(snake - prevPoint) == 1 ) {
drawBody(x - 1, y, x, y);
}
prevPoint = snake;
}
}
drawHead(head_x, head_y);
drawEye(head_x, head_y);
if (food_rand == 0) {
food_rand = random(1, SCREEN_WIDTH * SCREEN_HIGHT - head);
food_i = 0;
for (int i = 0; i < SCREEN_WIDTH; i++) {
for (int j = 0; j < SCREEN_HIGHT; j++) {
if (snake == 0) {
if (food_i == food_rand) {
food_x = i;
food_y = j;
}
food_i++;
}
}
}
}
// drawPoint(food_x, food_y);
arduboy.drawBitmap(food_x * POINT_SIZE, food_y * POINT_SIZE, shit, POINT_SIZE, POINT_SIZE, WHITE);
if (button == UP_BUTTON && prev != DOWN_BUTTON) {
forward(UP_BUTTON);
} else if (button == DOWN_BUTTON && prev != UP_BUTTON) {
forward(DOWN_BUTTON);
} else if (button == LEFT_BUTTON && prev != RIGHT_BUTTON) {
forward(LEFT_BUTTON);
} else if (button == RIGHT_BUTTON && prev != LEFT_BUTTON) {
forward(RIGHT_BUTTON);
} else {
forward(direct);
}
button = 0;
if (head_x == food_x && head_y == food_y) {
food_rand = 0;
flash_x = food_x;
flash_y = food_y;
flash_time = 3;
} else {
for (int i = 0; i < SCREEN_WIDTH; i++) {
for (int j = 0; j < SCREEN_HIGHT; j++) {
if (snake > 0) {
snake--;
}
}
}
}
if (flash_time > 0) {
arduboy.drawCircle(
flash_x * POINT_SIZE + POINT_SIZE / 2 - 1,
flash_y * POINT_SIZE + POINT_SIZE / 2 - 1,
POINT_SIZE / 2 + 4 - flash_time,
1);
flash_time--;
if (flash_time == 0) {
flash_x = -1;
flash_y = -1;
}
}
arduboy.display();
}
void drawPoint(int x, int y) {
//for (int i = 0; i < POINT_SIZE / 2; i++) {
// arduboy.drawRect(x * POINT_SIZE + i, y * POINT_SIZE + i, POINT_SIZE - i * 2, POINT_SIZE - i * 2, 1);
//}
arduboy.fillCircle(
x * POINT_SIZE + POINT_SIZE / 2 - 1,
y * POINT_SIZE + POINT_SIZE / 2 - 1,
POINT_SIZE / 2 - 1,
WHITE);
}
void drawHead(int x, int y) {
arduboy.fillCircle(
x * POINT_SIZE + POINT_SIZE / 2 - 1,
y * POINT_SIZE + POINT_SIZE / 2 - 1,
POINT_SIZE / 2,
WHITE);
}
void drawBody(int x0, int y0, int x1, int y1) {
int body_x;
int body_y;
if (x0 < x1) {
body_x = x0 * POINT_SIZE + POINT_SIZE / 2 - 1;
} else {
body_x = x0 * POINT_SIZE;
}
if (y0 < y1) {
body_y = y0 * POINT_SIZE + POINT_SIZE / 2 - 1;
} else {
body_y = y0 * POINT_SIZE;
}
arduboy.fillRect(body_x, body_y, POINT_SIZE - 1, POINT_SIZE - 1, WHITE);
}
void forward(int d) {
if (d == UP_BUTTON) {
head_y--;
} else if (d == DOWN_BUTTON) {
head_y++;
} else if (d == LEFT_BUTTON) {
head_x--;
} else if (d == RIGHT_BUTTON) {
head_x++;
}
if (head_x >= SCREEN_WIDTH
|| head_x < 0
|| head_y >= SCREEN_HIGHT
|| head_y < 0
|| snake > 0
) {
gameover = true;
} else {
snake = ++head;
direct = prev = d;
}
}
void drawEye(int x, int y) {
// arduboy.drawCircle(
// flash_x * POINT_SIZE + POINT_SIZE / 2 - 1,
// flash_y * POINT_SIZE + POINT_SIZE / 2 - 1,
// POINT_SIZE / 2 + 4 - flash_time,
// 1);
if (direct == UP_BUTTON || direct == DOWN_BUTTON) {
arduboy.fillCircle(
x * POINT_SIZE + POINT_SIZE / 2 - 1 - 2,
y * POINT_SIZE + POINT_SIZE / 2 - 1,
1,
BLACK);
arduboy.fillCircle(
x * POINT_SIZE + POINT_SIZE / 2 - 1 + 2,
y * POINT_SIZE + POINT_SIZE / 2 - 1,
1,
BLACK);
} else {
arduboy.fillCircle(
x * POINT_SIZE + POINT_SIZE / 2 - 1,
y * POINT_SIZE + POINT_SIZE / 2 - 1 - 2,
1,
BLACK);
arduboy.fillCircle(
x * POINT_SIZE + POINT_SIZE / 2 - 1,
y * POINT_SIZE + POINT_SIZE / 2 - 1 + 2,
1,
BLACK);
}
}
void initgame() {
for (int i = 0; i < SCREEN_WIDTH; i++) {
for (int j = 0; j < SCREEN_HIGHT; j++) {
snake = 0;
}
}
snake = 1;
snake = 2;
snake = 3;
direct = RIGHT_BUTTON;
gameover = false;
food_rand = 0;
flash_x = -1;
flash_y = -1;
flash_time = 0;
button = 0;
}
.........有具体一点的吗
最后效果啥样啊?
页:
[1]