initial
parent
761e85023c
commit
37a3080b48
|
@ -0,0 +1,49 @@
|
|||
{
|
||||
"files.associations": {
|
||||
"array": "cpp",
|
||||
"atomic": "cpp",
|
||||
"*.tcc": "cpp",
|
||||
"cctype": "cpp",
|
||||
"clocale": "cpp",
|
||||
"cmath": "cpp",
|
||||
"cstdarg": "cpp",
|
||||
"cstddef": "cpp",
|
||||
"cstdint": "cpp",
|
||||
"cstdio": "cpp",
|
||||
"cstdlib": "cpp",
|
||||
"ctime": "cpp",
|
||||
"cwchar": "cpp",
|
||||
"cwctype": "cpp",
|
||||
"deque": "cpp",
|
||||
"unordered_map": "cpp",
|
||||
"vector": "cpp",
|
||||
"exception": "cpp",
|
||||
"algorithm": "cpp",
|
||||
"functional": "cpp",
|
||||
"iterator": "cpp",
|
||||
"memory": "cpp",
|
||||
"memory_resource": "cpp",
|
||||
"numeric": "cpp",
|
||||
"optional": "cpp",
|
||||
"random": "cpp",
|
||||
"string": "cpp",
|
||||
"string_view": "cpp",
|
||||
"system_error": "cpp",
|
||||
"tuple": "cpp",
|
||||
"type_traits": "cpp",
|
||||
"utility": "cpp",
|
||||
"fstream": "cpp",
|
||||
"initializer_list": "cpp",
|
||||
"iosfwd": "cpp",
|
||||
"iostream": "cpp",
|
||||
"istream": "cpp",
|
||||
"limits": "cpp",
|
||||
"new": "cpp",
|
||||
"ostream": "cpp",
|
||||
"sstream": "cpp",
|
||||
"stdexcept": "cpp",
|
||||
"streambuf": "cpp",
|
||||
"cinttypes": "cpp",
|
||||
"typeinfo": "cpp"
|
||||
}
|
||||
}
|
859
src/main.cpp
859
src/main.cpp
|
@ -1,11 +1,9 @@
|
|||
#include <HardwareSerial.h>
|
||||
#include <ESP32-HUB75-MatrixPanel-I2S-DMA.h>
|
||||
#include <FastLED.h>
|
||||
#include <WebServer.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <Fonts/FreeMono9pt7b.h>
|
||||
#include <spacecraft.h>
|
||||
#include <gif_frames.h>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#pragma region Matrix Config
|
||||
#define R1_PIN 4
|
||||
#define G1_PIN 5
|
||||
|
@ -30,6 +28,15 @@
|
|||
#define PANE_WIDTH PANEL_WIDTH *PANELS_NUMBER
|
||||
#define PANE_HEIGHT PANEL_HEIGHT
|
||||
#define NUM_PIXELS PANE_WIDTH *PANE_HEIGHT
|
||||
|
||||
|
||||
#define COLOR_GREEN 0x07E0
|
||||
#define COLOR_RED 0xF800
|
||||
#define COLOR_BLUE 0x001F
|
||||
#define COLOR_YELLOW 0xFFE0
|
||||
#define COLOR_WHITE 0xFFFF
|
||||
#define COLOR_BLACK 0x0000
|
||||
|
||||
MatrixPanel_I2S_DMA *dma_display = nullptr;
|
||||
// Another way of creating config structure
|
||||
// Custom pin mapping for all pins
|
||||
|
@ -47,15 +54,32 @@ uint8_t buffer[bufferSize];
|
|||
int bufferIndex = 0;
|
||||
int numRows = 0;
|
||||
int numCols = 0;
|
||||
const int DATA_SIZE = 8192; // Số lượng phần tử trong mảng data
|
||||
uint16_t data[DATA_SIZE]; // Mảng để lưu trữ dữ liệu RGB565
|
||||
const int floatSize = sizeof(float);
|
||||
static int x_moi = 0;
|
||||
static int y_moi = 0;
|
||||
int num_moi = 0;
|
||||
bool reached = false;
|
||||
bool gen_moi = false;
|
||||
unsigned long period_time = millis();
|
||||
const int SCREEN_WIDTH = 128;
|
||||
const int SCREEN_HEIGHT = 128;
|
||||
const int PLAYER_WIDTH = 16;
|
||||
const int PLAYER_HEIGHT = 8;
|
||||
const int ENEMY_WIDTH = 16;
|
||||
const int ENEMY_HEIGHT = 8;
|
||||
const int BULLET_WIDTH = 2;
|
||||
const int BULLET_HEIGHT = 4;
|
||||
const int NUM_ENEMIES = 10;
|
||||
|
||||
|
||||
struct Player {
|
||||
int x, y;
|
||||
bool alive;
|
||||
};
|
||||
|
||||
struct Enemy {
|
||||
int x, y;
|
||||
bool alive;
|
||||
};
|
||||
|
||||
struct Bullet {
|
||||
int x, y;
|
||||
bool active;
|
||||
};
|
||||
|
||||
uint32_t rgb565_to_rgb888(uint16_t rgb565)
|
||||
{
|
||||
|
@ -72,725 +96,91 @@ uint32_t rgb565_to_rgb888(uint16_t rgb565)
|
|||
// Ghép các giá trị RGB888 thành một giá trị 32 bit
|
||||
return (r << 16) | (g << 8) | b;
|
||||
}
|
||||
void drawXbm565(int x, int y, int width, int height, const char *xbm, uint16_t color = 0xffff)
|
||||
Player player ;
|
||||
std::vector<Enemy> enemies(NUM_ENEMIES);
|
||||
Bullet bullet = {0, 0, false};
|
||||
void Initialize_State()
|
||||
{
|
||||
if (width % 8 != 0)
|
||||
player = {SCREEN_WIDTH / 2 - PLAYER_WIDTH / 2, SCREEN_HEIGHT - PLAYER_HEIGHT - 10, true};
|
||||
for (int i = 0; i < NUM_ENEMIES; ++i)
|
||||
{
|
||||
width = ((width / 8) + 1) * 8;
|
||||
enemies[i] = {i * (SCREEN_WIDTH / NUM_ENEMIES), 0, true};
|
||||
}
|
||||
for (int i = 0; i < width * height / 8; i++)
|
||||
{
|
||||
unsigned char charColumn = pgm_read_byte(xbm + i);
|
||||
for (int j = 0; j < 8; j++)
|
||||
{
|
||||
int targetX = (i * 8 + j) % width + x;
|
||||
int targetY = (8 * i / (width)) + y;
|
||||
if (bitRead(charColumn, j))
|
||||
{
|
||||
dma_display->drawPixel(targetX, targetY, color);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
uint16_t rgbToUint16(uint8_t r, uint8_t g, uint8_t b, bool mode)
|
||||
{
|
||||
if (mode)
|
||||
{
|
||||
uint16_t red = (r >> 3) & 0x1F; // Chuyển từ 8 bit xuống 5 bit
|
||||
uint16_t green = (g >> 2) & 0x3F; // Chuyển từ 8 bit xuống 6 bit
|
||||
uint16_t blue = (b >> 3) & 0x1F; // Chuyển từ 8 bit xuống 5 bit
|
||||
|
||||
return (red << 11) | (green << 5) | blue;
|
||||
}
|
||||
else
|
||||
{
|
||||
uint16_t red = (r >> 3) & 0x1F; // Chuyển từ 8 bit xuống 5 bit
|
||||
uint16_t green = (b >> 2) & 0x3F; // Chuyển từ 8 bit xuống 6 bit
|
||||
uint16_t blue = (g >> 3) & 0x1F; // Chuyển từ 8 bit xuống 5 bit
|
||||
|
||||
return (red << 11) | (green << 5) | blue;
|
||||
}
|
||||
}
|
||||
void draw_true_color(int x, int y, int r, int g, int b)
|
||||
{
|
||||
if (y <= 31)
|
||||
{
|
||||
dma_display->drawPixelRGB888(x, y, r, g, b);
|
||||
}
|
||||
else
|
||||
{
|
||||
dma_display->drawPixelRGB888(x, y, r, b, g);
|
||||
}
|
||||
}
|
||||
// Hàm kiểm tra xem điểm (x, y) có đủ xa điểm (x_ref, y_ref) không
|
||||
bool isFarEnough(int x, int x_ref, int min_distance)
|
||||
{
|
||||
int dx = x - x_ref;
|
||||
return (dx * dx) >= (min_distance * min_distance);
|
||||
}
|
||||
|
||||
// Hàm tạo điểm ngẫu nhiên không gần với điểm đã cho
|
||||
void drawRandomPoint(int x_ref, int y_ref, int min_distance, bool generated)
|
||||
{
|
||||
// int x, y;
|
||||
bool point_found = false;
|
||||
if (generated)
|
||||
{
|
||||
for (int i = x_moi - 1; i <= x_moi + 1; i++)
|
||||
{
|
||||
for (int j = y_moi - 1; j <= y_moi + 1; j++)
|
||||
{
|
||||
draw_true_color(i, j, 0, 0, 255);
|
||||
void updatePlayer(int x) {
|
||||
// Example: Move player left or right based on input
|
||||
player.x = x;
|
||||
}
|
||||
void updateEnemies() {
|
||||
for (auto& enemy : enemies) {
|
||||
if (enemy.alive) {
|
||||
enemy.y += 1; // Move downwards
|
||||
if (enemy.y > SCREEN_HEIGHT) {
|
||||
enemy.y = 0; // Reset position if off screen
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
while (!point_found)
|
||||
{
|
||||
x_moi = random(PANE_WIDTH);
|
||||
y_moi = y_ref;
|
||||
// Kiểm tra khoảng cách giữa điểm ngẫu nhiên và điểm đã cho
|
||||
if (isFarEnough(x_moi, x_ref, min_distance) && x_moi >= 32 && x_moi < 220)
|
||||
{
|
||||
point_found = true;
|
||||
}
|
||||
}
|
||||
for (int i = x_moi - 1; i < x_moi + 1; i++)
|
||||
{
|
||||
for (int j = y_moi - 1; j < y_moi + 1; j++)
|
||||
{
|
||||
draw_true_color(i, j, 0, 0, 255);
|
||||
}
|
||||
void updateBullet() {
|
||||
if (bullet.active) {
|
||||
bullet.y -= 4; // Move upwards
|
||||
if (bullet.y < 0) {
|
||||
bullet.active = false; // Deactivate if off screen
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int MinX_bar = 0;
|
||||
int MaxX_bar = 5;
|
||||
int MinY_bar = 50;
|
||||
void Drawbar(int x)
|
||||
{
|
||||
// measure x dimension
|
||||
int dodai = 16;
|
||||
MinX_bar = x - dodai / 2;
|
||||
MaxX_bar = x + dodai / 2;
|
||||
dma_display->drawLine(MinX_bar, MinY_bar - 1, MaxX_bar, MinY_bar - 1, dma_display->color333(3, 3, 3));
|
||||
dma_display->drawLine(MinX_bar, MinY_bar, MaxX_bar, MinY_bar, dma_display->color333(3, 3, 3));
|
||||
}
|
||||
|
||||
bool created = false;
|
||||
int X_vat = 20;
|
||||
int Y_vat = 2;
|
||||
int lastpoint[2];
|
||||
int r = 2;
|
||||
int speed = 150;
|
||||
void DrawObject(bool deleted = false)
|
||||
{
|
||||
// measure x dimension
|
||||
unsigned long now = millis();
|
||||
if (!created)
|
||||
{
|
||||
lastpoint[0] = X_vat;
|
||||
lastpoint[1] = Y_vat;
|
||||
dma_display->fillCircle(X_vat, Y_vat, r, dma_display->color333(0, 0, 0));
|
||||
dma_display->drawLine(MinX_bar, MinY_bar - 1, MaxX_bar, MinY_bar - 1, dma_display->color333(3, 3, 3));
|
||||
dma_display->drawLine(MinX_bar, MinY_bar, MaxX_bar, MinY_bar, dma_display->color333(3, 3, 3));
|
||||
|
||||
X_vat = random(20, 109); // random(min, max), max không bao gồm
|
||||
Y_vat = 2;
|
||||
created = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (now - period_time >= speed)
|
||||
{
|
||||
dma_display->fillCircle(X_vat, Y_vat, r, dma_display->color333(0, 0, 0));
|
||||
Y_vat += 1;
|
||||
if (Y_vat > 52)
|
||||
created = false;
|
||||
period_time = now;
|
||||
void checkCollisions() {
|
||||
// Check collision between bullet and enemies
|
||||
for (auto& enemy : enemies) {
|
||||
if (enemy.alive && bullet.active &&
|
||||
bullet.x < enemy.x + ENEMY_WIDTH &&
|
||||
bullet.x + BULLET_WIDTH > enemy.x &&
|
||||
bullet.y < enemy.y + ENEMY_HEIGHT &&
|
||||
bullet.y + BULLET_HEIGHT > enemy.y) {
|
||||
enemy.alive = false;
|
||||
bullet.active = false;
|
||||
}
|
||||
}
|
||||
|
||||
// dma_display->drawCircle(X_vat,Y_vat,1,dma_display->color333(7,0,0));
|
||||
dma_display->fillCircle(X_vat, Y_vat, r, dma_display->color333(7, 0, 0));
|
||||
}
|
||||
|
||||
int score = 0;
|
||||
#define COLOR_BLACK dma_display->color333(0, 0, 0)
|
||||
#define COLOR_WHITE dma_display->color333(7, 7, 7)
|
||||
void HeldObject()
|
||||
{
|
||||
if (Y_vat + r >= MinY_bar - 1 && X_vat >= MinX_bar - r && X_vat <= MaxX_bar + r)
|
||||
{
|
||||
Serial.println("Created");
|
||||
created = false;
|
||||
// DrawObject(true);
|
||||
DrawObject();
|
||||
|
||||
dma_display->setTextColor(COLOR_BLACK);
|
||||
dma_display->setCursor(2, 11);
|
||||
dma_display->printf("%d", score);
|
||||
score += 1;
|
||||
dma_display->setFont(&FreeMono9pt7b); // Change to the desired font size
|
||||
dma_display->setTextColor(COLOR_WHITE);
|
||||
|
||||
dma_display->setCursor(2, 11);
|
||||
dma_display->printf("%d", score);
|
||||
}
|
||||
else
|
||||
{
|
||||
dma_display->setFont(&FreeMono9pt7b); // Change to the desired font size
|
||||
dma_display->setTextColor(COLOR_WHITE);
|
||||
dma_display->setCursor(2, 11);
|
||||
dma_display->printf("%d", score);
|
||||
// Check collision between enemies and player
|
||||
for (auto& enemy : enemies) {
|
||||
if (enemy.alive &&
|
||||
player.x < enemy.x + ENEMY_WIDTH &&
|
||||
player.x + PLAYER_WIDTH > enemy.x &&
|
||||
player.y < enemy.y + ENEMY_HEIGHT &&
|
||||
player.y + PLAYER_HEIGHT > enemy.y) {
|
||||
player.alive = false; // Player is hit
|
||||
}
|
||||
}
|
||||
|
||||
// Stickman
|
||||
|
||||
int X_shot = 0;
|
||||
int Y_shot = 32;
|
||||
bool created_shot = false;
|
||||
|
||||
bool direction = false; // false = left, true = right;
|
||||
int lastpoint_shot[2];
|
||||
unsigned long period_shot = millis();
|
||||
int speed_shot = 50;
|
||||
void Drawbullet(bool deleted = false)
|
||||
{
|
||||
// measure x dimension
|
||||
unsigned long now = millis();
|
||||
if (!created_shot)
|
||||
{
|
||||
lastpoint_shot[0] = X_shot;
|
||||
lastpoint_shot[1] = Y_shot;
|
||||
dma_display->fillCircle(X_shot, Y_shot, 1, dma_display->color333(0, 0, 0));
|
||||
int ran = random(1, 3);
|
||||
if (ran == 1)
|
||||
{
|
||||
X_shot = 1;
|
||||
direction = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
X_shot = 126;
|
||||
direction = true;
|
||||
}
|
||||
created_shot = true;
|
||||
void render() {
|
||||
dma_display->clearScreen();
|
||||
dma_display->fillCircle(X_shot, Y_shot, 1, dma_display->color333(5, 0, 0));
|
||||
}
|
||||
else
|
||||
{
|
||||
if (now - period_shot >= speed_shot)
|
||||
{
|
||||
dma_display->fillCircle(lastpoint_shot[0], lastpoint_shot[1], 1, dma_display->color333(0, 0, 0));
|
||||
|
||||
if (direction == false)
|
||||
{
|
||||
X_shot += 1;
|
||||
if (X_shot >= 126)
|
||||
created_shot = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
X_shot -= 1;
|
||||
if (X_shot <= 2)
|
||||
created_shot = false;
|
||||
}
|
||||
lastpoint_shot[0] = X_shot;
|
||||
lastpoint_shot[1] = Y_shot;
|
||||
dma_display->fillCircle(X_shot, Y_shot, 1, dma_display->color333(5, 0, 0));
|
||||
period_shot = now;
|
||||
}
|
||||
}
|
||||
// dma_display->fillCircle(X_shot,Y_shot,1,dma_display->color333(5,0,0));
|
||||
|
||||
// dma_display->drawCircle(X_vat,Y_vat,1,dma_display->color333(7,0,0));
|
||||
if (player.alive) {
|
||||
dma_display->drawRect(player.x, player.y, PLAYER_WIDTH, PLAYER_HEIGHT, COLOR_GREEN);
|
||||
}
|
||||
|
||||
int current_sheildX = 0;
|
||||
int current_sheildY = 0;
|
||||
int length_shield = 11;
|
||||
int current_stickmanX = 0;
|
||||
int current_stickmanY = 0;
|
||||
int Shield_open = 0;
|
||||
unsigned long period_gameover = millis();
|
||||
void drawStickMan(int x, int y, int defend = 0)
|
||||
{
|
||||
// Vẽ đầu
|
||||
dma_display->drawCircle(x, y, 2, rgbToUint16(255, 0, 0, false)); // Đầu stick man
|
||||
|
||||
// Vẽ thân
|
||||
dma_display->drawLine(x, y + 3, x, y + 7, rgbToUint16(255, 0, 0, false)); // Thân stick man
|
||||
|
||||
// Vẽ tay
|
||||
dma_display->drawLine(x - 3, y + 4, x + 3, y + 4, rgbToUint16(255, 0, 0, false)); // Tay trái và tay phải
|
||||
// Vẽ chân
|
||||
dma_display->drawLine(x, y + 7, x - 3, y + 10, rgbToUint16(255, 0, 0, false)); // Chân trái
|
||||
dma_display->drawLine(x, y + 7, x + 3, y + 10, rgbToUint16(255, 0, 0, false)); // Chân phải
|
||||
current_stickmanX = x;
|
||||
current_stickmanY = y;
|
||||
if (defend == 2)
|
||||
{
|
||||
dma_display->drawLine(x - 4, y - 1, x - 4, y + 10, rgbToUint16(255, 0, 0, false));
|
||||
dma_display->drawLine(x - 5, y - 1, x - 5, y + 10, rgbToUint16(255, 0, 0, false));
|
||||
current_sheildX = x - 5;
|
||||
current_sheildY = y - 1;
|
||||
length_shield = 11;
|
||||
}
|
||||
else if (defend == 1)
|
||||
{
|
||||
dma_display->drawLine(x + 4, y - 1, x + 4, y + 10, rgbToUint16(255, 0, 0, false));
|
||||
dma_display->drawLine(x + 5, y - 1, x + 5, y + 10, rgbToUint16(255, 0, 0, false));
|
||||
current_sheildX = x + 5;
|
||||
current_sheildY = y - 1;
|
||||
length_shield = 11;
|
||||
}
|
||||
else
|
||||
{
|
||||
current_sheildX = 0;
|
||||
current_sheildY = 0;
|
||||
}
|
||||
}
|
||||
bool game_over = false;
|
||||
void Defend()
|
||||
{
|
||||
if (!direction)
|
||||
{
|
||||
// Do duoc ben trai
|
||||
if (current_sheildX != 0)
|
||||
{
|
||||
// co khien trai
|
||||
if (current_sheildX == current_stickmanX - 5)
|
||||
{
|
||||
if (X_shot >= current_sheildX)
|
||||
{
|
||||
created_shot = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
// ko co khien trai
|
||||
else
|
||||
{
|
||||
if (X_shot >= current_stickmanX - 5)
|
||||
{
|
||||
game_over = true;
|
||||
dma_display->setFont(&FreeMono9pt7b); // Change to the desired font size
|
||||
dma_display->setTextColor(COLOR_WHITE);
|
||||
dma_display->setCursor(2 + 16, 11 + 20);
|
||||
dma_display->print("GAME OVER");
|
||||
period_gameover = millis();
|
||||
}
|
||||
else
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (current_sheildX != 0)
|
||||
{
|
||||
// co khien phai
|
||||
if (current_sheildX == current_stickmanX + 5)
|
||||
{
|
||||
if (X_shot <= current_sheildX)
|
||||
{
|
||||
created_shot = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
// do nothing
|
||||
created_shot = true;
|
||||
}
|
||||
}
|
||||
// ko co khien trai
|
||||
else
|
||||
{
|
||||
if (X_shot <= current_stickmanX + 5)
|
||||
{
|
||||
game_over = true;
|
||||
dma_display->setFont(&FreeMono9pt7b); // Change to the desired font size
|
||||
dma_display->setTextColor(COLOR_WHITE);
|
||||
dma_display->setCursor(2 + 16, 11 + 20);
|
||||
dma_display->print("GAME OVER");
|
||||
period_gameover = millis();
|
||||
}
|
||||
else
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
}
|
||||
for (const auto& enemy : enemies) {
|
||||
if (enemy.alive) {
|
||||
dma_display->drawRect(enemy.x, enemy.y, ENEMY_WIDTH, ENEMY_HEIGHT, COLOR_RED);
|
||||
}
|
||||
}
|
||||
|
||||
bool mode = false;
|
||||
int reset_stickman = 2000;
|
||||
bool Mode_game = true;
|
||||
int width = 26;
|
||||
int height = 27;
|
||||
int X_craft = 0;
|
||||
int Y_craft = 0;
|
||||
bool exist = false;
|
||||
const int num_rocket = 15;
|
||||
int rockets[num_rocket][4] =
|
||||
{
|
||||
{0, -2, 0, -2},
|
||||
{0, -14, 0, -14},
|
||||
{0, -26, 0, -26},
|
||||
{0, -38, 0, -38},
|
||||
{0, -50, 0, -50},
|
||||
{0, -62, 0, -62},
|
||||
{0, -74, 0, -74},
|
||||
{0, -86, 0, -86},
|
||||
{0, -98, 0, -98},
|
||||
{0, -110, 0, -110}};
|
||||
const int enemies_numX = 15;
|
||||
const int enemies_numY = 3;
|
||||
const int enemies_num = enemies_numX * enemies_numY;
|
||||
int enemies[enemies_num][2];
|
||||
int x_step = 8;
|
||||
int y_step = 6;
|
||||
int live_enemies = 0;
|
||||
void create_enemies()
|
||||
{
|
||||
live_enemies = enemies_num;
|
||||
for (int i = 1; i < enemies_numX + 1; i++)
|
||||
{
|
||||
for (int j = 1; j < enemies_numY + 1; j++)
|
||||
{
|
||||
int x = i * x_step;
|
||||
int y = j * y_step;
|
||||
|
||||
enemies[(i - 1) * 3 + (j - 1)][0] = x;
|
||||
enemies[(i - 1) * 3 + (j - 1)][1] = y;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void draw_enemies()
|
||||
{
|
||||
for (int i = 0; i < enemies_num; i++)
|
||||
{
|
||||
uint16_t color = 0x03FF;
|
||||
int x = enemies[i][0];
|
||||
int y = enemies[i][1];
|
||||
if (x == 0 && y == 0)
|
||||
continue;
|
||||
dma_display->drawCircle(enemies[i][0], enemies[i][1], 1, color);
|
||||
if (bullet.active) {
|
||||
dma_display->drawRect(bullet.x, bullet.y, BULLET_WIDTH, BULLET_HEIGHT, COLOR_WHITE);
|
||||
}
|
||||
}
|
||||
|
||||
void destroy_enemy()
|
||||
{
|
||||
if (!exist)
|
||||
return;
|
||||
for (int i = 0; i < enemies_num; i++)
|
||||
{
|
||||
for (int x = 0; x < num_rocket; x++)
|
||||
{
|
||||
int x1_rocket = rockets[x][0];
|
||||
int y1_rocket = rockets[x][1];
|
||||
int x2_rocket = rockets[x][2];
|
||||
int y2_rocket = rockets[x][3];
|
||||
|
||||
int x_enemy = enemies[i][0];
|
||||
int y_enemy = enemies[i][1];
|
||||
bool check_init = x1_rocket!=0&&x2_rocket!=0&&x_enemy!=0&&y_enemy!=0;
|
||||
if (((x1_rocket == x_enemy && y1_rocket + 100 == y_enemy) || (x2_rocket == x_enemy && y2_rocket + 100 == y_enemy)) && live_enemies > 0 && check_init)
|
||||
{
|
||||
dma_display->drawCircle(enemies[i][0], enemies[i][1], 1, 0x0000);
|
||||
enemies[i][0] = 0;
|
||||
enemies[i][1] = 0;
|
||||
live_enemies--;
|
||||
Serial.println(live_enemies);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
draw_enemies();
|
||||
}
|
||||
void draw_enemy(int x, int y)
|
||||
{
|
||||
uint16_t color = 0x03FF;
|
||||
int _x = x;
|
||||
int _y = y;
|
||||
if (_y <= 64)
|
||||
{
|
||||
dma_display->fillCircle(_x, _y, 1, color);
|
||||
}
|
||||
else
|
||||
{
|
||||
_y = _y - 64;
|
||||
_x = _x + 128;
|
||||
|
||||
dma_display->drawPixel(_x, _y, color);
|
||||
}
|
||||
}
|
||||
void draw_rocket(int x_craft, int y_craft = 100)
|
||||
{
|
||||
unsigned long period_time = millis();
|
||||
bool set_FPS(int rate){
|
||||
unsigned long now = millis();
|
||||
if (now - period_shot >= speed_shot)
|
||||
{
|
||||
period_shot = now;
|
||||
// Serial.println("clear");
|
||||
for (int row = 0; row < num_rocket; row++)
|
||||
{
|
||||
for (int col = 0; col < 2; col++)
|
||||
{
|
||||
int x = rockets[row][col * 2];
|
||||
int y = rockets[row][col * 2 + 1] + y_craft;
|
||||
// dma_display->drawPixel(x,y,0x0000);
|
||||
uint16_t color = 0x0000;
|
||||
|
||||
int _x = x;
|
||||
int _y = y;
|
||||
if (_y <= 64)
|
||||
{
|
||||
dma_display->drawPixel(_x, _y, color);
|
||||
int elapsed = 1000/rate;
|
||||
if(now - period_time >=elapsed){
|
||||
period_time = now;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
_y = _y - 64;
|
||||
_x = _x + 128;
|
||||
|
||||
dma_display->drawPixel(_x, _y, color);
|
||||
}
|
||||
rockets[row][col * 2 + 1] -= 1;
|
||||
if (rockets[row][col * 2 + 1] <= -110)
|
||||
{
|
||||
rockets[row][col * 2 + 1] = -2;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Draw
|
||||
for (int row = 0; row < num_rocket; row++)
|
||||
{
|
||||
for (int col = 0; col < 2; col++)
|
||||
{
|
||||
if (rockets[row][col * 2 + 1] == -2)
|
||||
{
|
||||
rockets[row][0] = x_craft + 4;
|
||||
rockets[row][2] = x_craft + 21;
|
||||
}
|
||||
|
||||
int x = rockets[row][col * 2];
|
||||
int y = rockets[row][col * 2 + 1] + y_craft;
|
||||
if (y <= 0 || x == 0)
|
||||
continue;
|
||||
uint16_t color = 0xF9A0;
|
||||
|
||||
int _x = x;
|
||||
int _y = y;
|
||||
if (_y <= 64)
|
||||
{
|
||||
dma_display->drawPixel(_x, _y, color);
|
||||
}
|
||||
else
|
||||
{
|
||||
_y = _y - 64;
|
||||
_x = _x + 128;
|
||||
|
||||
dma_display->drawPixel(_x, _y, color);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
void draw_spacecraft(int x, int y = 100)
|
||||
{
|
||||
for (int i = 0; i < width; i++)
|
||||
{
|
||||
for (int j = 0; j < height; j++)
|
||||
{
|
||||
uint16_t color = aircraft[j * width + i];
|
||||
if (color == 0xFFFF || color == 0xFFDF || color == 0xFFFE || color == 0xFFBD || color == 0xFFBF)
|
||||
color = 0x0000;
|
||||
int _x = x + i;
|
||||
int _y = y + j;
|
||||
if (_y <= 64)
|
||||
{
|
||||
dma_display->drawPixel(_x, _y, color);
|
||||
}
|
||||
else
|
||||
{
|
||||
_y = _y - 64;
|
||||
_x = _x + 128;
|
||||
|
||||
dma_display->drawPixel(_x, _y, color);
|
||||
}
|
||||
}
|
||||
}
|
||||
exist = true;
|
||||
// draw_rocket(x,y);
|
||||
}
|
||||
int frame_count = num_frames;
|
||||
unsigned long last_frame_time = millis();
|
||||
int frame_rate = 100;
|
||||
|
||||
void S_frame_0(int x, int y = 100)
|
||||
{
|
||||
for (int i = 0; i < width; i++)
|
||||
{
|
||||
for (int j = 0; j < height; j++)
|
||||
{
|
||||
uint16_t color = gif_frame_0[j * width + i];
|
||||
if (color == 0xFFFF || color == 0xFFDF || color == 0xFFFE || color == 0xFFBD || color == 0xFFBF)
|
||||
color = 0x0000;
|
||||
int _x = x + i;
|
||||
int _y = y + j;
|
||||
if (_y <= 64)
|
||||
{
|
||||
dma_display->drawPixel(_x, _y, color);
|
||||
}
|
||||
else
|
||||
{
|
||||
_y = _y - 64;
|
||||
_x = _x + 128;
|
||||
|
||||
dma_display->drawPixel(_x, _y, color);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
void S_frame_1(int x, int y = 100)
|
||||
{
|
||||
for (int i = 0; i < width; i++)
|
||||
{
|
||||
for (int j = 0; j < height; j++)
|
||||
{
|
||||
uint16_t color = gif_frame_1[j * width + i];
|
||||
if (color == 0xFFFF || color == 0xFFDF || color == 0xFFFE || color == 0xFFBD || color == 0xFFBF)
|
||||
color = 0x0000;
|
||||
int _x = x + i;
|
||||
int _y = y + j;
|
||||
if (_y <= 64)
|
||||
{
|
||||
dma_display->drawPixel(_x, _y, color);
|
||||
}
|
||||
else
|
||||
{
|
||||
_y = _y - 64;
|
||||
_x = _x + 128;
|
||||
|
||||
dma_display->drawPixel(_x, _y, color);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
void S_frame_2(int x, int y = 100)
|
||||
{
|
||||
for (int i = 0; i < width; i++)
|
||||
{
|
||||
for (int j = 0; j < height; j++)
|
||||
{
|
||||
uint16_t color = gif_frame_2[j * width + i];
|
||||
if (color == 0xFFFF || color == 0xFFDF || color == 0xFFFE || color == 0xFFBD || color == 0xFFBF)
|
||||
color = 0x0000;
|
||||
int _x = x + i;
|
||||
int _y = y + j;
|
||||
if (_y <= 64)
|
||||
{
|
||||
dma_display->drawPixel(_x, _y, color);
|
||||
}
|
||||
else
|
||||
{
|
||||
_y = _y - 64;
|
||||
_x = _x + 128;
|
||||
|
||||
dma_display->drawPixel(_x, _y, color);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
void S_frame_3(int x, int y = 100)
|
||||
{
|
||||
for (int i = 0; i < width; i++)
|
||||
{
|
||||
for (int j = 0; j < height; j++)
|
||||
{
|
||||
uint16_t color = gif_frame_3[j * width + i];
|
||||
if (color == 0xFFFF || color == 0xFFDF || color == 0xFFFE || color == 0xFFBD || color == 0xFFBF)
|
||||
color = 0x0000;
|
||||
int _x = x + i;
|
||||
int _y = y + j;
|
||||
if (_y <= 64)
|
||||
{
|
||||
dma_display->drawPixel(_x, _y, color);
|
||||
}
|
||||
else
|
||||
{
|
||||
_y = _y - 64;
|
||||
_x = _x + 128;
|
||||
|
||||
dma_display->drawPixel(_x, _y, color);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
void S_frame_4(int x, int y = 100)
|
||||
{
|
||||
for (int i = 0; i < width; i++)
|
||||
{
|
||||
for (int j = 0; j < height; j++)
|
||||
{
|
||||
uint16_t color = gif_frame_4[j * width + i];
|
||||
if (color == 0xFFFF || color == 0xFFDF || color == 0xFFFE || color == 0xFFBD || color == 0xFFBF)
|
||||
color = 0x0000;
|
||||
int _x = x + i;
|
||||
int _y = y + j;
|
||||
if (_y <= 64)
|
||||
{
|
||||
dma_display->drawPixel(_x, _y, color);
|
||||
}
|
||||
else
|
||||
{
|
||||
_y = _y - 64;
|
||||
_x = _x + 128;
|
||||
|
||||
dma_display->drawPixel(_x, _y, color);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
void draw_spacecraft_2(int x, int y = 100)
|
||||
{
|
||||
unsigned long now = millis();
|
||||
if(now - last_frame_time >= frame_rate){
|
||||
frame_count --;
|
||||
last_frame_time = now;
|
||||
switch (frame_count){
|
||||
case 0:
|
||||
S_frame_0(x, y);
|
||||
break;
|
||||
case 1:
|
||||
S_frame_1(x, y);
|
||||
break;
|
||||
case 2:
|
||||
S_frame_2(x, y);
|
||||
break;
|
||||
case 3:
|
||||
S_frame_3(x, y);
|
||||
break;
|
||||
case 4:
|
||||
S_frame_4(x, y);
|
||||
break;
|
||||
default:
|
||||
frame_count = num_frames;
|
||||
break;
|
||||
}
|
||||
if(frame_count == 0) frame_count = num_frames;
|
||||
}
|
||||
exist = true;
|
||||
// draw_rocket(x,y);
|
||||
else return false;
|
||||
}
|
||||
void setup()
|
||||
{
|
||||
|
@ -799,14 +189,6 @@ void setup()
|
|||
dma_display->begin(); // setup the LED matrix
|
||||
dma_display->setBrightness8(200); // 0-255
|
||||
dma_display->clearScreen();
|
||||
// draw_spacecraft(32, 32);
|
||||
Mode_game = true;
|
||||
// create_enemies();
|
||||
// draw_enemies();
|
||||
// draw_enemy(32,32);
|
||||
// draw_rocket(32,100);
|
||||
// draw_cat(10,10,20,20);
|
||||
// drawXbm565(10,10,20,20,CAT_bits,dma_display->color565(100,0,0));
|
||||
}
|
||||
|
||||
void loop()
|
||||
|
@ -836,23 +218,10 @@ void loop()
|
|||
memcpy(&numRows, buffer, sizeof(int));
|
||||
memcpy(&numCols, buffer + sizeof(int), sizeof(int));
|
||||
|
||||
if (numRows == 1 && numCols == 0)
|
||||
{
|
||||
Mode_game = true;
|
||||
// dma_display->clearScreen();
|
||||
// Serial.println("Stick Man");
|
||||
}
|
||||
else if (numRows == 0 && numCols == 0)
|
||||
{
|
||||
Mode_game = false;
|
||||
// Serial.println("HungVat");
|
||||
// dma_display->clearScreen();
|
||||
}
|
||||
|
||||
int expectedSize = numRows * numCols * floatSize;
|
||||
if (bufferIndex >= (2 * sizeof(int) + expectedSize))
|
||||
{
|
||||
dma_display->clearScreen();
|
||||
//dma_display->clearScreen();
|
||||
|
||||
for (int row = 0; row < numRows; ++row)
|
||||
{
|
||||
|
@ -864,45 +233,23 @@ void loop()
|
|||
data[row][col] = value;
|
||||
}
|
||||
}
|
||||
|
||||
if (!Mode_game)
|
||||
if (set_FPS(20))
|
||||
{
|
||||
Drawbar(int(data[0][0]));
|
||||
if (player.alive)
|
||||
{
|
||||
int x = (int)data[0][0];
|
||||
updatePlayer(x);
|
||||
updateEnemies();
|
||||
updateBullet();
|
||||
checkCollisions();
|
||||
render();
|
||||
}
|
||||
else
|
||||
{
|
||||
X_craft = int(data[0][0]);
|
||||
//draw_spacecraft(X_craft);
|
||||
draw_spacecraft_2(X_craft);
|
||||
draw_rocket(X_craft);
|
||||
}
|
||||
|
||||
// Reset chỉ số chỉ mục sau khi xử lý
|
||||
}
|
||||
bufferIndex = 0;
|
||||
}
|
||||
// Stickman region
|
||||
if (Mode_game)
|
||||
{
|
||||
if (exist)
|
||||
{
|
||||
draw_spacecraft_2(X_craft);
|
||||
draw_rocket(X_craft);
|
||||
if (live_enemies <= 0)
|
||||
{
|
||||
create_enemies();
|
||||
}
|
||||
draw_enemies();
|
||||
destroy_enemy();
|
||||
}
|
||||
}
|
||||
else // Hung Vat
|
||||
{
|
||||
DrawObject(true);
|
||||
DrawObject();
|
||||
HeldObject();
|
||||
}
|
||||
#pragma endregion
|
||||
|
||||
// readDataFromSerial();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue