Snake Game
Snake Game
Snake Game
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;
// 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
// Initialize OpenGL
init();
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. 😊