update Stick Man

GameStickMan
CalisJI 2024-08-14 14:56:43 +07:00
parent 300e50e281
commit 02223a7c77
2 changed files with 382 additions and 183 deletions

43
src/index.html Normal file
View File

@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">
<style>
body {
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
background-color: #f0f0f0;
}
.container {
background: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.checkbox-group {
margin-bottom: 20px;
display: flex;
flex-direction: row;
}
.checkbox-group label {
display: block;
margin-bottom: 10px;
}
</style>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Checkbox Example</title>
</head>
<body>
<div class="container">
<div class="checkbox-group">
<label><input type="checkbox" id="auto" onchange="updateCheckbox('auto')"> Mode</label>
</div>
</div>
</body>
</html>

View File

@ -154,9 +154,9 @@ const char CAT_bits[] PROGMEM = {
0x28, 0xfc, 0xfc, 0x00, 0xc8, 0x82, 0x02, 0x00, 0x30, 0xc2, 0xfc, 0x00, 0xff, 0x5f, 0x7f, 0x00, 0x28, 0xfc, 0xfc, 0x00, 0xc8, 0x82, 0x02, 0x00, 0x30, 0xc2, 0xfc, 0x00, 0xff, 0x5f, 0x7f, 0x00,
0xc8, 0x22, 0x82, 0x00, 0xe8, 0xe2, 0x62, 0x00, 0xe8, 0xe2, 0x62, 0x00, 0xa8, 0xa2, 0xfc, 0x00, 0xc8, 0x22, 0x82, 0x00, 0xe8, 0xe2, 0x62, 0x00, 0xe8, 0xe2, 0x62, 0x00, 0xa8, 0xa2, 0xfc, 0x00,
0x7f, 0x3f, 0xbf, 0x00, 0x08, 0x82, 0xbc, 0x00, 0xc8, 0x82, 0x02, 0x00, 0xa8, 0x7c, 0x7c, 0x00, 0x7f, 0x3f, 0xbf, 0x00, 0x08, 0x82, 0xbc, 0x00, 0xc8, 0x82, 0x02, 0x00, 0xa8, 0x7c, 0x7c, 0x00,
0xc8, 0x7c, 0x7c, 0x00, 0xc8, 0x7c, 0x7c, 0x00, 0x28, 0x3c, 0x7c, 0x00, 0x28, 0x3c, 0xbc, 0x00 0xc8, 0x7c, 0x7c, 0x00, 0xc8, 0x7c, 0x7c, 0x00, 0x28, 0x3c, 0x7c, 0x00, 0x28, 0x3c, 0xbc, 0x00};
}; uint32_t rgb565_to_rgb888(uint16_t rgb565)
uint32_t rgb565_to_rgb888(uint16_t rgb565) { {
// Tách các kênh màu từ giá trị RGB565 // Tách các kênh màu từ giá trị RGB565
uint8_t r = (rgb565 >> 11) & 0x1F; // 5 bit cho đỏ uint8_t r = (rgb565 >> 11) & 0x1F; // 5 bit cho đỏ
uint8_t g = (rgb565 >> 5) & 0x3F; // 6 bit cho xanh lục uint8_t g = (rgb565 >> 5) & 0x3F; // 6 bit cho xanh lục
@ -172,15 +172,19 @@ uint32_t rgb565_to_rgb888(uint16_t rgb565) {
} }
void drawXbm565(int x, int y, int width, int height, const char *xbm, uint16_t color = 0xffff) void drawXbm565(int x, int y, int width, int height, const char *xbm, uint16_t color = 0xffff)
{ {
if (width % 8 != 0) { if (width % 8 != 0)
{
width = ((width / 8) + 1) * 8; width = ((width / 8) + 1) * 8;
} }
for (int i = 0; i < width * height / 8; i++ ) { for (int i = 0; i < width * height / 8; i++)
{
unsigned char charColumn = pgm_read_byte(xbm + i); unsigned char charColumn = pgm_read_byte(xbm + i);
for (int j = 0; j < 8; j++) { for (int j = 0; j < 8; j++)
{
int targetX = (i * 8 + j) % width + x; int targetX = (i * 8 + j) % width + x;
int targetY = (8 * i / (width)) + y; int targetY = (8 * i / (width)) + y;
if (bitRead(charColumn, j)) { if (bitRead(charColumn, j))
{
dma_display->drawPixel(targetX, targetY, color); dma_display->drawPixel(targetX, targetY, color);
} }
} }
@ -293,7 +297,6 @@ void Drawbar(int x)
MaxX_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 - 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)); dma_display->drawLine(MinX_bar, MinY_bar, MaxX_bar, MinY_bar, dma_display->color333(3, 3, 3));
} }
bool created = false; bool created = false;
@ -301,7 +304,7 @@ int X_vat = 20;
int Y_vat = 2; int Y_vat = 2;
int lastpoint[2]; int lastpoint[2];
int r = 2; int r = 2;
int speed = 500; int speed = 150;
void DrawObject(bool deleted = false) void DrawObject(bool deleted = false)
{ {
// measure x dimension // measure x dimension
@ -320,10 +323,12 @@ void DrawObject(bool deleted = false)
} }
else else
{ {
if(now-period_time>=speed){ if (now - period_time >= speed)
{
dma_display->fillCircle(X_vat, Y_vat, r, dma_display->color333(0, 0, 0)); dma_display->fillCircle(X_vat, Y_vat, r, dma_display->color333(0, 0, 0));
Y_vat += 1; Y_vat += 1;
if (Y_vat > 52)created = false; if (Y_vat > 52)
created = false;
period_time = now; period_time = now;
} }
} }
@ -354,7 +359,8 @@ void HeldObject()
dma_display->setCursor(2, 11); dma_display->setCursor(2, 11);
dma_display->printf("%d", score); dma_display->printf("%d", score);
} }
else{ else
{
dma_display->setFont(&FreeMono9pt7b); // Change to the desired font size dma_display->setFont(&FreeMono9pt7b); // Change to the desired font size
dma_display->setTextColor(COLOR_WHITE); dma_display->setTextColor(COLOR_WHITE);
dma_display->setCursor(2, 11); dma_display->setCursor(2, 11);
@ -367,10 +373,11 @@ void HeldObject()
int X_shot = 0; int X_shot = 0;
int Y_shot = 32; int Y_shot = 32;
bool created_shot = false; bool created_shot = false;
bool direction = false;
bool direction = false; // false = left, true = right;
int lastpoint_shot[2]; int lastpoint_shot[2];
unsigned long period_shot = millis(); unsigned long period_shot = millis();
int speed_shot = 300; int speed_shot = 100;
void Drawbullet(bool deleted = false) void Drawbullet(bool deleted = false)
{ {
// measure x dimension // measure x dimension
@ -392,12 +399,13 @@ void Drawbullet(bool deleted = false)
direction = true; direction = true;
} }
created_shot = true; created_shot = true;
dma_display->clearScreen();
dma_display->fillCircle(X_shot, Y_shot, 1, dma_display->color333(5, 0, 0)); dma_display->fillCircle(X_shot, Y_shot, 1, dma_display->color333(5, 0, 0));
} }
else else
{ {
if(now-period_shot>=speed_shot){ if (now - period_shot >= speed_shot)
{
dma_display->fillCircle(lastpoint_shot[0], lastpoint_shot[1], 1, dma_display->color333(0, 0, 0)); dma_display->fillCircle(lastpoint_shot[0], lastpoint_shot[1], 1, dma_display->color333(0, 0, 0));
if (direction == false) if (direction == false)
@ -406,7 +414,8 @@ void Drawbullet(bool deleted = false)
if (X_shot >= 126) if (X_shot >= 126)
created_shot = false; created_shot = false;
} }
else{ else
{
X_shot -= 1; X_shot -= 1;
if (X_shot <= 2) if (X_shot <= 2)
created_shot = false; created_shot = false;
@ -417,11 +426,19 @@ void Drawbullet(bool deleted = false)
period_shot = now; period_shot = now;
} }
} }
//dma_display->drawCircle(X_vat,Y_vat,1,dma_display->color333(7,0,0)); // dma_display->fillCircle(X_shot,Y_shot,1,dma_display->color333(5,0,0));
dma_display->fillCircle(X_vat,Y_vat,r,dma_display->color333(7,0,0));
// dma_display->drawCircle(X_vat,Y_vat,1,dma_display->color333(7,0,0));
} }
void drawStickMan(int x, int y)
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 // Vẽ đầu
dma_display->drawCircle(x, y, 2, rgbToUint16(255, 0, 0, false)); // Đầu stick man dma_display->drawCircle(x, y, 2, rgbToUint16(255, 0, 0, false)); // Đầu stick man
@ -434,20 +451,120 @@ void drawStickMan(int x, int y)
// Vẽ chân // 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 trái
dma_display->drawLine(x, y + 7, x + 3, y + 10, rgbToUint16(255, 0, 0, false)); // Chân phả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
}
}
}
}
} }
void setup() void setup()
{ {
Serial.begin(115200 * 2); // Đảm bảo baud rate khớp với baud rate của Python Serial.begin(115200 * 2); // Đảm bảo baud rate khớp với baud rate của Python
dma_display = new MatrixPanel_I2S_DMA(mxconfig); dma_display = new MatrixPanel_I2S_DMA(mxconfig);
dma_display->begin(); // setup the LED matrix dma_display->begin(); // setup the LED matrix
dma_display->setBrightness8(50); // 0-255 dma_display->setBrightness8(200); // 0-255
dma_display->clearScreen(); dma_display->clearScreen();
// draw_cat(10,10,20,20); // draw_cat(10,10,20,20);
// drawXbm565(10,10,20,20,CAT_bits,dma_display->color565(100,0,0)); // drawXbm565(10,10,20,20,CAT_bits,dma_display->color565(100,0,0));
} }
bool mode = false; bool mode = false;
int reset_stickman = 2000;
bool Mode_game = false;
void loop() void loop()
{ {
// server.handleClient(); // server.handleClient();
@ -474,10 +591,20 @@ void loop()
memcpy(&numRows, buffer, sizeof(int)); memcpy(&numRows, buffer, sizeof(int));
memcpy(&numCols, buffer + sizeof(int), 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; int expectedSize = numRows * numCols * floatSize;
if (bufferIndex >= (2 * sizeof(int) + expectedSize)) if (bufferIndex >= (2 * sizeof(int) + expectedSize))
{ {
dma_display->clearScreen(); dma_display->clearScreen();
@ -493,17 +620,46 @@ void loop()
} }
} }
if (!Mode_game)
{
Drawbar(int(data[0][0])); Drawbar(int(data[0][0]));
}
else
{
if (!game_over)
drawStickMan(int(data[0][0]), int(data[0][1]), int(data[0][2]));
}
// Reset chỉ số chỉ mục sau khi xử lý // Reset chỉ số chỉ mục sau khi xử lý
}
bufferIndex = 0; bufferIndex = 0;
} }
if (Mode_game)
{
if (game_over)
{
if (millis() - period_gameover >= reset_stickman)
{
game_over = false;
dma_display->clearScreen();
} }
//DrawObject(true); }
}
// Stickman region
if (Mode_game)
{
Drawbullet();
Defend();
}
else // Hung Vat
{
DrawObject(true);
DrawObject(); DrawObject();
HeldObject(); HeldObject();
}
#pragma endregion #pragma endregion
// readDataFromSerial(); // readDataFromSerial();