-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnake.cpp
More file actions
143 lines (117 loc) · 2.43 KB
/
Copy pathSnake.cpp
File metadata and controls
143 lines (117 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <SFML/Graphics.hpp>
#include <vector>
#include <iostream>
#include <time.h>
#define LEFT 1
#define RIGHT 2
#define UP 3
#define DOWN 4
#define HEIGHT 30
#define WIDTH 20
#define SIZE 16
int LENGTH = 5;
int dir = DOWN;
int score = 0;
struct Snake
{
int x;
int y;
} snake[100];
struct Fruit
{
int x;
int y;
}fruit;
void Tick()
{
for (int i = LENGTH; i > 0; i--)
{
snake[i].x = snake[i - 1].x;
snake[i].y = snake[i - 1].y;
}
if (dir == LEFT) snake[0].x -= 1;
if (dir == RIGHT) snake[0].x += 1;
if (dir == UP) snake[0].y -= 1;
if (dir == DOWN) snake[0].y += 1;
if (snake[0].x == fruit.x && snake[0].y == fruit.y)
{
score++;
std::cout << score << std::endl;
fruit.x = rand() % WIDTH;
fruit.y = rand() % HEIGHT;
LENGTH++;
}
if (snake[0].x > WIDTH) snake[0].x = 0;
if (snake[0].x < 0) snake[0].x = WIDTH;
if (snake[0].y > HEIGHT) snake[0].y = 0;
if (snake[0].y < 0) snake[0].y = HEIGHT;
for (int i = 1; i < LENGTH; i++)
{
if (snake[0].x == snake[i].x && snake[0].y == snake[i].y)
{
LENGTH /= 2;
}
}
}
int main()
{
srand(time(0));
sf::RenderWindow window(sf::VideoMode(WIDTH * SIZE, HEIGHT * SIZE), "Snake");
sf::RectangleShape fruitImage(sf::Vector2f(SIZE, SIZE));
sf::Texture snakeTexture;
snakeTexture.loadFromFile("images/snake.jpg");
sf::Sprite snakeImage(snakeTexture);
snakeImage.setTextureRect(sf::IntRect(0, 0, SIZE, SIZE));
sf::Texture grid;
grid.loadFromFile("images/background.png");
sf::Sprite background(grid);
fruitImage.setFillColor(sf::Color::Red);
fruit.x = 10;
fruit.y = 10;
sf::Clock clock;
float timer = 0;
float delay = 0.1;
while (window.isOpen())
{
float time = clock.getElapsedTime().asSeconds();
timer += time;
clock.restart();
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
switch (event.key.code)
{
case sf::Keyboard::Left:
dir = LEFT;
break;
case sf::Keyboard::Right:
dir = RIGHT;
break;
case sf::Keyboard::Up:
dir = UP;
break;
case sf::Keyboard::Down:
dir = DOWN;
break;
}
}
if (timer > delay)
{
timer = 0;
Tick();
}
window.clear();
window.draw(background);
for (int i = 0; i < LENGTH; i++)
{
snakeImage.setPosition(snake[i].x * SIZE, snake[i].y * SIZE);
window.draw(snakeImage);
}
fruitImage.setPosition(fruit.x * SIZE, fruit.y * SIZE);
window.draw(fruitImage);
window.display();
}
return 0;
}