-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnake.cpp
97 lines (87 loc) · 2.33 KB
/
Snake.cpp
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
#include "Snake.h"
#include "Food.h"
#include "Obstacles.h"
#include "Pictures.h"
#include "Sound.h"
#include <iostream>
#include <SDL.h>
#include <SDL_image.h>
#include <sstream>
#include <SDL_ttf.h>
void Snake::move()
{
position_head.x += velocity.stepX;
position_head.y += velocity.stepY;
}
void Snake::TurnLeft()
{
if (velocity.stepX != 1) {
velocity.stepX = -1;
velocity.stepY = 0;
}
}
void Snake::TurnRight()
{
if (velocity.stepX != -1) {
velocity.stepX = 1;
velocity.stepY = 0;
}
}
void Snake::TurnUp()
{
if (velocity.stepY != 1) {
velocity.stepY = -1;
velocity.stepX = 0;
}
}
void Snake::TurnDown()
{
if (velocity.stepY != -1) {
velocity.stepY = 1;
velocity.stepX = 0;
}
}
void Snake::drawHead(SDL_Renderer* renderer)
{
SDL_Rect sourceRect;
SDL_Rect desRect;
SDL_Texture* loadHead = NULL;
loadHead = IMG_LoadTexture(renderer,"yellow.png");
SDL_QueryTexture(loadHead, NULL, NULL, &sourceRect.w, &sourceRect.h);
sourceRect.x = 0;
sourceRect.y = 0;
sourceRect.w = snakeWidth;
sourceRect.h = snakeHeight;
desRect.x = snakeXY * position_head.x;
desRect.y = snakeXY * position_head.y;
desRect.w = sourceRect.w;
desRect.h = sourceRect.h;
SDL_RenderCopy(renderer, loadHead, NULL, &desRect);
SDL_DestroyTexture(loadHead);
}
void Snake::drawTails(SDL_Renderer *renderer)
{
SDL_Texture* loadTail = NULL;
for (int i = 0; i < tail_length; i++) {
Pos& tail_position = tail[(tail_start + i) % tail_max]; //tail_start + i
SDL_Rect sourceRect;
SDL_Rect desRect;
loadTail = IMG_LoadTexture(renderer,"red.png");
SDL_QueryTexture(loadTail, NULL, NULL, &sourceRect.w, &sourceRect.h);
sourceRect.x = 0;
sourceRect.y = 0;
sourceRect.w = snakeWidth; //12
sourceRect.h = snakeHeight; //12
desRect.x = snakeXY * tail_position.x; //tail_position
desRect.y = snakeXY * tail_position.y;
desRect.w = sourceRect.w;
desRect.h = sourceRect.h;
SDL_RenderCopy(renderer, loadTail, NULL, &desRect);
SDL_DestroyTexture(loadTail);
}
}
void Snake::draw(SDL_Renderer* renderer, Snake &snake)
{
snake.drawHead(renderer);
snake.drawTails(renderer);
}