还记得时代的贪吃蛇游戏吧,两只可爱的小蛇为了吃豆豆的冒险经历,贪吃蛇(也叫做贪食蛇)游戏是一款休闲益智类游戏,有PC和手机等多平台版本。既简单又耐玩。该游戏通过控制蛇头方向吃蛋,从而使得蛇变得越来越长。
今天小编整理了贪吃蛇的源码,大家可以试着去做一下,看看能不能做出来。
【Python/PySide2】贪吃蛇展示:
【Python/PySide2】贪吃蛇源代码:
import random
from enum import Enum
from sys import argv
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
class DIRECTION(Enum) :
"""
方向
"""
UP = 0
DOWN = 1
LEFT = 2
RIGHT = 3
#常量
SCREEN_WIDTH = 600 #屏幕宽度
SCREEN_HEIGHT = 600 #屏幕高度
UNIT_SIZE = 50 #格子边长
GAME_UNITS = int((SCREEN_WIDTH * SCREEN_HEIGHT) / UNIT_SIZE) #格子数量
DELAY = 150 #延迟
class GameThread(QThread) :
"""
游戏线程
"""
def __init__(self) - >None:
super(GameThread, self).__init__(None)
def run(self) - >None:
"""
游戏运行
"""
while w.running:
w.Move() #蛇移动
w.checkApple() #检查蛇头是否吃到苹果
w.checkCollisions() #检查蛇头是否撞到墙 / 蛇身
w.repaint() #重绘
app.processEvents() #处理事件
self.msleep(DELAY) #等待
class GameFrame(QWidget) :
"""
游戏窗口
"""
running = False #游戏是否正在运行
bodyParts = 6 #身体长度
applesEaten = 0 #已吃苹果数量
appleX = 0 #苹果X坐标
appleY = 0 #苹果Y坐标
direction = DIRECTION.RIGHT #蛇运动方向 (默认为右)
x = [0
for i in range(GAME_UNITS)] #蛇身x坐标
y = [0
for i in range(GAME_UNITS)] #蛇身y坐标
def __init__(self) - >None:
super(GameFrame, self).__init__()
self.gameThread = GameThread() #创建游戏线程
self.setWindowTitle(''''''''Snake'''''''') #设置窗口标题
self.setFixedSize(SCREEN_WIDTH, SCREEN_HEIGHT) #设置窗口固定大小
self.startGame() #开始游戏
def startGame(self) - >None:
"""
开始游戏
"""
self.running = True
self.newApple() #新建苹果
self.gameThread.start() #开始线程
def paintEvent(self, event: QPaintEvent) - >None:
"""
窗口绘制
"""
p = QPainter(self)
#背景绘制
p.fillRect(0, 0, self.width(), self.height(), Qt.GlobalColor.black)
#游戏结束
if not self.running:
p.setPen(Qt.GlobalColor.red) #设置为红色
p.setFont(QFont(''''''''微软雅黑'''''''', 20)) #设置字体
p.drawText(0, 0, self.width(), self.height(), Qt.AlignCenter, ''''''''Game Over! (Score: %d)'''''''' % self.applesEaten)
return
#游戏网格线
for i in range(int(SCREEN_HEIGHT / UNIT_SIZE)) :
p.setPen(Qt.GlobalColor.white) #设置为白色
p.drawLine(i * UNIT_SIZE, 0, i * UNIT_SIZE, SCREEN_HEIGHT) #竖线
p.drawLine(0, i * UNIT_SIZE, SCREEN_WIDTH, i * UNIT_SIZE) #横线
#苹果
brush = QBrush()
brush.setStyle(Qt.SolidPattern)
brush.setColor(Qt.GlobalColor.red)
p.setBrush(brush)
p.drawEllipse(self.appleX, self.appleY, UNIT_SIZE, UNIT_SIZE)
#蛇
brush.setColor(Qt.GlobalColor.green)
p.setBrush(brush)
for i in range(0, self.bodyParts) :
p.drawRect(self.x[i], self.y[i], UNIT_SIZE, UNIT_SIZE)
brush.setColor(QColor(45, 180, 0))
p.setBrush(brush)
#分数绘制
p.setPen(Qt.GlobalColor.red)
p.setFont(QFont(''''''''微软雅黑'''''''', 20))
p.drawText(0, 0, self.width(), self.height(), Qt.AlignHCenter, ''''''''Score: %d'''''''' % self.applesEaten)
def newApple(self) - >None:
"""
新建苹果
"""
self.appleX = random.randint(0, int(SCREEN_WIDTH / UNIT_SIZE) - 1) * UNIT_SIZE #随机产生坐标X
self.appleY = random.randint(0, int(SCREEN_HEIGHT / UNIT_SIZE) - 1) * UNIT_SIZE #随机产生坐标Y
def Move(self) - >None:
"""
蛇移动
"""
for i in range(self.bodyParts, 0, -1) :
self.x[i] = self.x[i - 1]
self.y[i] = self.y[i - 1]
if self.direction == DIRECTION.UP:
self.y[0] -= UNIT_SIZE
elif self.direction == DIRECTION.DOWN:
self.y[0] = UNIT_SIZE
elif self.direction == DIRECTION.LEFT:
self.x[0] -= UNIT_SIZE
elif self.direction == DIRECTION.RIGHT:
self.x[0] = UNIT_SIZE
def checkApple(self) - >None:
"""
检查蛇头是否吃到苹果
"""
if self.x[0] == self.appleX and self.y[0] == self.appleY:
self.bodyParts = 1
self.applesEaten = 1
self.newApple()
def checkCollisions(self) - >None:
"""
检查蛇头是否撞到墙/蛇身
"""
for i in range(self.bodyParts, 0, -1) :
if self.x[0] == self.x[i] and self.y[0] == self.y[i] :
self.running = False
return
if self.x[0] < 0 or self.x[0] > SCREEN_WIDTH or self.y[0] < 0 or self.y[0] > SCREEN_HEIGHT:
self.running = False
def keyPressEvent(self, event: QKeyEvent) - >None:
"""
捕获键盘输入
"""
if event.key() == Qt.Key_Up:
if self.direction != DIRECTION.DOWN:
self.direction = DIRECTION.UP
elif event.key() == Qt.Key_Down:
if self.direction != DIRECTION.UP:
self.direction = DIRECTION.DOWN
elif event.key() == Qt.Key_Left:
if self.direction != DIRECTION.RIGHT:
self.direction = DIRECTION.LEFT
elif event.key() == Qt.Key_Right:
if self.direction != DIRECTION.LEFT:
self.direction = DIRECTION.RIGHT
def closeEvent(self, event: QCloseEvent) - >None:
"""
关闭线程
"""
self.gameThread.terminate()
if __name__ == ''''''''__main__'''''''':
app = QApplication(argv)
w = GameFrame()
w.show()
exit(app.exec_())
转载声明:本文来源于网络,不作任何商业用途
IOS下载
安卓下载
小程序