Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
snake-game
==========

A snake game based on Qt.
A snake game based on Qt.
The game is created with c++ and c language.
With the graphics it looks good.
6 changes: 3 additions & 3 deletions gamecontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ void GameController::snakeAteFood(Food *food)
addNewFood();
}

//void GameController::snakeHitWall(Snake *snake, Wall *wall)
//{
//}




void GameController::snakeAteItself()
{
Expand Down
21 changes: 13 additions & 8 deletions snake.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
//Snake game with c++

//Adding important libraries
#include <QPainter>

#include "constants.h"
#include "gamecontroller.h"
#include "snake.h"

static const qreal SNAKE_SIZE = TILE_SIZE;

static const qreal SNAKE_SIZE = TILE_SIZE; //snake size
//setup controller of game
Snake::Snake(GameController &controller) :
head(0, 0),
growing(7),
Expand Down Expand Up @@ -39,7 +42,7 @@ QRectF Snake::boundingRect() const
);
return bound;
}

//head to tail
QPainterPath Snake::shape() const
{
QPainterPath path;
Expand All @@ -54,14 +57,14 @@ QPainterPath Snake::shape() const

return path;
}

//adding graphics
void Snake::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
painter->save();
painter->fillPath(shape(), Qt::yellow);
painter->restore();
}

//setting bottons for changing direction of snake .
void Snake::setMoveDirection(Direction direction)
{
if (moveDirection == MoveLeft && direction == MoveRight)
Expand All @@ -79,7 +82,7 @@ Snake::Direction Snake::currentDirection()
{
return moveDirection;
}

//add counter and speed of the snake
void Snake::advance(int step)
{
if (!step) {
Expand Down Expand Up @@ -120,6 +123,7 @@ void Snake::advance(int step)
handleCollisions();
}

//increasing size of snake
void Snake::moveLeft()
{
head.rx() -= SNAKE_SIZE;
Expand Down Expand Up @@ -156,7 +160,7 @@ void Snake::handleCollisions()
{
QList<QGraphicsItem *> collisions = collidingItems();

// Check collisions with other objects on screen
// Check collisions with other objects on screen
foreach (QGraphicsItem *collidingItem, collisions) {
if (collidingItem->data(GD_Type) == GO_Food) {
// Let GameController handle the event by putting another apple
Expand All @@ -165,8 +169,9 @@ void Snake::handleCollisions()
}
}

// Check snake eating itself
// Check snake eating itself
if (tail.contains(head)) {
controller.snakeAteItself();
}
}
//now lets time to test it.