easyx贪吃蛇(分文件)
时间: 2023-11-27 15:28:24 浏览: 83
Easyx贪吃蛇
好的,下面是分文件的 EasyX 贪吃蛇代码:
main.cpp:
```cpp
#include "SnakeGame.h"
int main()
{
SnakeGame game;
game.run();
return 0;
}
```
SnakeGame.h:
```cpp
#pragma once
#include <graphics.h>
#include "Snake.h"
#include "Food.h"
class SnakeGame
{
public:
SnakeGame();
~SnakeGame();
void run();
private:
void init();
void update();
void draw();
void generateFood();
bool isGameOver();
private:
Snake m_snake;
Food m_food;
int m_score;
int m_speed;
bool m_isOver;
};
```
SnakeGame.cpp:
```cpp
#include "SnakeGame.h"
SnakeGame::SnakeGame()
{
init();
}
SnakeGame::~SnakeGame()
{
closegraph();
}
void SnakeGame::run()
{
while (!m_isOver)
{
update();
draw();
if (isGameOver())
m_isOver = true;
Sleep(200 - m_speed * 15);
}
outtextxy(200, 200, "Game Over!");
}
void SnakeGame::init()
{
initgraph(640, 480);
setbkcolor(WHITE);
settextcolor(BLACK);
setlinecolor(BLACK);
m_score = 0;
m_speed = 1;
m_isOver = false;
generateFood();
}
void SnakeGame::update()
{
m_snake.move();
if (m_snake.isEatFood(m_food))
{
m_score += 10;
m_speed = m_score / 100 + 1;
generateFood();
}
}
void SnakeGame::draw()
{
cleardevice();
m_snake.draw();
m_food.draw();
char scoreStr[10];
sprintf(scoreStr, "Score: %d", m_score);
outtextxy(10, 10, scoreStr);
}
void SnakeGame::generateFood()
{
int x = rand() % (640 / 10) * 10;
int y = rand() % (480 / 10) * 10;
m_food.setPosition(x, y);
}
bool SnakeGame::isGameOver()
{
if (m_snake.isHitWall() || m_snake.isHitSelf())
return true;
return false;
}
```
Snake.h:
```cpp
#pragma once
#include <vector>
#include <graphics.h>
class Snake
{
public:
Snake();
~Snake();
void move();
void draw();
bool isHitWall();
bool isHitSelf();
bool isEatFood(const class Food& food);
private:
std::vector<POINT> m_body;
enum Direction { UP, DOWN, LEFT, RIGHT } m_direction;
};
```
Snake.cpp:
```cpp
#include "Snake.h"
#include "Food.h"
Snake::Snake()
{
m_body.push_back({ 320, 240 });
m_body.push_back({ 310, 240 });
m_body.push_back({ 300, 240 });
m_direction = RIGHT;
}
Snake::~Snake()
{
}
void Snake::move()
{
for (int i = m_body.size() - 1; i > 0; i--)
m_body[i] = m_body[i - 1];
switch (m_direction)
{
case UP:
m_body[0].y -= 10;
break;
case DOWN:
m_body[0].y += 10;
break;
case LEFT:
m_body[0].x -= 10;
break;
case RIGHT:
m_body[0].x += 10;
break;
}
}
void Snake::draw()
{
setfillcolor(BLACK);
for (auto& p : m_body)
fillrectangle(p.x, p.y, p.x + 10, p.y + 10);
}
bool Snake::isHitWall()
{
if (m_body[0].x < 0 || m_body[0].x >= 640 || m_body[0].y < 0 || m_body[0].y >= 480)
return true;
return false;
}
bool Snake::isHitSelf()
{
for (int i = 1; i < m_body.size(); i++)
if (m_body[0].x == m_body[i].x && m_body[0].y == m_body[i].y)
return true;
return false;
}
bool Snake::isEatFood(const class Food& food)
{
if (m_body[0].x == food.getX() && m_body[0].y == food.getY())
{
m_body.push_back(m_body[m_body.size() - 1]);
return true;
}
return false;
}
```
Food.h:
```cpp
#pragma once
#include <graphics.h>
class Food
{
public:
Food();
~Food();
void draw();
void setPosition(int x, int y);
int getX() const { return m_x; }
int getY() const { return m_y; }
private:
int m_x;
int m_y;
};
```
Food.cpp:
```cpp
#include "Food.h"
Food::Food()
{
m_x = 0;
m_y = 0;
}
Food::~Food()
{
}
void Food::draw()
{
setfillcolor(RED);
fillrectangle(m_x, m_y, m_x + 10, m_y + 10);
}
void Food::setPosition(int x, int y)
{
m_x = x;
m_y = y;
}
```
阅读全文