/
Shymer123
/
Snake
Обзор
Документация
Войти
/
Shymer123
/
Snake
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
snake.cpp
232 строки
4 KB
Shymer123
Add images snake
05 май 2024, 16:49
05 май 2024, 16:49
4722035
Код
Авторство
О чём код?
#include "Snake.h" #include "./ui_snake.h" Snake::Snake(QWidget *parent) : QWidget(parent) , ui(new Ui::Snake) { ui->setupUi(this); setStyleSheet("background-color:black;"); leftDirection = false; rightDirection = true; upDirection = false; downDirection = false; inGame = true; resize(B_WIDTH, B_HEIGHT); loadImages(); initGame(); } Snake::~Snake() { delete ui; } void Snake::paintEvent(QPaintEvent *e) { Q_UNUSED(e); doDrawing(); } void Snake::timerEvent(QTimerEvent *e) { Q_UNUSED(e); if(inGame) { checkApple(); checkCollision(); move(); } repaint(); } void Snake::keyPressEvent(QKeyEvent *e) { int key = e->key(); if((key == Qt::Key_Left) && (!rightDirection)) { leftDirection = true; upDirection = false; downDirection = false; } if((key == Qt::Key_Right) && (!leftDirection)) { rightDirection = true; upDirection = false; downDirection = false; } if((key == Qt::Key_Up) && (!downDirection)) { upDirection = true; rightDirection = false; leftDirection = false; } if((key == Qt::Key_Down) && (!upDirection)) { downDirection = true; rightDirection = false; leftDirection = false; } QWidget::keyPressEvent(e); } void Snake::loadImages() { QString dotPath = ":/image/Images/dot.png"; QString headPath = ":/image/Images/head.png"; QString applePath = ":/image/Images/apple.png"; dot.load(dotPath); head.load(headPath); apple.load(applePath); } void Snake::initGame() { dots = 3; for(int z = 0; z < dots; ++z) { x[z] = 50 - z * 10; y[z] = 50; } locateApple(); timerID = startTimer(DELAY); } void Snake::locateApple() { QTime time = QTime::currentTime(); srand((uint) time.msec()); int r = rand() % RAND_POS; apple_x = (r * DOT_SIZE); r = rand() % RAND_POS; apple_y = (r * DOT_SIZE); } void Snake::checkApple() { if((x[0] == apple_x) && (y[0] == apple_y)) { ++dots; locateApple(); } } void Snake::checkCollision() { for(int z = dots; z > 0; z--) { if((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) { inGame = false; } } if(x[0] >= B_WIDTH) { inGame = false; } if(x[0] < 0) { inGame = false; } if(y[0] >= B_HEIGHT) { inGame = false; } if(y[0] < 0) { inGame = false; } if(!inGame) { killTimer(timerID); } } void Snake::move() { for(int z = dots; z > 0; z--) { x[z] = x[(z - 1)]; y[z] = y[(z - 1)]; } if(leftDirection) { x[0] -= DOT_SIZE; } if(rightDirection) { x[0] += DOT_SIZE; } if(upDirection) { y[0] -= DOT_SIZE; } if(downDirection) { y[0] += DOT_SIZE; } } void Snake::doDrawing() { QPainter qp(this); if(inGame) { qp.drawImage(apple_x, apple_y, apple); for(int z = 0; z < dots; ++z) { if(z == 0) { qp.drawImage(x[z], y[z], head); } else { qp.drawImage(x[z], y[z], dot); } } } else { gameOver(qp); } } void Snake::gameOver(QPainter &qp) { QString message = "Game Over"; QFont font("Courier", 15, QFont::DemiBold); QFontMetrics fm(font); int textWidth = fm.horizontalAdvance(message); qp.setPen(QColor(Qt::white)); qp.setFont(font); int h = height(); int w = width(); qp.translate(QPoint(w/2, h/2)); qp.drawText(-textWidth/2, 0, message); }