Skip to content

Commit 2045e31

Browse files
committed
Initial commit: basic version of the game
0 parents  commit 2045e31

37 files changed

+1104
-0
lines changed

CMakeLists.txt

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
cmake_minimum_required(VERSION 3.12)
2+
project(Snake)
3+
4+
include(FindSDL2_ttf.cmake)
5+
6+
set(CMAKE_CXX_STANDARD 11)
7+
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/Cmake")
8+
9+
set(SDL2_INCLUDE_DIR /Library/Frameworks/SDL2.framework/)
10+
set(SDL2_LIBRARY:FILEPATH=/Library/Frameworks/SDL2.framework)
11+
set(SDL2_LIBRARY:FILEPATH=/Library/Frameworks/SDL2_ttf.framework/)
12+
13+
set(SDL_TTF_INCLUDE_DIRS /Library/Frameworks/SDL2_ttf.framework/)
14+
set(SDL_TTF_LIBRARIES /Library/Frameworks/SDL2_ttf.framework)
15+
16+
include_directories(.)
17+
include_directories (${SDL2_INCLUDE_DIR})
18+
include_directories (${SDL2_TTF_INCLUDE_DIR})
19+
20+
find_library(SDL2_LIBRARY SDL2)
21+
find_library(SDL_TTF_LIBRARIES SDL2_ttf)
22+
23+
add_executable(${PROJECT_NAME}
24+
SnakeGame.cpp
25+
SnakeGame.h
26+
main.cpp
27+
SnakeBoard.cpp
28+
SnakeBoard.h
29+
Snake.cpp Snake.h)
30+
31+
message(SDL_LIBRARY: ${SDL2_LIBRARY})
32+
message(SDL_TTF_LIBRARIES: ${SDL2_TTF_LIBRARIES})
33+
message(SDL_TTF_found: ${SDL_TTF_FOUND})
34+
35+
target_link_libraries(Snake ${SDL2_LIBRARY})
36+
target_link_libraries(Snake ${SDL_TTF_LIBRARIES})
37+
38+
file(COPY resources DESTINATION ${CMAKE_BINARY_DIR})

FindSDL2_ttf.cmake

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Locate SDL_image library
2+
#
3+
# This module defines:
4+
#
5+
# ::
6+
#
7+
# SDL_TTF_LIBRARIES, the name of the library to link against
8+
# SDL_TTF_INCLUDE_DIRS, where to find the headers
9+
# SDL_TTF_FOUND, if false, do not try to link against
10+
# SDL_F_VERSION_STRING - human-readable string containing the version of SDL_ttf
11+
#
12+
#
13+
#
14+
# For backward compatiblity the following variables are also set:
15+
#
16+
# ::
17+
#
18+
# SDLTTF_LIBRARY (same value as SDL_TTF_LIBRARIES)
19+
# SDLTTF_INCLUDE_DIR (same value as SDL_TTF_INCLUDE_DIRS)
20+
# SDLTTF_FOUND (same value as SDL_TTF_FOUND)
21+
#
22+
#
23+
#
24+
# $SDLDIR is an environment variable that would correspond to the
25+
# ./configure --prefix=$SDLDIR used in building SDL.
26+
#
27+
# Created by Eric Wing. This was influenced by the FindSDL.cmake
28+
# module, but with modifications to recognize OS X frameworks and
29+
# additional Unix paths (FreeBSD, etc).
30+
31+
#=============================================================================
32+
# Copyright 2005-2009 Kitware, Inc.
33+
# Copyright 2012 Benjamin Eikel
34+
#
35+
# Distributed under the OSI-approved BSD License (the "License");
36+
# see accompanying file Copyright.txt for details.
37+
#
38+
# This software is distributed WITHOUT ANY WARRANTY; without even the
39+
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
40+
# See the License for more information.
41+
#=============================================================================
42+
# (To distribute this file outside of CMake, substitute the full
43+
# License text for the above reference.)
44+
45+
find_path(SDL2_TTF_INCLUDE_DIR SDL_ttf.h
46+
HINTS
47+
ENV SDL2TTFDIR
48+
ENV SDL2DIR
49+
PATH_SUFFIXES SDL2
50+
# path suffixes to search inside ENV{SDLDIR}
51+
include/SDL2 include
52+
)
53+
54+
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
55+
set(VC_LIB_PATH_SUFFIX lib/x64)
56+
else()
57+
set(VC_LIB_PATH_SUFFIX lib/x86)
58+
endif()
59+
60+
find_library(SDL2_TTF_LIBRARY
61+
NAMES SDL2_ttf
62+
HINTS
63+
ENV SDL2TTFDIR
64+
ENV SDL2DIR
65+
PATH_SUFFIXES lib ${VC_LIB_PATH_SUFFIX}
66+
)
67+
68+
if(SDL2_TTF_INCLUDE_DIR AND EXISTS "${SDL2_TTF_INCLUDE_DIR}/SDL_ttf.h")
69+
file(STRINGS "${SDL2_TTF_INCLUDE_DIR}/SDL_ttf.h" SDL_TTF_VERSION_MAJOR_LINE REGEX "^#define[ \t]+SDL_TTF_MAJOR_VERSION[ \t]+[0-9]+$")
70+
file(STRINGS "${SDL2_TTF_INCLUDE_DIR}/SDL_ttf.h" SDL2_TTF_VERSION_MINOR_LINE REGEX "^#define[ \t]+SDL2_TTF_MINOR_VERSION[ \t]+[0-9]+$")
71+
file(STRINGS "${SDL2_TTF_INCLUDE_DIR}/SDL_ttf.h" SDL2_TTF_VERSION_PATCH_LINE REGEX "^#define[ \t]+SDL2_TTF_PATCHLEVEL[ \t]+[0-9]+$")
72+
string(REGEX REPLACE "^#define[ \t]+SDL2_TTF_MAJOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_TTF_VERSION_MAJOR "${SDL2_TTF_VERSION_MAJOR_LINE}")
73+
string(REGEX REPLACE "^#define[ \t]+SDL2_TTF_MINOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_TTF_VERSION_MINOR "${SDL2_TTF_VERSION_MINOR_LINE}")
74+
string(REGEX REPLACE "^#define[ \t]+SDL2_TTF_PATCHLEVEL[ \t]+([0-9]+)$" "\\1" SDL2_TTF_VERSION_PATCH "${SDL2_TTF_VERSION_PATCH_LINE}")
75+
set(SDL2_TTF_VERSION_STRING ${SDL2_TTF_VERSION_MAJOR}.${SDL2_TTF_VERSION_MINOR}.${SDL2_TTF_VERSION_PATCH})
76+
unset(SDL2_TTF_VERSION_MAJOR_LINE)
77+
unset(SDL2_TTF_VERSION_MINOR_LINE)
78+
unset(SDL2_TTF_VERSION_PATCH_LINE)
79+
unset(SDL2_TTF_VERSION_MAJOR)
80+
unset(SDL2_TTF_VERSION_MINOR)
81+
unset(SDL2_TTF_VERSION_PATCH)
82+
endif()
83+
84+
set(SDL2_TTF_LIBRARIES ${SDL2_TTF_LIBRARY})
85+
set(SDL2_TTF_INCLUDE_DIRS ${SDL2_TTF_INCLUDE_DIR})
86+
87+
include(FindPackageHandleStandardArgs)
88+
89+
FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2_ttf
90+
REQUIRED_VARS SDL2_TTF_LIBRARIES SDL2_TTF_INCLUDE_DIRS
91+
VERSION_VAR SDL2_TTF_VERSION_STRING)
92+
93+
# for backward compatiblity
94+
#set(SDLTTF_LIBRARY ${SDL_TTF_LIBRARIES})
95+
#set(SDLTTF_INCLUDE_DIR ${SDL_TTF_INCLUDE_DIRS})
96+
#set(SDLTTF_FOUND ${SDL_TTF_FOUND})
97+
98+
mark_as_advanced(SDL2_TTF_LIBRARY SDL2_TTF_INCLUDE_DIR)

Snake.cpp

+238
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
#include <iostream>
2+
#include "Snake.h"
3+
#include "SnakeBoard.h"
4+
5+
SDL_Color Snake::SNAKE_COLOR = {0x00, 0xFF, 0x00, SDL_ALPHA_OPAQUE}; // green
6+
7+
Snake::Snake(int row, int col) : Snake(UP, row, col) {}
8+
9+
Snake::Snake(Snake::Direction d, int row, int col) :
10+
head_(new Block(row, col)),
11+
tail_(head_),
12+
toGrow_(0),
13+
direction_(d)
14+
{}
15+
16+
Snake::~Snake()
17+
{
18+
Block *block = tail_;
19+
20+
while (block != nullptr)
21+
{
22+
Block *prev = block->getPrev();
23+
delete block;
24+
block = prev;
25+
}
26+
}
27+
28+
bool Snake::collidesWith(std::pair<int, int> coords) const
29+
{
30+
Block *block = head_;
31+
32+
while (block != nullptr)
33+
{
34+
if (block->collidesWith(coords))
35+
return true;
36+
block = block->getNext_();
37+
}
38+
39+
return false;
40+
}
41+
42+
void Snake::draw(SDL_Renderer *renderer) const
43+
{
44+
Block *block = head_;
45+
46+
while (block != nullptr)
47+
{
48+
SnakeBoard::drawBlock(renderer, Snake::SNAKE_COLOR, block->getRow(), block->getColumn());
49+
block = block->getNext_();
50+
}
51+
}
52+
53+
int Snake::getHeadRow() const
54+
{
55+
return head_->getRow();
56+
}
57+
58+
int Snake::getHeadColumn() const
59+
{
60+
return head_->getColumn();
61+
}
62+
63+
void Snake::grow()
64+
{
65+
grow(1);
66+
}
67+
68+
void Snake::grow(int n)
69+
{
70+
toGrow_ += n;
71+
}
72+
73+
bool Snake::headCollidesWith(std::pair<int, int> coords) const
74+
{
75+
return head_->collidesWith(coords);
76+
}
77+
78+
bool Snake::headCollidesWithSelf() const
79+
{
80+
Block *b = tail_;
81+
while (b != head_)
82+
{
83+
if (b->collidesWith(*head_))
84+
return true;
85+
86+
b = b->getPrev();
87+
}
88+
return false;
89+
}
90+
91+
void Snake::move()
92+
{
93+
newHead();
94+
if (toGrow_) // if we need to grow the Snake, we do so by not deleting the tail
95+
{
96+
toGrow_--;
97+
}
98+
else
99+
{
100+
deleteTail();
101+
}
102+
}
103+
104+
bool Snake::setDirectionIfPossible(Snake::Direction d)
105+
{
106+
if ((direction_ == UP && d == DOWN) || (direction_ == DOWN && d == UP)
107+
|| (direction_ == LEFT && d == RIGHT) || (direction_ == RIGHT && d == LEFT))
108+
{
109+
return false;
110+
}
111+
else
112+
{
113+
direction_ = d;
114+
return true;
115+
}
116+
}
117+
118+
int Snake::size() const
119+
{
120+
return head_->size();
121+
}
122+
123+
void Snake::deleteTail()
124+
{
125+
Block *oldTail = tail_;
126+
tail_ = tail_->getPrev();
127+
delete oldTail;
128+
}
129+
130+
std::pair<int, int> Snake::getNewRowAndColumn()
131+
{
132+
int r = head_->getRow();
133+
int c = head_->getColumn();
134+
135+
switch (direction_)
136+
{
137+
case UP:
138+
r -= 1;
139+
break;
140+
case DOWN:
141+
r += 1;
142+
break;
143+
case LEFT:
144+
c -= 1;
145+
break;
146+
case RIGHT:
147+
c += 1;
148+
break;
149+
}
150+
151+
return std::make_pair(r, c);
152+
}
153+
154+
void Snake::newHead()
155+
{
156+
std::pair<int, int> coords = getNewRowAndColumn();
157+
auto *newHead = new Block(coords.first, coords.second, head_);
158+
head_->setPrev(newHead);
159+
head_ = newHead;
160+
}
161+
162+
Snake::Block::Block(int row, int col) : Block(row, col, nullptr) {}
163+
164+
Snake::Block::Block(int row, int col, Block *next):
165+
row_(row),
166+
col_(col),
167+
prev_(nullptr),
168+
next_(next)
169+
{}
170+
171+
Snake::Block::~Block()
172+
{
173+
if (prev_ != nullptr)
174+
{
175+
prev_->next_ = nullptr;
176+
}
177+
if (next_ != nullptr)
178+
{
179+
next_->prev_ = nullptr;
180+
}
181+
}
182+
183+
// Accessors
184+
int Snake::Block::getRow() const
185+
{
186+
return row_;
187+
}
188+
189+
int Snake::Block::getColumn() const
190+
{
191+
return col_;
192+
}
193+
194+
Snake::Block* Snake::Block::getPrev() const
195+
{
196+
return prev_;
197+
}
198+
199+
/**
200+
* Get the Block ahead of this one (i.e. the one immediately closer to the tail).
201+
*
202+
* @return a pointer to the Block which comes after this one.
203+
*/
204+
Snake::Block* Snake::Block::getNext_() const
205+
{
206+
return next_;
207+
}
208+
209+
210+
void Snake::Block::setPrev(Snake::Block *const pBlock)
211+
{
212+
prev_ = pBlock;
213+
}
214+
215+
void Snake::Block::setNext(Snake::Block *const pBlock)
216+
{
217+
next_ = pBlock;
218+
}
219+
220+
bool Snake::Block::collidesWith(std::pair<int, int> coords) const
221+
{
222+
return (row_ == coords.first && col_ == coords.second);
223+
}
224+
225+
bool Snake::Block::collidesWith(const Block& otherBlock) const
226+
{
227+
return (row_ == otherBlock.row_ && col_ == otherBlock.col_);
228+
}
229+
230+
int Snake::Block::size() const
231+
{
232+
if (next_ == nullptr)
233+
return 1;
234+
else
235+
return 1 + next_->size();
236+
}
237+
238+

0 commit comments

Comments
 (0)