#小游戏
先加头文件:
//graph.h
#include<graphics.h>
#ifndef usemain
int EGEAPI egeMain();
int main(){
int id=egeMain();
return id;
}
#endif
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<fstream>
#include<cctype>
#include<bits/stdc++.h>
#include<cmath>
用的时候就直接
#include"graph.h"
画背景:
class star{
public:
star(){}
int x;
int y;
star(int x1,int y1){x=x1;y=y1;}
int draw(){
setfillcolor(WHITE);
bar(x,y,x+1,y+1);
}
};
class RSG{
public:
RSG(){}
star st[600];
int rd(){
return rand()%600;
}
int ini(){
for(int i=0;i<600;++i){
st[i]=star(rd(),rd());
}
}
int draw(){
for(int i=0;i<600;++i){
st[i].draw();
}
}
int move(){
cleardevice();
for(int i=0;i<600;++i){
st[i].y+=6;
if(st[i].y>599){
st[i].y=0;
}
}
draw();
delay(15);
}
};
先试试效果:
int EGEAPI egeMain(){
initgraph(400,400);
RSG j;
j.ini();
while(1){
j.move();
}
getch();
closegraph();
return 0;
}
这是移动的,但我的电脑不能录屏
插入图片
加到D:里
#include"graph.h"
class star{
public:
star(){}
int x;
int y;
star(int x1,int y1){x=x1;y=y1;}
int draw(){
setfillcolor(WHITE);
bar(x,y,x+1,y+1);
}
};
PIMAGE img;
class RSG{
public:
RSG(){}
star st[600];
int rd(){
return rand()%600;
}
int ini(){
for(int i=0;i<600;++i){
st[i]=star(rd(),rd());
}
}
int draw(){
for(int i=0;i<600;++i){
st[i].draw();
}
}
int move(){
cleardevice();
putimage(100,100,img);
for(int i=0;i<600;++i){
st[i].y+=6;
if(st[i].y>599){
st[i].y=0;
}
}
draw();
delay(10);
}
};
int EGEAPI egeMain(){
initgraph(400,400);
img = newimage(100,178);
getimage(img,"D:\\33.jpg");
RSG j;
j.ini();
while(1){
j.move();
putimage(100,100,img);
if(kbhit()){
break;
}
}
delimage(img);
getch();
closegraph();
return 0;
}
(未完待续)
未完待续
最后附上我的贪吃蛇:
/*
apple can't be in
x<=9&&y<=3;
x==1||x==39||y==1||y==39
x==0||x==40||y==0||y==40
Name: rsnack 3.4
Copyright:2020-2090
Author: cleverman
Date: 06/02/20 22:38
Description: nullptr
*/
#include<windows.h>
#define HIDE_CONSOLE 1
#ifdef SHOW_CONSOLE
#undef SHOW_CONSOLE
#endif
#include<graphics.h>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<iostream>
#define H 400
#define W 400
#define maxseg 100
#define blocksize 10
#define blockw (W/blocksize)
#define blockh (H/blocksize)
#define apple 1204
#define snake 2048
#define _ma(u,i) u##i
#define ma(u,i) _ma(u,i)
char name[100];
typedef class CTX{
public:
CTX():color(BLACK){}
int Rect(int x=0,int y=0,int height=0,int weight=0,bool isfill=false){
setfillcolor(color);
setcolor(color);
if(!isfill)rectangle(x,y,x+weight,y+height);
else{
bar(x,y,x+weight,y+height);
}
}
int sh(){
setfont(20,0,"Courier New");
}
int showtxt(int x,int y){
outtextxy(x,y,"score: ");
}
int setcc(color_t colorr){
color=colorr;
}
int circ(int x,int y,int radius,bool c=true){
setcolor(color);
setfillcolor(color);
circle(x, y, radius);
if(c)fillellipse(x,y,radius,radius);
}
inline int cls(){ cleardevice(); }
int init0(){
initgraph(400,400);
setcaption("my snake");
}
color_t color;
} Ctx;
CTX ctx;
int score;
int drawborder(void){
ctx.setcc(DARKGRAY);
ctx.Rect(0,0,blocksize,W,true);
ctx.Rect(0,H-blocksize,blocksize,W,true);
ctx.Rect(0,0,H,blocksize,true);
ctx.Rect(W-blocksize,0,H,blocksize,true);
}
int other(){
ctx.setcc(CYAN);
ctx.Rect(blocksize,blocksize,H-2*blocksize,W-2*blocksize,true);
}
int drawpaddle(){
drawborder();
other();
}
int drawnum(int num){
char str[10];
sprintf(str, "%d",num/2);
ctx.setcc(BROWN);
ctx.showtxt(10,10);
outtextxy(55,10,str);
ctx.setcc(WHITE);
char stra[110];
sprintf(stra,"name:%s",name);
outtextxy(300,0,stra,0);
}
int drawback(){
drawpaddle();
drawnum(score);
}
int EGEAPI gameover(){
PIMAGE img=newimage();
getimage(img,"D:\\55.jpg");
putimage(0,0,img);
delay_ms(5000);
delimage(img);
MessageBoxA(NULL,"GAME OVER! \n you died.\a","rsnake4.1",MB_OK);
closegraph();
exit(0);
}
bool allowed(int x,int y){
bool ma(allow,1) = x<=9&&y<=3;
bool ma(allow,2) = x==2||x==39||y==2||y==39;
bool ma(allow,3) = x==1||x==40||y==1||y==40;
return !allow1&&!allow2&&!allow3;
}
//ongame
class block{
public:
block(int a=0){}
block(int col,int row):x(col),y(row){}
int x,y;
int draw(int rr){
int xx=x*blocksize,yy=y*blocksize;
if(rr==snake) {
ctx.setcc(BLUE);ctx.Rect(xx,yy,10,10,1);}
if(rr==apple) {
ctx.setcc(GREEN);ctx.circ(xx+5,yy+5,5,1);}
}
friend bool operator ==(block a,block b){
return a.x==b.x&&a.y==b.y;
}
block operator =(block b){
x=b.x;
y=b.y;
return *this;
}
};
block popa(block arr[],int &len){
block c;
c=arr[len-1];
arr[--len]=block(-1,-1);
return c;
}
int unshifta(block arr[],int& len,const int biggestlen,block what){
if(biggestlen<=len) return len;
block c[len];
for(int i=0;i<len;i++){
c[i]=arr[i];
}
arr[0]=what;
for(int j=0;j<len;j++){
arr[j+1]=c[j];
}
++len;
return len;
}
class Apple{
public:
Apple(){ position=block(20,8); }
block position;
int draw(){
position.draw(apple);
}
int move(){
int rx,ry;
srand(time(0));
rx=(rand()%38)+2;
ry=(rand()%38)+2;
if(allowed(rx,ry)){
position=block(rx,ry);++score;}
else {
position=block(10,10);++score;}
}
};
Apple app;
char gethit(void); //for Snake::forkey(void)
int time7=300;
class Snake{
public:
block seg[maxseg];
char dir;
int lenseg;
char dirn;
Snake(){
seg[0]=block(8,8);
seg[1]=block(7,8);
seg[2]=block(6,8);
dir='d';
dirn='d';
lenseg=3;
}
int draw(){
for(int i=0;i<lenseg;i++){
seg[i].draw(snake);
}
}
bool collection(block head){
bool l = (head.x==1);
bool t = (head.y==1);
bool r = (head.x==40);
bool b = (head.y==40);
bool wal = l||t||r||b;
bool slf = false;
for(int i=1;i<lenseg;i++){
if(head==seg[i]){
slf=true;
}
}
return wal||slf;
}
int move(){
block head = seg[0];
block newhead;
dir=dirn;
if(dir=='d') newhead = block(head.x+1, head.y);
if(dir=='s') newhead = block(head.x, head.y+1);
if(dir=='a') newhead = block(head.x-1, head.y);
if(dir=='w') newhead = block(head.x, head.y-1);
//char f[10];
//sprintf(f,"%d",int(collection(newhead)));
//MessageBoxA(0,f,f,MB_OK);
if(collection(newhead)) gameover();
unshifta(seg,lenseg,maxseg,newhead);
if(newhead==app.position){
score++;
time7-=6;
app.move();
} else {
popa(seg,lenseg);
}
draw();
}
void setdir(char ndir){
if(dir=='w'&&ndir=='s'){
return;
} else if(dir=='d'&&ndir=='a'){
return;
} else if(dir=='a'&&ndir=='d'){
return;
} else if(dir=='s'&&ndir=='w'){
return;
}
dirn=ndir;
}
void forkey(){
char nd = gethit();
if(nd!='\0') setdir(nd);
}
};
char gethit(){
char d = '\0';
if(kbhit()){
char ch=getch();
switch(ch){
case 'w':
case 'W':
d='w';
break;
case 's':
case 'S':
d='s';
break;
case 'a':
case 'A':
d='a';
break;
case 'd':
case 'D':
d='d';
break;
case ' ':
getch();
break;
default:
break;
}
}
return d;
}
//main
void start(void){
score = 0;
setinitmode(0);
ctx.init0();
PIMAGE img=newimage();
getimage(img,"D:\\54.jpg");
putimage(0,0,img);
delay_ms(7890);
delimage(img);
inputbox_getline("rsnake 4.1","please enter your name",name,100);
char aname[110];
sprintf(aname,"你好,%s",name);
MessageBox(0,aname,"rsnake 4.1",MB_OK);
cleardevice();
MessageBox(0,"开始前按[shift]关闭中文模式!\243\273","rsnake 3.4",MB_OK);
outtextxy(190,175,"按任意键继续...",NULL);
getch();
outtextxy(190,235,"Loading...",NULL);
delay_ms(5000);
}
int winpro(){
if(score>=30) {
PIMAGE img=newimage();
getimage(img,"D:\\56.jpg");
putimage(0,0,img);
delay_ms(5000);
delimage(img);
MessageBoxA(NULL,"GAME END! \n YOU WIN!!\a","rsnake4.1",MB_OK);
closegraph();
exit(0);
}
}
int main(){
start();
Snake sn;
while(1){
drawback();
app.draw();
sn.draw();
sn.forkey();
sn.move();
winpro();
delay_ms(time7);
ctx.cls();
}
return 0;
}
54.jpg:
55.jpg
56.jpg