免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3225 | 回复: 5
打印 上一主题 下一主题

我自己写的游戏源码(for Linux and Unix) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-05-17 16:58 |只看该作者 |倒序浏览
pang是什么游戏不用多言了,希望对有兴趣学习opengl和X窗口编程的同志有用:

编译命令:g++ -lX11 -lGLU -lpthread <你保存的源码文件名> -o <输出的文件名>


//++++++++++++++++++++++++++++++++++++++++

//++++++++++THIS IS FREE SOURCE+++++++++++++

//++++++++++2009-5-17 by Little_Cat++++++++++++++

//++++++++++++++++++++++++++++++++++++++++


#include<iostream>
#include<GL/glx.h>
#include<GL/glu.h>
#include<X11/Xlib.h>
#include<time.h>
#include<cstdlib>
#include<pthread.h>
using namespace std;
//++++++++++++++++THE CLASS+++++++++++++++++++++++++++++++
class timect{     //the class about the time
public:
    clock_t l;
    void init();
    void mathl();
private:
    clock_t b;
};
    void timect::init(){
        b=clock();
        l=0;
    }
    void timect::mathl(){
        l=clock()-b;
    }
//-------------------------------------------------------
class player{    //the class about the player
public:   
    float v;
    float y;
    int s;
    void init();
    void move();
};
    void player::init(){
        v=0.0;
        y=0.0;
        s=0;
    }
    void player::move(){
        if((y>10.0)||(y<-10.0)){
            v=0.0-v;
            if(y>10.0) y-=0.1;
            if(y<-10.0) y+=0.1;
        }
        y+=v;
    }
//--------------------------------------------------------
class ballt{    //the class about the ball
public:
    float x,y;
    float vx,vy;
    void init();
    void move();
    void change();
    void change_b(float dy,float dx);
};
    void ballt::init(){
        x=0.0;
        y=0.0;
        vx=0.2;
        vy=0.12;
    }
    void ballt::move(){
        x+=vx;
        y+=vy;
    }
    void ballt::change(){
        if((x>10.0)||(x<-10.0)) vx=0.0-vx;
        if((y>10.0)||(y<-10.0)) vy=0.0-vy;
    }
    void ballt::change_b(float dy,float dx){
        if((y<dy+0.5)&&(y>dy-0.5)&&(x<=dx+0.1)&&(x>=dx-0.1)) vx=0.0-vx;
    }
