//EGE图形库
#include <graphics.h>
#include "head.h"
void ShowBack();
void Getimg();
Player play;
PIMAGE img = newimage();
void Setup() {
int x = 620;
int y = 400;
int n;//障碍物高度
int v;//障碍物速度
int m = 0;
for (; is_run(); delay_fps(60)) {
cleardevice();
ShowBack();
Getimg();
xyprintf(0, 100, "分数:%d", m);
if (x == 620) {
n = 50 + rand() % 100;
v = 5 + rand() % 15;
}
bar(x, y, x + 20, y - n);
fillcircle(play.player_x, play.player_y, 5);
if (kbhit()) {
char key = getch();
switch (key) {
case'd':play.player_Right();
break;
case'a':play.player_Left();
break;
case' ':play.player_Jump();
break;
case'j':play.player_Attack();
break;
default:
break;
}
}
play.player_Dateup();
if (play.player_x > x-5 && play.player_x < x + 20 && play.player_y > y - n && play.player_y < y) {
outtextxy(310, 220, "Game Over");
outtextxy(300, 250, "1-重新开始-1");
outtextxy(300, 280, "2-退出游戏-2");
return;
}
x -= v;
if (x < 0) {
x = 620;
m++;
}
}
}
void ShowBack() {
setlinecolor(GREEN);
setlinewidth(2);
setfillcolor(YELLOW);
line(0, 0, 10, 0);
line(0, 10, 10, 10);
line(0, 0, 0, 10);
line(10, 0, 10, 10);
fillcircle(5, 5, 3.5);
}
//画背景(胡乱画的)
void Getimg() {
getimage(img, 0, 0, 10, 10);
for (int i = 0; i < 640; i += 10) {
for (int j = 400; j < 480; j += 10) {
putimage(i, j, img);
}
}
for (int x = 0; x < 640; x += 10) {
for (int y = 0; y < 50; y += 10) {
putimage(x, y, img);
}
}
}
void Menu() {
initgraph(WinWidth, WinHeight, 0);
setbkcolor(BLACK);
setfillcolor(BLACK);
setbkmode(1);
setlinewidth(12);
setlinecolor(GREEN);
setcaption("小小球-1.0");
void(*p)() = &Setup;
setfont(20, 0, "幼圆");
outtextxy(250, 100, "1-开始游戏-1");
outtextxy(250, 150, "2-退出游戏-2");
while (1) {
char key = getch();
switch (key) {
case '1':p();
break;
case '2':exit(0);
break;
default:
break;
}
}
}
int main() {
Menu();
}
#pragma once
#define WinWidth 640
#define WinHeight 480
class Player {
public:
Player();
~Player();
void player_Left();//向左
void player_Right();//向右
void player_Jump();//跳跃
void player_Attack();//攻击
void player_Dateup();//更新状态
public:
float player_x;//小球坐标
float player_y;
float player_vx;//初速度
float player_vy;
float g;//重力加速度
float a;//阻力加速度
bool player_downup;//是否处于地面
};
Player::Player() {
player_x = 100.0;
player_y = 396.0;
player_vx = 0.0;
player_vy = 0.0;
g = -1.0;
a = -1.0;
player_downup = true;
}
Player::~Player() {
//析构函数......
}
void Player::player_Attack() {
//攻击函数......
}
void Player::player_Jump() {
if (player_downup) {
player_vy = -20.0;
player_downup = false;
}
}
void Player::player_Left() {
player_vx = 20.0;
}
void Player::player_Right() {
player_vx = -20.0;
}
void Player::player_Dateup() {
if (!player_downup) {
player_vy = player_vy - g;
player_y = player_y + player_vy;
}
if (player_y > 396) {
player_y = 396;
player_vy = 0;
player_downup = true;
}
if (player_vx > 0) {
player_vx += a;
player_x -= player_vx;
}
else if (player_vx < 0) {
player_vx -= a;
player_x -= player_vx;
}
if (player_x < 10) {
player_x = 10;
}
if (player_x > 630) {
player_x = 630;
}
}
效果如下:
有兴趣的可以搞成个小游戏