/*
* game.c
*
* Created on: Nov 30, 2020
* Author: Administrator
*/
#include "game.h"
#include "debug.h"
#include "u8g.h"
#include "stdlib.h"
#include "time.h"
#include "string.h"
u8g_t u8g;
u8 reset**=0;
//初始化变量
//声明变量很多地方用了byte类型不用int类型,可以节约内存
char wait=40;//延迟
int HP=20;//HP
char PPX=4;//初始化玩家X位置
char PPY=2;//初始化玩家Y位置
char PX=0;//初始化玩家X轴坐标
char PY=0;//初始化玩家Y轴坐标
//int RT=1;//初始化运行周期
int RT=1;//初始化运行周期
int S=0;//分数
char ObjA;//障碍物
char ObjB;//障碍物
char ObjC;//障碍物
char ObjD;//障碍物
//int Atmp=0;//缓存A
//int Btmp;//缓存B
//下面湿障碍物的数据,我们把障碍物数据保存在数组里,由运行内存显示,每隔20次运行周期会重新覆盖,保存内存不溢出
char CA[50];//障碍物A的位置
char CB[50];//障碍物B的位置
char CC[50];//障碍物C的位置
char CD[50];//障碍物D的位置
//下面湿障碍物A、B、C、D的坐标信息
char CAX[50];//障碍物A_X轴
char CBX[50];//障碍物B_X轴
char CCX[50];//障碍物C_X轴
char CDX[50];//障碍物D_X轴
char CAY[50];//障碍物A_Y轴
char CBY[50];//障碍物B_Y轴
char CCY[50];//障碍物C_Y轴
char CDY[50];//障碍物D_Y轴
//载入位图
const uint8_t title[] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
0x98, 0x81, 0x31, 0xF3, 0x20, 0x45, 0x1F, 0x44, 0x8A, 0x00, 0xA0, 0x00, 0x00, 0x81, 0x00, 0x00,
0x68, 0x41, 0x4A, 0x92, 0x20, 0x45, 0x04, 0x44, 0x8A, 0x00, 0xB0, 0x01, 0x00, 0x40, 0x01, 0x00,
0x14, 0x02, 0x49, 0xF2, 0xE0, 0x45, 0xC4, 0xC4, 0x8B, 0x00, 0x4E, 0x06, 0x00, 0x80, 0x00, 0x00,
0x14, 0x82, 0x48, 0x92, 0x20, 0x55, 0xC4, 0x44, 0xAA, 0x00, 0xB0, 0x01, 0x00, 0x00, 0x00, 0x00,
0x68, 0x41, 0x48, 0x92, 0x20, 0x29, 0x04, 0x44, 0x52, 0x08, 0xA8, 0x00, 0x3E, 0x00, 0x00, 0x00,
0x98, 0xC1, 0x33, 0xF7, 0x20, 0x29, 0x04, 0x5C, 0x52, 0x00, 0x40, 0xC0, 0x61, 0x00, 0x00, 0x30,
0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x38, 0xC3, 0x00, 0x00, 0x0C,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0xE7, 0xC6, 0xFF, 0x07, 0x03,
0x00, 0x40, 0x00, 0x04, 0x52, 0x00, 0x00, 0x00, 0x00, 0x02, 0xE0, 0x0C, 0xFE, 0xFF, 0xF9, 0x00,
0xC0, 0xF3, 0x1D, 0x04, 0x12, 0x00, 0x08, 0x00, 0x00, 0x00, 0x30, 0xE8, 0x81, 0x0F, 0xFC, 0x40,
0x00, 0x42, 0x94, 0x3F, 0x7E, 0x00, 0x14, 0x40, 0x00, 0x00, 0x1C, 0x1F, 0x00, 0x8E, 0x7F, 0x00,
0x00, 0xE5, 0x14, 0x04, 0x12, 0x00, 0x08, 0x00, 0x00, 0x00, 0xF6, 0x00, 0x00, 0xFC, 0x1F, 0x08,
0x00, 0xE3, 0x14, 0x0A, 0x57, 0x01, 0x00, 0x00, 0x00, 0x00, 0x3C, 0x00, 0x00, 0xFC, 0x0F, 0x00,
0x00, 0xE5, 0x14, 0x0A, 0x25, 0x00, 0x00, 0x00, 0x20, 0x00, 0x76, 0x00, 0x00, 0xF8, 0x03, 0x00,
0x00, 0x51, 0x15, 0x11, 0x55, 0x00, 0x00, 0x00, 0x00, 0x80, 0xF1, 0x00, 0x00, 0xF6, 0x01, 0x00,
0x00, 0x52, 0xA3, 0x20, 0x47, 0x00, 0x02, 0x20, 0x00, 0x60, 0xFC, 0x00, 0x80, 0x79, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x18, 0xFA, 0x01, 0x60, 0x36, 0x10, 0x14,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x38, 0x84, 0xF1, 0x03, 0x90, 0x09, 0x00, 0x02,
0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x1D, 0x00, 0x76, 0x43, 0xF0, 0x03, 0x7C, 0x07, 0x00, 0x54,
0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x07, 0x80, 0xCD, 0x00, 0xF8, 0x07, 0x9B, 0x01, 0x20, 0x00,
0x00, 0xE6, 0x00, 0x00, 0x00, 0x00, 0x02, 0x60, 0x33, 0x00, 0xEA, 0xC3, 0x44, 0x00, 0x02, 0x05,
0x00, 0x11, 0x03, 0x00, 0x00, 0x40, 0x02, 0xD8, 0x0C, 0x00, 0xE5, 0x31, 0x33, 0x00, 0x00, 0x28,
0xC0, 0xCC, 0x1C, 0x00, 0x00, 0x00, 0x70, 0x36, 0x03, 0x40, 0xDA, 0xC8, 0x1E, 0x00, 0x40, 0x75,
0x20, 0x32, 0x61, 0x80, 0x00, 0x00, 0x88, 0x8F, 0x00, 0xB0, 0x36, 0x36, 0x03, 0x00, 0x00, 0x20,
0x98, 0x0A, 0x81, 0x03, 0x00, 0x00, 0xE8, 0x70, 0x00, 0xE8, 0x8D, 0xC9, 0x20, 0x10, 0x50, 0x14,
0x64, 0x0C, 0x02, 0x1C, 0x00, 0x00, 0x78, 0x18, 0x00, 0x5E, 0x63, 0x36, 0x00, 0x02, 0x00, 0x00,
0x13, 0x08, 0x04, 0xE0, 0x00, 0x00, 0x08, 0x06, 0x80, 0xE7, 0x90, 0x19, 0x00, 0x00, 0x15, 0x75,
0x08, 0x08, 0x04, 0x00, 0x1F, 0x00, 0x90, 0x01, 0x40, 0x31, 0x6C, 0x06, 0x06, 0x00, 0x00, 0x20,
0x06, 0x10, 0x08, 0x00, 0xE0, 0xFF, 0x5F, 0x00, 0xF0, 0x10, 0x9B, 0x01, 0x00, 0x44, 0x50, 0x55,
0x01, 0x10, 0x10, 0x00, 0x00, 0x00, 0x30, 0x00, 0x28, 0xC8, 0x64, 0x00, 0x00, 0x00, 0x00, 0x20,
0x80, 0x10, 0x10, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x1E, 0x38, 0x13, 0x00, 0x00, 0x11, 0x75, 0x65,
0x00, 0x20, 0x20, 0x00, 0x00, 0x00, 0x03, 0x80, 0x07, 0xCC, 0x0C, 0x00, 0x00, 0x00, 0x02, 0xA0,
0x00, 0x20, 0x40, 0x00, 0x00, 0xC0, 0x00, 0x40, 0x01, 0x34, 0x03, 0x00, 0x44, 0x55, 0x55, 0x5D,
0x00, 0x40, 0x40, 0x00, 0x00, 0x20, 0x00, 0xF0, 0x00, 0xCE, 0x00, 0x00, 0x00, 0x80, 0x82, 0x28,
0x04, 0x50, 0x80, 0x00, 0x00, 0x18, 0x00, 0x38, 0x00, 0x23, 0x00, 0x00, 0x11, 0x15, 0x57, 0x65,
0x00, 0x40, 0x00, 0x01, 0x00, 0x06, 0x00, 0x0E, 0x00, 0x19, 0x20, 0x00, 0x00, 0x8A, 0x08, 0x20,
0x00, 0x80, 0x00, 0x01, 0x80, 0x01, 0x80, 0x07, 0x80, 0x06, 0x00, 0x44, 0xD1, 0x25, 0x44, 0x15,
0x00, 0x80, 0x00, 0x03, 0x60, 0x00, 0xC0, 0x01, 0xC0, 0x81, 0x00, 0x00, 0xA8, 0x60, 0xA0, 0x22,
0x00, 0x80, 0xC0, 0x00, 0x18, 0x00, 0x70, 0x00, 0x40, 0x00, 0x00, 0x45, 0x76, 0xC7, 0x30, 0x03,
0x00, 0x00, 0x31, 0x00, 0x04, 0x00, 0x78, 0x00, 0x20, 0x00, 0x00, 0x00, 0x22, 0xCA, 0x81, 0x00,
0x40, 0x00, 0x8D, 0x00, 0x03, 0x00, 0x4E, 0x00, 0x20, 0x00, 0x40, 0x00, 0x10, 0x80, 0x41, 0x05,
0x00, 0x80, 0xE3, 0xC0, 0x00, 0x80, 0x27, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x14,
0x00, 0x80, 0x18, 0x31, 0x00, 0xC0, 0x21, 0x00, 0xC8, 0x4B, 0x8F, 0x17, 0x66, 0x3A, 0x06, 0x04,
0x00, 0x20, 0x07, 0x0D, 0x00, 0x40, 0x20, 0x00, 0x08, 0x49, 0x81, 0x14, 0xA9, 0x0A, 0x1E, 0x04,
0x00, 0x00, 0x00, 0x02, 0x00, 0x20, 0x10, 0x00, 0x04, 0x79, 0x87, 0x17, 0xAF, 0x3A, 0xFF, 0xE4,
0x00, 0x00, 0xC0, 0x03, 0x00, 0x20, 0x10, 0x00, 0x04, 0x49, 0x81, 0x10, 0x29, 0x0B, 0xFE, 0xF3,
0x00, 0x00, 0xC0, 0x03, 0x00, 0x2A, 0x10, 0x00, 0x02, 0x49, 0x8F, 0x70, 0x29, 0x3A, 0xBF, 0xDF,
0x20, 0x00, 0xF0, 0x03, 0x00, 0x15, 0x08, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7E, 0x0C,
0x00, 0x80, 0xF8, 0x07, 0xA0, 0x1E, 0x08, 0x00, 0x01, 0x00, 0x88, 0xCC, 0x01, 0xC0, 0x7F, 0x44,
0x00, 0x40, 0x79, 0x07, 0x54, 0x13, 0x08, 0x80, 0x00, 0x00, 0x88, 0x52, 0x21, 0xC4, 0x5B, 0x34,
0x00, 0x80, 0xA8, 0xA7, 0xEA, 0x1C, 0x04, 0x80, 0x00, 0xFE, 0x88, 0x5E, 0x71, 0x97, 0x9F, 0x39,
0x00, 0x18, 0xD8, 0x5F, 0x35, 0x0B, 0x04, 0x5F, 0x80, 0xF9, 0xA8, 0xD2, 0xA8, 0x8A, 0x17, 0xE0,
0x00, 0x86, 0xD8, 0xBC, 0xFE, 0x0C, 0xE4, 0x30, 0x60, 0xF8, 0x50, 0x52, 0xD1, 0x45, 0x07, 0x90,
0x80, 0xAA, 0xD8, 0xFA, 0x13, 0x0B, 0x1E, 0x3C, 0x18, 0xF0, 0x50, 0x52, 0xA9, 0x08, 0xA7, 0x86,
0xF0, 0xCF, 0xF1, 0xFF, 0xD0, 0x04, 0xE2, 0x13, 0x06, 0x60, 0x00, 0x00, 0x74, 0x01, 0x07, 0x05,
0xAC, 0x72, 0xF7, 0x1F, 0x30, 0x04, 0x1E, 0x90, 0x01, 0x18, 0x00, 0x00, 0xA8, 0x86, 0x85, 0x22,
0x57, 0xDD, 0xE1, 0x07, 0x10, 0x04, 0x01, 0x68, 0x00, 0x06, 0x00, 0xC0, 0x5D, 0xFE, 0x49, 0x05,
0xAA, 0x8A, 0xC0, 0x00, 0x08, 0xFE, 0x01, 0x04, 0x80, 0x01, 0x28, 0xA8, 0x0A, 0x18, 0xC9, 0xA3,
0x77, 0xBF, 0x34, 0x00, 0x04, 0x02, 0x01, 0x04, 0x70, 0x00, 0x76, 0x76, 0x77, 0xE3, 0x50, 0x01,
0xAA, 0x0A, 0x17, 0x00, 0x04, 0x82, 0x00, 0x02, 0x0C, 0x07, 0xA2, 0x8A, 0xAA, 0x42, 0xE1, 0x02,
0xD5, 0xC1, 0x16, 0x00, 0x04, 0x82, 0x00, 0x01, 0xC3, 0x08, 0xD1, 0xC5, 0xD5, 0x45, 0x40, 0x47,
0xAA, 0xB8, 0x08, 0x00, 0x02, 0x81, 0x80, 0xC1, 0x30, 0x06, 0xAA, 0x0A, 0x80, 0x4A, 0xF2, 0x2F,
};
const uint8_t QR[] PROGMEM = {
0x7F, 0xAD, 0x41, 0x4C, 0x0D, 0xFD, 0x01, 0x41, 0x6D, 0x9D, 0x01, 0xCD, 0x05, 0x01, 0x5D, 0x3F,
0xC9, 0xFE, 0xB2, 0x75, 0x01, 0x5D, 0x9F, 0xA3, 0x2A, 0x9B, 0x74, 0x01, 0x5D, 0xCE, 0xE7, 0x8F,
0x14, 0x74, 0x01, 0x41, 0x57, 0x49, 0x0C, 0x5B, 0x04, 0x01, 0x7F, 0x55, 0x55, 0x55, 0x55, 0xFD,
0x01, 0x00, 0x04, 0x7A, 0x24, 0xF6, 0x01, 0x00, 0x73, 0x3A, 0xD1, 0xCF, 0x00, 0xE9, 0x01, 0x10,
0x16, 0x49, 0xA2, 0x1F, 0x2B, 0x00, 0x67, 0x7C, 0xB5, 0x7E, 0xC2, 0x5F, 0x01, 0xB2, 0x41, 0xB6,
0x49, 0xFB, 0x32, 0x00, 0xC6, 0x88, 0xB2, 0x3C, 0x4E, 0xBE, 0x00, 0xB0, 0x61, 0x1C, 0xA7, 0x16,
0x23, 0x00, 0xEF, 0x55, 0x6D, 0x65, 0xCB, 0x47, 0x01, 0x96, 0xB6, 0x65, 0x6A, 0xBC, 0x34, 0x00,
0x50, 0x22, 0x81, 0xC7, 0xEA, 0xCA, 0x00, 0x23, 0x23, 0xC9, 0x99, 0x35, 0xB9, 0x00, 0xF3, 0x09,
0x6B, 0x54, 0xE5, 0xBC, 0x01, 0x22, 0x36, 0x89, 0x19, 0x29, 0x39, 0x01, 0xD2, 0x6D, 0x5C, 0x96,
0x26, 0x2F, 0x00, 0x02, 0xD9, 0xE7, 0x07, 0xB4, 0x71, 0x00, 0xFB, 0x93, 0xD0, 0xC7, 0x39, 0xBF,
0x01, 0x1B, 0x17, 0x78, 0x1C, 0x6F, 0x31, 0x00, 0x52, 0x1B, 0x6B, 0x75, 0x42, 0x75, 0x01, 0x1A,
0xB5, 0x61, 0x94, 0xA5, 0xF1, 0x01, 0xFD, 0x7D, 0xE4, 0x97, 0xBF, 0xFF, 0x01, 0xB8, 0xCB, 0x7E,
0x61, 0x50, 0x0C, 0x00, 0xE2, 0x88, 0x72, 0xD5, 0xE0, 0xDB, 0x01, 0x8A, 0x76, 0x7C, 0x1B, 0x35,
0x2C, 0x01, 0x43, 0x66, 0xE9, 0xC4, 0x06, 0xBE, 0x01, 0x9E, 0xE4, 0xE1, 0x92, 0xE2, 0x8B, 0x01,
0x53, 0xA5, 0x98, 0x90, 0xE4, 0xD8, 0x01, 0xB9, 0x17, 0x4D, 0x36, 0x09, 0x4D, 0x00, 0x41, 0x9A,
0xE2, 0xF4, 0x43, 0x4E, 0x01, 0x1B, 0xAF, 0xB8, 0x69, 0xF9, 0xAB, 0x01, 0x5C, 0x76, 0xBC, 0x5B,
0xA8, 0x0B, 0x00, 0xBD, 0xCB, 0x27, 0x3F, 0x9F, 0x45, 0x00, 0x62, 0xFD, 0x56, 0x64, 0x53, 0x6C,
0x01, 0x0E, 0x17, 0x1E, 0x8C, 0xDF, 0xA7, 0x01, 0x47, 0x28, 0xE0, 0x9F, 0xEA, 0x7F, 0x00, 0x00,
0x61, 0x69, 0x8C, 0x39, 0x91, 0x00, 0x7F, 0x6A, 0x7D, 0xCD, 0x02, 0xF5, 0x01, 0x41, 0x47, 0x5F,
0x9C, 0x32, 0xB1, 0x00, 0x5D, 0x99, 0xF2, 0x97, 0x00, 0x3F, 0x00, 0x5D, 0x74, 0x1C, 0x06, 0xBC,
0xAB, 0x01, 0x5D, 0x60, 0x2F, 0xAD, 0xB9, 0xFA, 0x01, 0x41, 0x8B, 0x47, 0x09, 0x8F, 0xB5, 0x00,
0x7F, 0x05, 0xEB, 0x76, 0x60, 0xC9, 0x01,
};
const uint8_t pause[] PROGMEM = {
0xA7, 0xAA, 0xAA, 0xAA, 0x3A, 0x55, 0x55, 0x55, 0x55, 0x29, 0x07, 0x00, 0x00, 0x00, 0x38, 0xF0,
0xF7, 0xFB, 0xF7, 0x03, 0x19, 0x1C, 0x8E, 0x1C, 0x26, 0x8A, 0xCF, 0x04, 0xC8, 0x14, 0x89, 0x08,
0x24, 0x09, 0x24, 0x8A, 0x08, 0x24, 0xC9, 0x17, 0x89, 0xC9, 0x24, 0x09, 0x24, 0x1A, 0xCC, 0x24,
0x19, 0x16, 0xF1, 0xFF, 0xFF, 0xFF, 0x23, 0x0A, 0x86, 0x4C, 0x18, 0x16, 0xC9, 0x34, 0x4D, 0xC8,
0x24, 0x0A, 0x04, 0x4D, 0x0E, 0x14, 0x09, 0x02, 0xCD, 0xD9, 0x27, 0xCA, 0x33, 0x41, 0x10, 0x14,
0xC9, 0x33, 0x21, 0x18, 0x26, 0xFA, 0xFF, 0xFF, 0xFF, 0x17, 0x70, 0xFC, 0xDE, 0xEF, 0x03, 0x07,
0x00, 0x00, 0x00, 0x38, 0x55, 0x55, 0x55, 0x55, 0x29, 0xA7, 0xAA, 0xAA, 0xAA, 0x3A,
};
const uint8_t plane[] PROGMEM = {0x8, 0xC, 0x8C, 0xFF, 0xFF, 0x8C, 0xC, 0x8};
const uint8_t BOA[] PROGMEM = {0xCC, 0xCC, 0x33, 0x33, 0xCC, 0xCC, 0x33, 0x33};
const uint8_t BOB[] PROGMEM = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
const uint8_t BOC[] PROGMEM = {0x18, 0x66, 0x7E, 0xBD, 0xBD, 0x7E, 0x66, 0x18};
const uint8_t BOD[] PROGMEM = {0x0, 0x7E, 0x42, 0x5A, 0x5A, 0x42, 0x7E, 0x0};
const uint8_t life[] PROGMEM = {0x0, 0x66, 0xFF, 0xCF, 0xDF, 0x7E, 0x3C, 0x18};
/********************************************************/
void setup() {
// put your setup code here, to run once:
char key_value=0;
//初始化按钮
srand((unsigned)time(NULL));//伪随机数PIN
reset**=0;
u8g_FirstPage(&u8g);
do
{
u8g_DrawXBMP(&u8g,0,0,128,64,title);
}
while(u8g_NextPage(&u8g));
u8g_Delay(1024);
u8g_Delay(1024);
u8g_Delay(1024);
GET_KEY_Basedata();
key_value = GET_KEY_VALUE(JOY_X, JOY_Y, JOY_Z);
while(key_value!=LEFT_UP)
{
//显示
u8g_FirstPage(&u8g);
do
{
u8g_DrawXBMP(&u8g,0,0,128,64,title);
}
while(u8g_NextPage(&u8g));
GET_KEY_Basedata();
key_value = GET_KEY_VALUE(JOY_X, JOY_Y, JOY_Z);
printf("SETUP: x:%d y:%d z:%d\n", JOY_X,JOY_Y,JOY_Z);
switch(key_value)
{
case UP :
printf("UP\n");
break ;
case DOWN :
printf("DOWN\n");
break ;
case LEFT :
printf("LEFT\n");
break ;
case RIGHT :
printf("RIGHT\n");
break ;
case LEFT_UP:
printf("LEFT_UP\n");
break ;
case LEFT_DOWN:
printf("LEFT_DOWN\n");
break ;
case RIGHT_UP:
printf("RIGHT_UP\n");
break ;
case RIGHT_DONW :
printf("RIGHT_DONW\n");
break ;
case ENTER :
printf("ENTER\n");
break ;
default:
break ;
}
}
}
/**********************************************************/
void(* resetFunc)(void)=0;//制造从起命令的函数,下面会用到
void loop() {
int i=0;
int tmprand=0; //随机数缓存
char key_value=0;
int Atmp=0;//缓存A
int Btmp=0;//缓存B
//下面是GAME OVER或胜利后的处理
if(HP<0||S>30000)
{
//循环检测按钮
while(1)
{
//如果GAME OVER就等待按下A键复位游戏
GET_KEY_Basedata();
key_value = GET_KEY_VALUE(JOY_X, JOY_Y, JOY_Z);
printf("x:%d y:%d z:%d\n", JOY_X,JOY_Y,JOY_Z);
switch(key_value)
{
case UP :
printf("UP\n");
break ;
case DOWN :
printf("DOWN\n");
break ;
case LEFT :
printf("LEFT\n");
break ;
case RIGHT :
printf("RIGHT\n");
break ;
case LEFT_UP:
printf("LEFT_UP\n");
break ;
case LEFT_DOWN:
printf("LEFT_DOWN\n");
break ;
case RIGHT_UP:
printf("RIGHT_UP\n");
break ;
case RIGHT_DONW :
printf("RIGHT_DONW\n");
break ;
case ENTER :
printf("ENTER\n");
break ;
default:
break ;
}
//ENTER 代表A键
if(key_value==ENTER)//复位
{
printf("I'm in ENTER\n");
if(HP<0)
{
printf("I'm in HP<0 ENTER\n");
//resetFunc();//调用前面的软复位函数,达到软件重启Arduino的目的
NVIC_SystemReset();
//复位初始数据
wait=40;//延迟
HP=20;//HP
PPX=4;//初始化玩家X位置
PPY=2;//初始化玩家Y位置
PX=0;//初始化玩家X轴坐标
PY=0;//初始化玩家Y轴坐标
//int RT=1;//初始化运行周期
RT=1;//初始化运行周期
S=0;//分数
ObjA=0;//障碍物
ObjB=0;//障碍物
ObjC=0;//障碍物
ObjD=0;//障碍物
//int Atmp=0;//缓存A
//int Btmp;//缓存B
//////////memset(arr, 0, 1024); //清零
//下面湿障碍物的数据,我们把障碍物数据保存在数组里,由运行内存显示,每隔20次运行周期会重新覆盖,保存内存不溢出
memset(CA,0,50);//障碍物A的位置
memset(CB,0,50);//障碍物B的位置
memset(CC,0,50);//障碍物C的位置
memset(CD,0,50);//障碍物D的位置
//下面湿障碍物A、B、C、D的坐标信息
memset(CAX,0,50);//障碍物A_X轴
memset(CBX,0,50);//障碍物B_X轴
memset(CCX,0,50);//障碍物C_X轴
memset(CDX,0,50);//障碍物D_X轴
memset(CAY,0,50);//障碍物A_Y轴
memset(CBY,0,50);//障碍物B_Y轴
memset(CCY,0,50);//障碍物C_Y轴
memset(CDY,0,50);//障碍物D_Y轴
printf("RESTORE DATA\n");
}
// break;
}
//如果检测到分数大于3万,就判断赢了游戏,放出二维码彩蛋
if(S>=30000){
while(1)
{
u8g_SetColorIndex(&u8g,1);
u8g_FirstPage(&u8g);
do
{
u8g_DrawXBMP(&u8g,40,8,56,49,QR);
}
while(u8g_NextPage(&u8g));
}
}
}
}
GET_KEY_Basedata();
key_value = GET_KEY_VALUE(JOY_X, JOY_Y, JOY_Z);
if(key_value==UP)
{
//u8g_Delay(10);
if(key_value==UP)
{
if(PPY>1) PPY-=1;
}
}
if(key_value==DOWN)
{
//u8g_Delay(10);
if(key_value==DOWN)
{
if(PPY<4) PPY+=1;
}
}
if(key_value==LEFT)
{
//u8g_Delay(10);
if(key_value==LEFT)
{
if(PPX>1) PPX-=1;
}
}
if(key_value==RIGHT)
{
//u8g_Delay(10);
if(key_value==RIGHT)
{
if(PPX<15) PPX+=1;
}
}
//LEFT_UP代表B键
if(key_value==LEFT_UP)
{
//u8g_Delay(10);
if(key_value==LEFT_UP)
{
//u8g_Delay(200);
u8g_FirstPage(&u8g);
do
{
draw();
u8g_SetColorIndex(&u8g,0);
u8g_DrawBox(&u8g,42,10,46,28);//先覆盖掉原屏幕,避免下面的图片变透明
u8g_SetColorIndex(&u8g,1);
u8g_DrawXBMP(&u8g,45,13,40,22,pause);
}
while(u8g_NextPage(&u8g));
while(1)
{
GET_KEY_Basedata();
key_value = GET_KEY_VALUE(JOY_X, JOY_Y, JOY_Z);
if(key_value==LEFT_UP)
{
u8g_Delay(10);
if(key_value==LEFT_UP)
{
u8g_Delay(200);
break;
}
}
}
}
}
/**************************************/
//计算
S+=1;
PX=PPX*8;
PY=PPY*8;
ObjA=9;
ObjB=9;
ObjC=9;
ObjD=9;
if(RT<16)
{
//srand((unsigned)time(NULL));
tmprand= random();
Atmp=1+tmprand%100;//1~100
if(Atmp<=70)
{
//srand((unsigned)time(NULL));
tmprand= random();
ObjA=tmprand%5;
}
//乌云
//srand((unsigned)time(NULL));
tmprand= random();
Atmp=1+tmprand%100;//1~100
if(Atmp<=30)
{
//srand((unsigned)time(NULL));
tmprand= random();
ObjB=tmprand%5;
}
//加油机
//srand((unsigned)time(NULL));
tmprand= random();
Atmp=1+tmprand%100;//1~100
if(Atmp<=5)
{
//srand((unsigned)time(NULL));
tmprand= random();
ObjC=tmprand%5;
}
//其他飞机
//srand((unsigned)time(NULL));
tmprand= random();
Atmp=1+tmprand%100;//1~100
if(Atmp<=50)
{
//srand((unsigned)time(NULL));
tmprand= random();
ObjD=tmprand%5;
}
}
if(HP>12)HP=12;
//保存障碍物位置
CAY[RT]=ObjA*8;
CBY[RT]=ObjB*8;
CCY[RT]=ObjC*8;
CDY[RT]=ObjD*8;
CA[RT]=ObjA;
CB[RT]=ObjB;
CC[RT]=ObjC;
CD[RT]=ObjD;
CAX[RT]=120;
CBX[RT]=120;
CCX[RT]=120;
CDX[RT]=120;
//判断伤害
Btmp=15-PPX;
if(RT>Btmp)
{
Atmp=RT-Btmp;
if(PPY==CA[Atmp])
{
HP-=1;
}
if(PPY==CB[Atmp])
{
HP-=2;
}
if(PPY==CC[Atmp])
{
//Atmp=random(1,3);
//srand((unsigned)time(NULL));
tmprand= random();
Atmp=1+tmprand%3;
HP+=Atmp;
S+=30;
}
if(PPY==CD[Atmp])
{
//Atmp=random(1,100);
//srand((unsigned)time(NULL));
tmprand= random();
Atmp=1+tmprand%100;
if(Atmp<50)
{
HP+=1;
S+=15;
}
else
{
HP-=1;
}
}
}
/*******************************华丽的分割线*******************/
u8g_FirstPage(&u8g);
do
{
draw();
}
while(u8g_NextPage(&u8g));
//障碍物往下移
Atmp=RT-16;
if(Atmp<1)
{
Atmp=1;
}
for(Atmp;Atmp<=RT;Atmp++)
{
CAX[Atmp]-=8;//把障碍物的数组下移
CBX[Atmp]-=8;
CCX[Atmp]-=8;
CDX[Atmp]-=8;
}
RT+=1;
if(RT>=32)
{
RT=1;
}
u8g_Delay(wait);//防止游戏运行到不能玩的级别
}
void draw()
{
int Atmp=0;
int Btmp=0;
char tmpbuf[500]={0};
//障碍物循环码初始化计算
Atmp=RT-16;
if(Atmp<1)
{
Atmp=1;
}
//障碍物显示
for(Atmp;Atmp<=RT;Atmp++)
{
if(CCX[Atmp]>0)
{
u8g_DrawBitmapP(&u8g,CCX[Atmp],CCY[Atmp],1,8,BOC);
}
if(CDX[Atmp]>0)
{
u8g_DrawBitmapP(&u8g,CDX[Atmp],CDY[Atmp],1,8,BOD);
}
if(CAX[Atmp]>0)
{
u8g_DrawBitmapP(&u8g,CAX[Atmp],CAY[Atmp],1,8,BOA);
}
if(CBX[Atmp]>0)
{
u8g_DrawBitmapP(&u8g,CBX[Atmp],CBY[Atmp],1,8,BOB);
}
}
//显示玩家
u8g_DrawBitmapP(&u8g,PX,PY,1,8,plane);
//生命及分数
u8g_SetFont(&u8g,u8g_font_profont10);
u8g_SetColorIndex(&u8g,255);
if(HP>0)
{
u8g_DrawStr(&u8g, 8, 54, "HP:");
for(Atmp=1;Atmp<=HP;Atmp++)
{
Btmp=Atmp*8+20;
u8g_DrawBitmapP(&u8g,Btmp,45,1,8,life);
}
}
sprintf(tmpbuf,"score: %d",S);
u8g_DrawStr(&u8g, 8, 62,tmpbuf);
if(HP<0||S>=30000)
{
u8g_SetColorIndex(&u8g,255);
u8g_DrawBox(&u8g,0,0,128,64);
//打印结果
u8g_SetFont(&u8g,u8g_font_profont10);
u8g_SetColorIndex(&u8g,0);
if(HP<0)
u8g_DrawStr(&u8g, 4, 10,"GAME OVER");
if(S>=30000)
u8g_DrawStr(&u8g, 4, 10,"You Win");
//打印分数
u8g_SetFont(&u8g,u8g_font_profont10);
u8g_SetColorIndex(&u8g,0);
if(HP<0)
{
sprintf(tmpbuf,"score: %d",S);
u8g_DrawStr(&u8g, 4, 30,tmpbuf);
}
if(S>=30000)
{
u8g_DrawStr(&u8g, 4, 30,"score: 30000");
}
if(HP<0)
u8g_DrawStr(&u8g, 2, 62,"please push A to RESET");
if(S>=30000)
u8g_DrawStr(&u8g, 2, 62,"A to QR code");
}
}