//+++++++++++++++++++++++++++++++++++++++++++++++++
int work=0;
int gameover=1;
float def_v=0.2;
player leftb,rightb;
ballt ball;
timect timec;
int allover=0;
Display *dpy;
Window root,win;
//+++++++++++++++++++++++++++++++++++++++++++++++++
void keyboard(unsigned int);
void init();
void reshape(int, int);
void* idle(void* mes);
void* display(void* mes);
void draw_score(int);
//+++++++++++THE MAIN++++++++++++++++++++++++++++++
int main(){
pthread_t id,id_dis;
XVisualInfo *vi;
Colormap cmap;
XSetWindowAttributes swa;
XWindowAttributes gwa;
XEvent xev;

GLXFBConfig *fc;
GLXWindow glw;
GLXContext glc;
int att[]={
    GLX_RENDER_TYPE, GLX_RGBA_BIT,
    GLX_DOUBLEBUFFER, True,
    GLX_DEPTH_SIZE, 16,
    None
    };
int nelement;
//****************found the window*************************
dpy=XOpenDisplay(NULL);
fc=glXChooseFBConfig(dpy,0,att,&nelement);
vi=glXGetVisualFromFBConfig(dpy,*fc);
root=DefaultRootWindow(dpy);
cmap=XCreateColormap(dpy,root,vi->visual,AllocNone);
swa.colormap=cmap;
swa.event_mask=ExposureMask | KeyPressMask;
win=XCreateWindow(dpy,root,
        100,100,500,500,
        0,vi->depth,
        InputOutput,vi->visual,
        CWColormap | CWEventMask,&swa);
XMapWindow(dpy,win);
XStoreName(dpy,win,"Fuck pong!";
glw=glXCreateWindow(dpy,*fc,win,NULL);
glc=glXCreateNewContext(dpy,*fc,GLX_RGBA_TYPE,NULL,GL_TRUE);
glXMakeContextCurrent(dpy,win,win,glc);
//*********************************************************
timec.init();
ball.init();
leftb.init();
rightb.init();

init();
//****************the 2 thread*****************************
pthread_create(&id,NULL,idle,NULL);//used to manage data
pthread_create(&id_dis,NULL,display,NULL);//used to draw

while(1){
    XNextEvent(dpy,&xev);
    if(xev.type==Expose){//if expose event happen
        XGetWindowAttributes(dpy,win,&gwa);
        reshape(gwa.width,gwa.height);
    }
    else if(xev.type==KeyPress){//if any key is pressed
        if(work==0)while(!work);//avoid modifying data with idle() at the same time
        keyboard(xev.xkey.keycode);
    }
}
//****************to end************************************
allover=1;
pthread_join(id,NULL);
pthread_join(id_dis,NULL);
glXMakeCurrent(dpy,None,NULL);
glXDestroyContext(dpy,glc);
XDestroyWindow(dpy,win);
XCloseDisplay(dpy);
return 0;
}
//+++++++++++END++++++++++++END++++++++++++++END++++++++++++

void init(){
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-10.0,10.0,-10.0,10.0,-1.0,1.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glClearColor(0.0,0.0,0.0,0.0);
}

void* idle(void* mes){//manage data
    while(!allover){if(work==0){
    double l;
    timec.mathl();
    l=(double)(timec.l)/CLOCKS_PER_SEC;
    if ((gameover == 0)&&(l>=0.04)){
        leftb.move();
        rightb.move();
        ball.move();
        ball.change();
        ball.change_b(leftb.y,-7.1);
        ball.change_b(rightb.y,7.1);
        if(ball.x<-10.0){
            rightb.s+=1;
            if(rightb.s>2) gameover=1;
        }
        else if(ball.x>10.0){
            leftb.s+=1;
            if(leftb.s>2) gameover=1;
        }
        if(gameover==1){
            leftb.init();
            rightb.init();
            ball.init();
        }
        timec.init();
    }
    work=1;
    }}
}

void* display(void* mes){//draw
    while(!allover){if(work==1){
    glClearColor(1.0,1.0,1.0,0.0);
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(0.0,0.0,0.0);
    //the line
    glBegin(GL_LINES);
    glVertex2f(0.0,10.0);
    glVertex2f(0.0,-10.0);
    glEnd();
    //the left board
    glRectf(-7.2,leftb.y+0.5,-7.0,leftb.y-0.5);
    //the right board
    glRectf(7.2,rightb.y+0.5,7.0,rightb.y-0.5);
    //the ball
    glRectf(ball.x+0.1, ball.y+0.1, ball.x-0.1, ball.y-0.1);
    //the score
    glLoadIdentity();
    glTranslatef(-2.0,8.0,0.0);
    draw_score(leftb.s);
    glLoadIdentity();
    glTranslatef(1.0,8.0,0.0);
    draw_score(rightb.s);
    glLoadIdentity();
    if(gameover){
        glColor3f(1.0,0.0,0.0);
        glRectf(5.0,5.0,-5.0,-5.0);
    }
    glXSwapBuffers(dpy,win);
    work=0;
    }}
}

void keyboard(unsigned int key){//if the key is pressed
    if     (key==25)/*w*/leftb.v=def_v;
    else if(key==39)/*s*/leftb.v=0.0-def_v;
    else if(key==31)/*i*/rightb.v=def_v;
    else if(key==45)/*k*/rightb.v=0.0-def_v;
    else if(key==33)/*p*/exit(0);
    else if(key==2/*t*/{
        if(gameover==1) gameover=0;
    }
}

void draw_score(int s){//draw the score of the player
    if     (s==0){
        glColor3f(0.0,0.0,0.0);
        glRectf(0.0,0.0,1.0,2.0);
        glColor3f(1.0,1.0,1.0);
        glRectf(0.2,0.2,0.8,1.;
    }
    else if(s==1){
        glColor3f(0.0,0.0,0.0);
        glRectf(0.5,0.0,0.7,2.0);
    }
    else if(s==2){
        glColor3f(0.0,0.0,0.0);
        glRectf(0.0,0.0,1.0,2.0);
        glColor3f(1.0,1.0,1.0);
        glRectf(0.0,1.8,0.8,1.1);
        glRectf(0.2,0.9,1.0,0.2);
    }
}

void reshape(int w,int h){//reshape the window if it has been changed
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-10.0,10.0,-10.0,10.0,-1.0,1.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

论坛徽章:
0
2 [报告]
发表于 2009-05-19 06:09 |只看该作者
发程序要用[CODE][/CODE]括起来,不然显示不完整。

论坛徽章:
0
3 [报告]
发表于 2009-05-19 08:59 |只看该作者
应该改介绍一下
格式也改一下

论坛徽章:
0
4 [报告]
发表于 2009-05-19 16:17 |只看该作者
代码里面有表情………………………………

论坛徽章:
0
5 [报告]
发表于 2009-05-19 16:34 |只看该作者

回复 #4 mgqw 的帖子

看到你的头像再看看你的回复,我真的是很囧啊,你的表情说明问题了

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:53:172015亚冠之水原三星
日期:2015-06-02 16:34:202015年亚冠纪念徽章
日期:2015-10-19 18:13:37程序设计版块每日发帖之星
日期:2015-11-08 06:20:00
6 [报告]
发表于 2009-05-19 16:52 |只看该作者
不错 用[code][/code]包一下
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP