Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <random>
- #include <iostream>
- #include <glut.h>
- #include <windows.h>
- #include <mmsystem.h>
- using std::cout;
- using std::endl;
- struct enemy {
- int color;
- int st_points[2];
- int end_points[2];
- int b_points_0[2];
- int b_points_1[2];
- float t;
- };
- struct powerup {
- int st_points[2];
- int end_points[2];
- int b_points_0[2];
- int b_points_1[2];
- float t;
- };
- bool playing;
- int counter;
- int speX;
- int speY;
- bool gen = false;
- enemy enemies[20];
- int numEnemies;
- powerup powerups[20];
- int numPowerups;
- int p0[2];
- int p3[2];
- int mouthAngle = 50;
- void generateEnemyPowerup() {
- gen = false;
- int x = rand() % 100;
- if (x < 14) {
- powerups[numPowerups].t = 1;
- powerups[numPowerups].st_points[0] = 0;
- powerups[numPowerups].st_points[1] = rand() % 300;
- powerups[numPowerups].end_points[0] = 800;
- powerups[numPowerups].end_points[1] = rand() % 300;
- powerups[numPowerups].b_points_0[0] = rand() % 400;
- powerups[numPowerups].b_points_0[1] = rand() % 300;
- powerups[numPowerups].b_points_1[0] = rand() % 400 + 400;
- powerups[numPowerups].b_points_1[1] = rand() % 300;
- numPowerups++;
- //cout << "\nPowerup! x=";
- }
- else {
- enemies[numEnemies].t = 1;
- enemies[numEnemies].st_points[0] = 0;
- enemies[numEnemies].st_points[1] = rand() % 300;
- enemies[numEnemies].end_points[0] = 800;
- enemies[numEnemies].end_points[1] = rand() % 300;
- enemies[numEnemies].b_points_0[0] = rand() % 400;
- enemies[numEnemies].b_points_0[1] = rand() % 300;
- enemies[numEnemies].b_points_1[0] = rand() % 400 + 400;
- enemies[numEnemies].b_points_1[1] = rand() % 300;
- enemies[numEnemies].color = rand() % 3;
- numEnemies++;
- //cout << "\nEnemy >:( x=";
- }
- glutPostRedisplay();
- }
- int* bezier(float t, int* p0, int* p1, int* p2, int* p3)
- {
- int res[2];
- res[0] = pow((1 - t), 3)*p0[0] + 3 * t*pow((1 - t), 2)*p1[0] + 3 * pow(t, 2)*(1 - t)*p2[0] + pow(t, 3)*p3[0];
- res[1] = pow((1 - t), 3)*p0[1] + 3 * t*pow((1 - t), 2)*p1[1] + 3 * pow(t, 2)*(1 - t)*p2[1] + pow(t, 3)*p3[1];
- return res;
- }
- void removeP(int index) {
- for (int i = index; i < numPowerups - 1; i++) {
- powerups[i] = powerups[i + 1];
- }
- numPowerups--;
- }
- void removeE(int index) {
- for (int i = index; i < numEnemies - 1; i++) {
- enemies[i] = enemies[i + 1];
- }
- numEnemies--;
- }
- void moveX(int x) {
- if (x == 0)return;
- if (x > 0)
- x--;
- else
- x++;
- speX += x;
- glutTimerFunc(30, moveX, x);
- }
- void playMusicIn(int x) {
- switch (x) {
- case 0: PlaySound(TEXT("C:\\Users\\im-is\\Documents\\graphics assignment\\ConsoleApplication1\\pacman_intro.wav"), NULL, SND_ASYNC | SND_FILENAME);; break;
- case 1: PlaySound(TEXT("C:\\Users\\im-is\\Documents\\graphics assignment\\ConsoleApplication1\\pacman_chomp.wav"), NULL, SND_ASYNC | SND_FILENAME);; break;
- }
- }
- void reset() {
- speX = 170;
- speY = 200;
- numPowerups = 0;
- numEnemies = 0;
- counter = 0;
- playing = false;
- playMusicIn(0);
- }
- void renderPowerups() {
- for (int i = 0; i < numPowerups; i++) {
- glPushMatrix();
- glPointSize(20);
- glColor3f(1, 1, 0);
- glBegin(GL_POINTS);
- int* b = bezier(powerups[i].t, powerups[i].st_points, powerups[i].b_points_0, powerups[i].b_points_1, powerups[i].end_points);
- glVertex3d(b[0], b[1], 0);
- glEnd();
- glPopMatrix();
- if ((b[0] <= speX + 30 && b[0] >= speX - 30 && b[1] <= speY + 30 && b[1] >= speY - 30)) {
- counter++;
- if (counter == 3) {
- reset();
- return;
- }
- moveX(10);
- removeP(i);
- }
- if (powerups[i].t <= 0)removeP(i);
- }
- }
- void renderEnemies() {
- for (int i = 0; i < numEnemies; i++) {
- glPushMatrix();
- glPointSize(40);
- glColor3f(1, 0, 0);
- glBegin(GL_POINTS);
- int* b = bezier(enemies[i].t, enemies[i].st_points, enemies[i].b_points_0, enemies[i].b_points_1, enemies[i].end_points);
- glVertex3d(b[0], b[1], 0);
- glEnd();
- glPopMatrix();
- if ((b[0] <= speX + 30 && b[0] >= speX - 30 && b[1] <= speY + 30 && b[1] >= speY - 30)) {
- counter--;
- if (counter == -3) {
- reset();
- return;
- }
- moveX(-10);
- removeE(i);
- }
- if (enemies[i].t <= 0)removeE(i);
- }
- }
- void runPacmanMouth(int x) {
- if (x == 0) {
- mouthAngle -= 1;
- if (mouthAngle <= 0) {
- x = 1;
- mouthAngle = 0;
- }
- }
- else {
- mouthAngle += 1;
- if (mouthAngle >= 50) {
- x = 0;
- mouthAngle = 50;
- }
- }
- //int y = x;
- glutTimerFunc(5, runPacmanMouth, x);
- }
- void Display() {
- glClearColor(0, 0, 0, 0.0f);
- glClear(GL_COLOR_BUFFER_BIT);
- //Pacman
- GLUquadricObj* quadratic;
- quadratic = gluNewQuadric();
- glPushMatrix();
- glColor3f(1, 1, 0);
- glTranslated(speX, speY, 0);
- gluDisk(quadratic, 0, 30, 302, 302);
- glPushMatrix();
- //Pacman's mouth
- glBegin(GL_TRIANGLES);
- glColor3f(0.0f, 0.0f, 0.0f);
- glVertex3f(-7.0f, 0.0f, 0.0f);
- glColor3f(0.0f, 0.0f, 0.0f);
- glVertex3f(60.0f, mouthAngle, 0.0f);
- glColor3f(0.0f, 0.0f, 0.0f);
- glVertex3f(60.0f, 0 - mouthAngle, 0.0f);
- glEnd();
- glPopMatrix();
- glPopMatrix();
- if (gen) {
- generateEnemyPowerup();
- }
- renderPowerups();
- renderEnemies();
- glFlush();
- }
- void time(int val) {
- gen = true;
- glutPostRedisplay();
- glutTimerFunc(1000, time, 0);
- }
- void moveY(int x) {
- if (x == 0)return;
- if (x > 0)
- x--;
- else
- x++;
- speY += x;
- if (speY <= 34) {
- speY = 34;
- return;
- }
- if (speY >= 215) {
- speY = 215;
- return;
- }
- glutTimerFunc(30, moveY, x);
- }
- void spe(int k, int x, int y)
- {
- if (k == GLUT_KEY_UP)
- moveY(10);
- if (k == GLUT_KEY_DOWN)
- moveY(-10);
- glutPostRedisplay();
- }
- void blerp(int val) {
- for (int i = 0; i < numPowerups; i++) {
- powerups[i].t -= 0.002;
- }
- for (int i = 0; i < numEnemies; i++) {
- enemies[i].t -= 0.003;
- }
- glutPostRedisplay();
- glutTimerFunc(10, blerp, 0);
- }
- void main(int argc, char** argr) {
- glutInit(&argc, argr);
- glutInitWindowSize(850, 400);
- glutInitWindowPosition(150, 150);
- glutCreateWindow("Control");
- glutDisplayFunc(Display);
- glutSpecialFunc(spe);
- glPointSize(25);
- glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
- glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluOrtho2D(0.0, 500, 0.0, 250);
- glutTimerFunc(0, time, 0);
- glutTimerFunc(0, blerp, 0);
- reset();
- runPacmanMouth(0);
- glutMainLoop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement