当前位置:网站首页>Easyx----- C language implementation 2048
Easyx----- C language implementation 2048
2022-06-26 11:02:00 【Sauerkraut】



tool.hpp
#pragma once
#include<easyx.h>
void drawImg(int x, int y,IMAGE *src)
{
// Variable initialization
DWORD* pwin = GetImageBuffer(); // Window buffer pointer
DWORD* psrc = GetImageBuffer(src); // Picture buffer pointer
int win_w = getwidth(); // The window is wide and high
int win_h = getheight();
int src_w = src->getwidth(); // The picture is wide and high
int src_h = src->getheight();
// Calculate the actual length and width of the map
int real_w = (x + src_w > win_w) ? win_w - x : src_w; // The processing goes beyond the right boundary
int real_h = (y + src_h > win_h) ? win_h - y : src_h; // Processing beyond the lower boundary
if (x < 0) { psrc += -x; real_w -= -x; x = 0; } // Processing goes beyond the left boundary
if (y < 0) { psrc += (src_w * -y); real_h -= -y; y = 0; } // Processing goes beyond the upper boundary
// Fixed map start position
pwin += (win_w * y + x);
// Achieve transparency mapping
for (int iy = 0; iy < real_h; iy++)
{
for (int ix = 0; ix < real_w; ix++)
{
// Calculate the value of the transparent channel [0,256) 0 Is completely transparent 255 Is completely opaque
byte a = (byte)(psrc[ix] >> 24);
if (a > 100)
{
pwin[ix] = psrc[ix];
}
}
// Switch to the next line
pwin += win_w;
psrc += src_w;
}
}
void drawImg(int x, int y, int dstW, int dstH, IMAGE* src, int srcX, int srcY)
{
// Variable initialization
DWORD* pwin = GetImageBuffer(); // Window buffer pointer
DWORD* psrc = GetImageBuffer(src); // Picture buffer pointer
int win_w = getwidth(); // The window is wide and high
int win_h = getheight();
int src_w = src->getwidth(); // The picture is wide and high
int src_h = src->getheight();
// Calculate the actual length and width of the map
int real_w = (x + dstW > win_w) ? win_w - x : dstW; // The processing goes beyond the right boundary
int real_h = (y + dstH > win_h) ? win_h - y : dstH; // Processing beyond the lower boundary
if (x < 0) { psrc += -x; real_w -= -x; x = 0; } // Processing goes beyond the left boundary
if (y < 0) { psrc += (dstW * -y); real_h -= -y; y = 0; } // Processing goes beyond the upper boundary
//printf("realw,h(%d,%d)\n", real_w, real_h);
// Fixed map start position
pwin += (win_w * y + x);
// Achieve transparency mapping
for (int iy = 0; iy < real_h; iy++)
{
for (int ix = 0; ix < real_w; ix++)
{
// Calculate the value of the transparent channel [0,256) 0 Is completely transparent 255 Is completely opaque
byte a = (byte)(psrc[ix + srcX + srcY * src_w] >> 24);
if (a > 100)
{
pwin[ix] = psrc[ix + srcX + srcY * src_w];
}
}
// Switch to the next line
pwin += win_w;
psrc += src_w;
}
}Source .cpp
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<easyx.h>
#include<math.h>
#include<conio.h>
#include"tools.hpp"
#define ROW 4
#define COL 4
#define INTERVAL 15 // The space between the grids
#define GRID_SIZE 117 // The width and height of the grid
//38 * 195
IMAGE bk; // background
// An array of data
int map[ROW][COL];
int flag = 0; // Whether to generate a number
// Define image resources
IMAGE imgs[11];
// Special treatment of blank pictures
IMAGE zero;
void loadResource()
{
loadimage(&bk, "./pieces/bk.png", 584, 734);
loadimage(&zero, "./pieces/0.png", GRID_SIZE, GRID_SIZE);
for (int i = 2,j = 0; i <= 2048; i *= 2,j++)
{
char imgPath[50] = { 0 }; // String formatting
sprintf_s(imgPath, "./pieces/%d.png", i); // buffer ( Array ) Format string
for (int i = 0; i < 17; i++) // The character array must use for Cycle to print | Out-of-service "%s" How to print
{
printf("%c", imgPath[i]);
}
putchar('\n');
// Be careful : Pictures should be output one by one
loadimage(&imgs[j], imgPath, GRID_SIZE, GRID_SIZE); // Multibyte character set
}
}
// Randomly generated 2or4 2 The probability is higher
int createNumber()
{
if (rand() % 10 != 0)
{
return 2;
}
else
{
return 4;
}
}
// Fill the empty space of the array with a number
void mapFillNumber()
{
// Generate two subscripts randomly
while (true)
{
// When subscripts are generated many times , May repeat 0 1 0 1 Although the probability is small, it is still possible
int r = rand() % ROW; //0 1 2 3
int c = rand() % COL; //0 1 2 3
// Avoid problems First, judge whether the original position is equal to 0 If it is equal to 0 Before putting data
if (map[r][c] == 0)
{
map[r][c] = createNumber();
return;
}
}
}
// initialization
void init()
{
// Set random number seed
srand(time(NULL));
// Randomly generate a number , And put it in the array map[?][?] = createNumber();
for (int i = 0; i < 2; i++)
{
mapFillNumber();
}
}
// draw
void draw()
{
for (int i = 0; i < ROW; i++)
{
for (int k = 0; k < COL; k++)
{
// Find the coordinates of the upper left corner of each grid
int x = k * GRID_SIZE + (k + 1) * INTERVAL + 38-18;
int y = i * GRID_SIZE + (i + 1) * INTERVAL + 195-17;
switch (map[i][k]) {
case 0:
drawImg(x, y, &zero);
break;
case 2:
drawImg(x, y, &imgs[0]);
break;
case 4:
drawImg(x, y, &imgs[1]);
break;
case 8:
drawImg(x, y, &imgs[2]);
break;
case 16:
drawImg(x, y, &imgs[3]);
break;
case 32:
drawImg(x, y, &imgs[4]);
break;
case 64:
drawImg(x, y, &imgs[5]);
break;
case 128:
drawImg(x, y, &imgs[6]);
break;
case 256:
drawImg(x, y, &imgs[7]);
break;
case 512:
drawImg(x, y, &imgs[8]);
break;
case 1024:
drawImg(x, y, &imgs[9]);
break;
case 2048:
drawImg(x, y, &imgs[10]);
break;
}
}
printf("\n");
}
}
// Move up
void moveUp()
{
/*
Move up : Traverse each column
*/
for (int i = 0; i < COL; i++)
{
int temp = 0;
for (int begin = 1; begin < ROW; begin++)
{
if (map[begin][i] != 0)
{
if (map[temp][i] == 0)
{
map[temp][i] = map[begin][i];
map[begin][i] = 0;
}
else if (map[temp][i] == map[begin][i])
{
map[temp][i] += map[begin][i];
map[begin][i] = 0;
temp++;
}
else
{
map[temp + 1][i] = map[begin][i];
if (temp + 1 != begin)// If temp+1 and begn Not in the same place , let begin The number of the position is null
{
map[begin][i] = 0;
}
temp++;
}
flag = 1;
}
}
}
}
// Move down the
void moveDown()
{
for (int i = 0; i < COL; i++)
{
int temp = ROW - 1;
for (int begin = ROW - 2; begin >= 0; begin--)
{
if (map[begin][i] != 0)
{
if (map[temp][i] == 0)
{
map[temp][i] = map[begin][i];
map[begin][i] = 0;
}
else if (map[temp][i] == map[begin][i])
{
map[temp][i] += map[begin][i];
map[begin][i] = 0;
temp--;
}
else
{
map[temp - 1][i] = map[begin][i];
if ((temp - 1) != begin)
{
map[begin][i] = 0;
}
temp--;
}
flag = 1;
}
}
}
}
// Move to the left
void moveLeft()
{
for (int i = 0; i < COL; i++)
{
int temp = 0;
for (int begin = 1; begin < ROW; begin++)
{
if (map[i][begin] != 0)
{
if (map[i][temp] == 0)
{
map[i][temp] = map[i][begin];
map[i][begin] = 0;;
}
else if (map[i][temp] == map[i][begin])
{
map[i][temp] += map[i][begin];
map[i][begin] = 0;
temp++;
}
else
{
map[i][temp + 1] = map[i][begin];
if (temp + 1 != begin)
{
map[i][begin] = 0;
}
temp++;
}
flag = 1;
}
}
}
}
// To the right
void moveRight()
{
for (int i = 0; i < ROW; i++)
{
int temp = COL - 1;
for (int begin = COL - 2; begin >= 0; begin--)
{
if (map[i][begin] != 0)
{
if (map[i][temp] == 0)
{
map[i][temp] = map[i][begin];
map[i][begin] = 0;
}
else if (map[i][temp] == map[i][begin])
{
map[i][temp] += map[i][begin];
map[i][begin] = 0;
temp--;
}
else
{
map[i][temp - 1] = map[i][begin];
if (temp - 1 != begin)
{
map[i][begin] = 0;
}
temp--;
}
flag = 1;
}
}
}
}
// Move the grid
void move()
{
// Get the keyboard keys 72 80 75 77
int key = _getch();
switch (key)
{
case 'W':
case 'w':
case 72:
moveUp();
break;
case 80:
moveDown();
break;
case 75:
moveLeft();
break;
case 77:
moveRight();
break;
}
if (flag == 1)
{
mapFillNumber();
flag = 0;
}
}
int main()
{
// window
initgraph(584,734, EW_SHOWCONSOLE);
init();
loadResource();
drawImg(0, 0, &bk);
while (true)
{
draw();
move();
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
printf("%d", map[i][j]);
}
printf("\n");
}
}
getchar();
return 0;
}边栏推荐
猜你喜欢

How does unity prevent other camera positions and rotations from being controlled by steamvrplugin when using steamvrplugin

Flutter and native communication (Part 1)

栖霞市住建局和消防救援大队开展消防安全培训

JWT (SSO scheme) + three ways of identity authentication

02 linked list of redis data structure
![[deep learning theory] (7) long and short term memory network LSTM](/img/fb/57781998390e0722df68e6c2e49098.gif)
[deep learning theory] (7) long and short term memory network LSTM

3、 Linked list exercise

(Typora图床)阿里云oss搭建图床+Picgo上传图片详细教程

Code specification & explain in detail the functions and uses of husky, prettier, eslint and lint staged

Postman入门教程
随机推荐
Which PHP open source works deserve attention
Easyx-----c语言实现2048
Introduction to sysbench Basics
2、 Linear table
Oracle sqlplus query result display optimization
24 个必须掌握的数据库面试问题!
laravel 写原生SQL语句
jwt认证协议阐述之——我开了一家怡红院
Fabric. JS upper dash, middle dash (strikethrough), underline
3、 Linked list exercise
Installing MySQL under Linux [details]
10年程序员职业生涯感悟—写给正在迷茫的你
Oracle11g reports an error when starting the database ora-27154: post/wait create failed
OpenCV图像处理-灰度处理
MySQL 30 military regulations
Fabric.js 上划线、中划线(删除线)、下划线
[difficult and miscellaneous diseases] @transitional failure summary
mysql性能监控和sql语句
QT连接MySql数据查询失败
指南针软件买股票进行交易安全吗?怎么开户买股票