Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#111 snake logic #124

Open
wants to merge 8 commits into
base: Develop
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion NERODevelopment/content/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ qt6_add_qml_module(content
TorqueValueComponent.qml
TorqueAdj.qml
SpeedMode.qml
Snake.qml
TimerDisplay.qml
MaxSpeedComparator.qml
MaxDrawGraph.qml
Expand All @@ -47,7 +48,6 @@ qt6_add_qml_module(content
Arrow.qml
OffScreen2.qml
StatusDisplay.qml
Snake.qml
SnakeBody.qml
SnakeFood.qml

Expand Down
180 changes: 164 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,181 @@ 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: Math.floor(gridWidth / 3 * 2)
property int y: Math.floor(gridHeight / 2)
}

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() {
let centerX = Math.floor(gridWidth / 2)
let centerY = Math.floor(gridHeight / 2)
snakeBody = [
{ x: centerX, y: centerY },
{ x: centerX - 1, y: centerY },
{ x: centerX - 2, y: centerY }
]
direction = 1
gameOver = false
score = 0
food.x = Math.floor(gridWidth / 3 * 2)
food.y = centerY
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
Loading