Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <GLUT/glut.h>
- float deltaTime = 0.0f;
- float playerX = 20.0f;
- float playerY = 20.0f;
- float enemyX = 500.0f;
- float enemyY = 10.0f;
- float prevtime = 0.0f;
- int lastFrameTime = 0;
- void makeQuad(float,float,float,float);
- //int wkey, akey, skey, dkey = 0;
- int keyDown[256];
- int playerspeed = 500;
- float gravity = 0.0f;
- /*
- Compile using:
- gcc -framework GLUT -framework OpenGL main.c -o game
- Then run using:
- ./game
- Controls:
- WASD
- Goal:
- Evade the small cube. If you hit the cube, your X will be reset to 20. Take that player!
- */
- void keyPressed(unsigned char key, int x, int y) {
- keyDown[key]=1;
- }
- void keyUp(unsigned char key, int x, int y){
- keyDown[key]=0;
- }
- void display(void){
- if(!lastFrameTime){
- lastFrameTime = glutGet(GLUT_ELAPSED_TIME);
- }
- int now = glutGet(GLUT_ELAPSED_TIME);
- int elapsedMilliseconds = now - lastFrameTime;
- float deltaTime = elapsedMilliseconds / 1000.0f;
- lastFrameTime = now;
- //printf("\ndeltaTime is: %d", deltaTime);
- int windowWidth = glutGet(GLUT_WINDOW_WIDTH);
- //playerX+=1000.0F*deltaTime;
- //--------------PLAYER CONTROLS---------
- if(keyDown[100]){
- playerX+=playerspeed*deltaTime;
- }
- if(keyDown[97]){
- playerX-=playerspeed*deltaTime;
- }
- if(keyDown[119]&&playerY<21){
- playerY+=100;
- }
- if(playerX>windowWidth){
- playerX-=windowWidth;
- //printf("true");
- }
- if(playerX<0){
- playerX+=windowWidth;
- }
- if(playerY>19){
- gravity-=1*deltaTime;
- playerY+=gravity;
- }
- else{
- gravity=0;
- }
- if(playerX<enemyX+10&&playerX>enemyX-10&&playerY<enemyY+10&&playerY>enemyY-10){
- playerX = 20;
- }
- //---------------ENEMY CONTROLS---------------
- enemyX-=100*deltaTime;
- if(enemyX<0){
- enemyX+=windowWidth;
- }
- //--------------DRAW THINGS-------------------
- //printf("\nwindowWidth is: %d", windowWidth);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glPushMatrix();
- glTranslatef(playerX, playerY,0.0f);
- makeQuad(0,0,20,40);
- glPopMatrix();
- glPushMatrix();
- glTranslatef(enemyX, enemyY,0.0f);
- makeQuad(0,0,20,20);
- glPopMatrix();
- glutSwapBuffers();
- }
- void reshape(int width, int height){
- glViewport(0,0,width,height);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluOrtho2D(0, width, 0, height);
- glMatrixMode(GL_MODELVIEW);
- }
- void idle(void){
- glutPostRedisplay();
- }
- void makeQuad(float x, float y, float width, float height){
- glBegin(GL_QUADS);
- glVertex2f(x-(width/2), y-(height/2));
- glVertex2f(x+(width/2), y-(height/2));
- glVertex2f(x+(width/2), y+(height/2));
- glVertex2f(x-(width/2), y+(height/2));
- glEnd();
- }
- int main(int argc, char** argv){
- glutInit(&argc, argv);
- //glutKeyboardFunc(keyPressed);
- glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
- glutInitWindowSize(800, 600);
- glutCreateWindow("Wolfos' epic game");
- glutDisplayFunc(display);
- glutReshapeFunc(reshape);
- glutIdleFunc(idle);
- glutKeyboardFunc(keyPressed);
- glutKeyboardUpFunc(keyUp);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment