Skip to content

Commit

Permalink
#111: Mostly finished snake logic
Browse files Browse the repository at this point in the history
  • Loading branch information
sumershinde22 committed Nov 6, 2024
1 parent 1f862ea commit 4be6a17
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 16 deletions.
175 changes: 159 additions & 16 deletions NERODevelopment/content/Snake.qml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import QtQuick 2.15
import QtQuick.Controls 2.15

Rectangle {
id: snake
id: snakeGame

anchors.fill: parent
focus: snake.isFocused
focus: snakeGame.isFocused
visible: true

property bool isFocused: false
Expand All @@ -14,33 +14,176 @@ Rectangle {
height: 480
color: "black"

property int minX: 200
property int maxX: 600
property int minY: 40
property int maxY: 440
property int tileSize: 20
property int gridWidth: Math.floor(width / tileSize)
property int gridHeight: Math.floor(height / tileSize)

property var snakeBody: [{"x": 300, "y": 300}, {"x": 280, "y": 300}, {"x": 260, "y": 300}]
property int segmentSize: 20
property string direction: "right"
property var snakeBody: []

QtObject {
id: food
property int x: 0
property int y: 0
}

property int direction: 1 // 0: Up, 1: Right, 2: Down, 3: Left
property bool gameOver: false
property int score: 0

Rectangle {
width: 400
height: 400
Component.onCompleted: startGame()

function startGame() {
snakeBody = [{ x: Math.floor(gridWidth / 2), y: Math.floor(gridHeight / 2) }]
direction = 1
gameOver = false
score = 0
food.x = 0
food.y = 0
placeFood()
gameTimer.start()
updateSnakeModel()
}

function placeFood() {
var x, y
do {
x = Math.floor(Math.random() * gridWidth)
y = Math.floor(Math.random() * gridHeight)
} while (isSnakePosition(x, y))
food.x = x
food.y = y
}

function isSnakePosition(x, y) {
for (let i = 0; i < snakeBody.length; i++) {
if (snakeBody[i].x === x && snakeBody[i].y === y) {
return true
}
}
return false
}

function checkCollision(x, y) {
if (x < 0 || x >= gridWidth || y < 0 || y >= gridHeight) {
return true
}
for (let i = 1; i < snakeBody.length; i++) {
if (snakeBody[i].x === x && snakeBody[i].y === y) {
return true
}
}
return false
}

function updateGame() {
if (gameOver) {
gameTimer.stop()
snakeController.saveScore(score)
return
}

let head = { x: snakeBody[0].x, y: snakeBody[0].y }

switch (direction) {
case 0: // Up
head.y -= 1
break
case 1: // Right
head.x += 1
break
case 2: // Down
head.y += 1
break
case 3: // Left
head.x -= 1
break
}

if (checkCollision(head.x, head.y)) {
gameOver = true
return
}

snakeBody.unshift(head)

if (head.x === food.x && head.y === food.y) {
score += 1
placeFood()
} else {
snakeBody.pop()
}

updateSnakeModel()
}

function updateSnakeModel() {
snakeModel.clear()
for (let segment of snakeBody) {
snakeModel.append({ "x": segment.x, "y": segment.y })
}
}

Timer {
id: gameTimer
interval: 100
repeat: true
running: false
onTriggered: updateGame()
}

Keys.onPressed: {
console.log("Key pressed:", event.key)
snakeController.handleKeyPress(event.key)
if (event.key === Qt.Key_Return && gameOver) {
startGame()
}
}

ListModel {
id: snakeModel
}

Repeater {
model: snakeModel
delegate: SnakeBody {
x: model.x * tileSize
y: model.y * tileSize
dimension: tileSize
}
}

SnakeFood {
x: food.x * tileSize
y: food.y * tileSize
dimension: tileSize
}

Text {
id: gameOverText
anchors.centerIn: parent
border.color: "white"
border.width: 2
color: "transparent"
text: gameOver ? "Game Over\nPress Enter to Restart" : ""
color: "white"
font.pixelSize: 24
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
visible: gameOver
}

Text {
id: scoreText
text: "Score: " + snake.score
text: "Score: " + score
font.pixelSize: 24
color: "white"
anchors.top: parent.top
anchors.horizontalCenter: parent.horizontalCenter
anchors.topMargin: 10
}

Connections {
target: snakeController
onDirectionChanged: {
direction = newDirection
console.log("Direction updated to:", direction)
}
}
}
3 changes: 3 additions & 0 deletions NERODevelopment/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "controllers/keyboardcontroller.h"
#include "controllers/navigationcontroller.h"
#include "controllers/offviewcontroller.h"
#include "controllers/snakecontroller.h"
#include "controllers/speedcontroller.h"
#include "import_qml_components_plugins.h"
#include "import_qml_plugins.h"
Expand Down Expand Up @@ -50,6 +51,7 @@ int main(int argc, char *argv[]) {
NavigationController navigationController(model);
DebugTableController tableController(model);
FlappyBirdController flappyBirdController(model);
SnakeController snakeController(model);
ConfigurationController configurationController(model);
KeyboardController keyboardController(model);
DebugGraphController graphController(model);
Expand All @@ -76,6 +78,7 @@ int main(int argc, char *argv[]) {
&navigationController);
engine.rootContext()->setContextProperty("flappyBirdController",
&flappyBirdController);
engine.rootContext()->setContextProperty("snakeController", &snakeController);
engine.rootContext()->setContextProperty("configurationController",
&configurationController);
engine.rootContext()->setContextProperty("keyboardViewController",
Expand Down

0 comments on commit 4be6a17

Please sign in to comment.