用户:
信好离李阿查看:0 回复:0 评论:0 创建时间:2023-01-06T14:28:22
【作品展示】

【作品介绍】
请大佬多多指教!
【作品源代码】
import random
import sys
import pygame
from pygame.locals import *
FPS = 5 # 刷新帧率
WINDOW_WIDTH = 喵0 # 屏幕宽度
WINDOW_HEIGHT = 480 # 屏幕高度
CELLSIZE = 20 # 格子大小
CELLWIDTH = int(WINDOW_WIDTH / CELLSIZE) # 一行格子的个数
CELLHEIGHT = int(WINDOW_HEIGHT / CELLSIZE) # 一列格子的个数
WHITE = (255, 255, 255) # 白色
BLACK = (0, 0, 0) # 黑色
RED = (255, 0, 0) # 红色
GREEN = (0, 255, 0) # 绿色
DARKGREEN = (0, 155, 0) # 浅绿色
DARKGRAY = (40, 40, 40) # 浅灰色
BGCOLOR = BLACK # 背景色--黑色
"""新加程序1 开始++++++++++++++++++++++++++++++++++++++++++++++++++"""
UP = 'up'
DOWN = 'down'
LEFT = 'left'
RIGHT = 'right'
"""新加程序1 结束=================================================="""
def main(): # 游戏主逻辑
global FPSCLOCK, SCREEN, BASICFONT # FPS 游戏屏幕 文字字体
pygame.init() # 实例化
FPSCLOCK = pygame.time.Clock() # 帧率刷新
SCREEN = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) # 游戏屏幕
BASICFONT = pygame.font.Font(r'C:\Windows\Fonts\simkai.ttf', 18) # 文字设置
pygame.display.set_caption('贪吃蛇') # 游戏名称
showStartScreen()
while True: # 事件控制
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
runGame()
showGameOverScreen()
def showStartScreen():
WINDOW_WIDTH1 = 喵0 # 屏幕宽度
WINDOW_HEIGHT1 = 480 # 屏幕高度
titleFont = pygame.font.Font(r'C:\Windows\Fonts\simkai.ttf', 100) # 游戏首页旋转文字的字体设置
titleSurf1 = titleFont.render('贪吃蛇', True, WHITE, DARKGREEN) # 文字 1 设置
titleSurf2 = titleFont.render('贪吃蛇', True, GREEN) # 文字 2 设置
degrees1 = 0 # 文字 1 的角度
degrees2 = 0 # 文字 2 的角度
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == KEYDOWN:
return
SCREEN.fill(BGCOLOR)
rotatedSurf1 = pygame.transform.rotate(titleSurf1, degrees1) # 旋转文字1
rotatedRect1 = rotatedSurf1.get_rect() # 获取外切矩形
rotatedRect1.center = (WINDOW_WIDTH1 / 2, WINDOW_HEIGHT1 / 2) # 以文字的中心点,设置文字摆放位置
SCREEN.blit(rotatedSurf1, rotatedRect1)
rotatedSurf2 = pygame.transform.rotate(titleSurf2, degrees2) # 旋转文字2
rotatedRect2 = rotatedSurf2.get_rect() # 获取外切矩形
rotatedRect2.center = (WINDOW_WIDTH1 / 2, WINDOW_HEIGHT1 / 2) # 以文字的中心点,设置文字摆放位置
SCREEN.blit(rotatedSurf2, rotatedRect2)
drawPressKeyMsg() # 函数调用
pygame.display.update()
FPSCLOCK.tick(FPS)
degrees1 += 3
degrees2 += 7
def drawPressKeyMsg():
pressKeySurf = BASICFONT.render('按任意键进入游戏', True, WHITE)
pressKeyRect = pressKeySurf.get_rect()
pressKeyRect.topleft = (WINDOW_WIDTH - 200, WINDOW_HEIGHT - 30)
SCREEN.blit(pressKeySurf, pressKeyRect)
def drawGrid():
for x in range(0, WINDOW_WIDTH, CELLSIZE):
pygame.draw.line(SCREEN, DARKGRAY, (x, 0), (x, WINDOW_HEIGHT))
for y in range(0, WINDOW_HEIGHT, CELLSIZE):
pygame.draw.line(SCREEN, DARKGRAY, (0, y), (WINDOW_WIDTH, y ))
def getRandomLocation():
return {'x' : random.randint(0, CELLWIDTH - 1), 'y' : random.randint(0, CELLHEIGHT - 1)}
def runGame():
# 随机一个贪吃蛇的位置
startx = random.randint(5, CELLWIDTH - 5)
starty = random.randint(5, CELLHEIGHT - 5)
# 贪吃蛇
wormCoords = [{'x': startx,'y': starty},
{'x': startx - 1,'y':starty},
{'x': startx - 2, 'y':starty}]
"""新加程序1 开始++++++++++++++++++++++++++++++++++++++++++++++++++"""
# 初始移动方向
direction = RIGHT
"""新加程序1 结束=================================================="""
# 苹果
apple = getRandomLocation()
while True:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
"""新加程序3 开始++++++++++++++++++++++++++++++++++++++++++++++++++"""
if event.type == KEYDOWN:
if (event.key == K_LEFT or event.key == K_a) and direction != RIGHT:
direction = LEFT
elif (event.key == K_RIGHT or event.key == K_d) and direction != LEFT:
direction = RIGHT
elif (event.key == K_UP or event.key == K_w) and direction != DOWN:
direction = UP
elif (event.key == K_DOWN or event.key == K_s) and direction != UP:
direction = DOWN
elif event.key == K_ESCAPE:
sys.exit()
"""新加程序3 结束=================================================="""
"""新加程序4 开始++++++++++++++++++++++++++++++++++++++++++++++++++"""
# 贪吃蛇撞墙
if wormCoords[0]['x'] == -1 or wormCoords[0]['x'] == CELLWIDTH \
or wormCoords[0]['y'] == -1 or wormCoords[0]['y'] == CELLHEIGHT:
return
for wormBody in wormCoords[1:]: # 蛇头碰到了自己
if wormBody['x'] == wormCoords[0]['x'] and \
wormBody['y'] == wormCoords[0]['y']:
return
"""新加程序4 结束=================================================="""
"""新加程序5 开始++++++++++++++++++++++++++++++++++++++++++++++++++"""
# 贪吃蛇吃到苹果
if wormCoords[0]['x'] == apple['x'] and wormCoords[0]['y'] == apple['y']:
# 再生成随机苹果
apple = getRandomLocation()
else:
del wormCoords[-1] # 删除最后一节
"""新加程序5 结束=================================================="""
"""新加程序2 开始++++++++++++++++++++++++++++++++++++++++++++++++++"""
if direction == UP:
newHead = {'x': wormCoords[0]['x'], 'y':wormCoords[0]['y']-1}
elif direction == DOWN:
newHead = {'x': wormCoords[0]['x'], 'y':wormCoords[0]['y']+1}
elif direction == LEFT:
newHead = {'x': wormCoords[0]['x']-1, 'y':wormCoords[0]['y']}
elif direction == RIGHT:
newHead = {'x': wormCoords[0]['x']+1, 'y':wormCoords[0]['y']}
wormCoords.insert(0, newHead) # 新知识 insert(位置,对象) 用于将指定对象插入列表的指定位置。
"""新加程序2 结束=================================================="""
SCREEN.fill(BGCOLOR)
drawGrid() # 函数调用
drawWorm(wormCoords) # 绘制贪吃蛇
drawApple(apple) # 绘制苹果
drawScore(len(wormCoords) - 3) # 绘制分数
pygame.display.update()
FPSCLOCK.tick(FPS)
def drawWorm(wormCoords):
for coord in wormCoords:
x = coord['x'] * CELLSIZE
y = coord['y'] * CELLSIZE
wormSegmentRect = pygame.Rect(x, y, CELLSIZE, CELLSIZE)
pygame.draw.rect(SCREEN, DARKGREEN, wormSegmentRect)
wormInnerSegmentRect = pygame.Rect(x+4, y+4, CELLSIZE -8, CELLSIZE -8 )
pygame.draw.rect(SCREEN, DARKGREEN, wormInnerSegmentRect)
def drawScore(score):
scoreSurf = BASICFONT.render('分数:%s' % (score), True, WHITE)
scoreRect = scoreSurf.get_rect()
scoreRect.topleft = (WINDOW_WIDTH - 120, 10)
SCREEN.blit(scoreSurf, scoreRect)
def drawApple(coord):
x = coord['x'] * CELLSIZE
y = coord['y'] * CELLSIZE
appleRect = pygame.Rect(x, y, CELLSIZE, CELLSIZE)
pygame.draw.rect(SCREEN, RED, appleRect)
"""新加程序6 开始++++++++++++++++++++++++++++++++++++++++++++++++++"""
def showGameOverScreen():
gameOverFont = pygame.font.Font('C://Windows//Fonts//msyh.ttc', 150)
gameSurf = gameOverFont.render('游戏', True, WHITE)
overSurf = gameOverFont.render('失败', True, WHITE)
gameRect = gameSurf.get_rect()
overRect = overSurf.get_rect()
gameRect.midtop = (WINDOW_WIDTH / 2, 10)
overRect.midtop = (WINDOW_WIDTH / 2, gameRect.height + 10 + 25)
SCREEN.blit(gameSurf, gameRect)
SCREEN.blit(overSurf, overRect)
drawPressKeyMsg()
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
return
"""新加程序6 结束=================================================="""
if __name__ == "__main__":
main()
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn