QT 贪吃蛇简单代码实现(原创用于理解原理,请勿随意传播)

本文通过QT框架,详细讲解了如何编写一个简单的贪吃蛇游戏,主要涉及main.cpp、mainwindow.h和mainwindow.cpp三个关键文件,旨在帮助读者理解游戏逻辑和QT编程原理。
摘要由CSDN通过智能技术生成

main.cpp 

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow *w = new MainWindow;
    w->show();

    return a.exec();
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    void paintEvent(QPaintEvent*event);
    void keyPressEvent(QKeyEvent*event);
    void keyReleaseEvent(QKeyEvent *event);

    void timerEvent(QTimerEvent *event);
    void move_judg(QString snake);

    int get_random_number(int min,int max);
    void set_food();
    void init_snake();
public:
    int snake_max = 100;
    int speed = 10;
    int timerate = 150;
    int maxview_x = 450;
    int maxview_y = 600;
    QString snakedirection = "down";

private:
    Ui::MainWindow *ui;

    int snake_x[100] =
    {
       50,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
    };
    int snake_y[100] =
    {
       30,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
       0,0,0,0,0,0
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值