Snake Game

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

#include <GL/glut.

h>
#include <stdlib.h>
#include <time.h>

// Snake properties
int snake_size = 3;
int snake_speed = 100; // milliseconds
int direction = 1; // 1: up, 2: right, 3: down, 4: left
int score = 0;

// Snake position
int snake_x[100] = {20, 20, 20}; // initial x positions
int snake_y[100] = {20, 19, 18}; // initial y positions

// Food position
int food_x, food_y;

// Function to generate random food position


void generateFood() {
srand(time(NULL));
food_x = rand() % 40; // assuming a 40x40 grid
food_y = rand() % 40;
}

// Function to initialize OpenGL


void init() {
glClearColor(0.0, 0.0, 0.0, 1.0); // set clear color to black
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 40.0, 0.0, 40.0, -1.0, 1.0); // set up 2D orthographic viewing
region
generateFood(); // generate initial food position
}

// Function to handle keyboard input


void keyboard(int key, int x, int y) {
switch (key) {
case GLUT_KEY_UP:
if (direction != 3) // prevent going in opposite direction
direction = 1; // set direction to up
break;
case GLUT_KEY_RIGHT:
if (direction != 4)
direction = 2; // set direction to right
break;
case GLUT_KEY_DOWN:
if (direction != 1)
direction = 3; // set direction to down
break;
case GLUT_KEY_LEFT:
if (direction != 2)
direction = 4; // set direction to left
break;
}
}
// Function to update snake position and check for collisions
void update(int value) {
// Update snake position based on direction
switch (direction) {
case 1: // up
for (int i = snake_size - 1; i > 0; i--) {
snake_x[i] = snake_x[i - 1];
snake_y[i] = snake_y[i - 1] + 1;
}
snake_y[0]++;
break;
case 2: // right
for (int i = snake_size - 1; i > 0; i--) {
snake_y[i] = snake_y[i - 1];
snake_x[i] = snake_x[i - 1] + 1;
}
snake_x[0]++;
break;
case 3: // down
for (int i = snake_size - 1; i > 0; i--) {
snake_x[i] = snake_x[i - 1];
snake_y[i] = snake_y[i - 1] - 1;
}
snake_y[0]--;
break;
case 4: // left
for (int i = snake_size - 1; i > 0; i--) {
snake_y[i] = snake_y[i - 1];
snake_x[i] = snake_x[i - 1] - 1;
}
snake_x[0]--;
break;
}

// Check for collisions with food


if (snake_x[0] == food_x && snake_y[0] == food_y) {
score++; // increment score
snake_size++; // increase snake size
generateFood(); // generate new food position
}

// Check for collisions with walls


if (snake_x[0] < 0 || snake_x[0] >= 40 || snake_y[0] < 0 || snake_y[0] >= 40) {
// Game over
exit(0);
}

// Check for collisions with snake body


for (int i = 1; i < snake_size; i++) {
if (snake_x[0] == snake_x[i] && snake_y[0] == snake_y[i]) {
// Game over
exit(0);
}
}

glutPostRedisplay(); // tell OpenGL to redraw the scene


glutTimerFunc(snake_speed, update, 0); // call update function after specified
time interval
}
// Function to draw the snake and food
void draw() {
glClear(GL_COLOR_BUFFER_BIT); // clear the color buffer

// Draw snake
glColor3f(1.0, 1.0, 1.0); // set snake color to white
for (int i = 0; i < snake_size; i++) {
glRectd(snake_x[i], snake_y[i], snake_x[i] + 1, snake_y[i] + 1); // draw
snake segment as a rectangle
}

// Draw food
glColor3f(1.0, 0.0, 0.0); // set food color to red
glRectd(food_x, food_y, food_x + 1, food_y + 1); // draw food as a rectangle

// Draw score counter


glColor3f(1.0, 1.0,

ChatGPTBot | Midjourney, [6/30/2023 9:13 PM]

1.0); // set text color to white


glRasterPos2f(0.5, 0.5); // set text position
char score_text[20];
sprintf(score_text, "Score: %d", score); // format score as text
for (int i = 0; score_text[i] != '0'; i++) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, score_text[i]); // draw score
text
}

glFlush(); // flush the OpenGL pipeline


}

int main(int argc, char** argv) {


// Initialize GLUT and create window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400, 400);
glutInitWindowPosition(100, 100);
glutCreateWindow("Snake Game");

// Register callback functions


glutDisplayFunc(draw);
glutSpecialFunc(keyboard);
glutTimerFunc(0, update, 0);

// Initialize OpenGL
init();

// Enter the main loop


glutMainLoop();

return 0;
}

This code uses the GLUT library to create a window and handle keyboard input. It
uses OpenGL to draw the snake, food, and score counter on the screen. The snake's
movement, collision detection, and score counting are implemented in the update()
function.

Please note that this code is a basic example and may not include advanced features
like levels, power-ups, or high scores. You can modify and enhance the code
according to your requirements.

I hope this helps! Let me know if you have any further questions. 😊

You might also like