当前位置: 首页 > news >正文

石家庄做网站设计站长工具seo综合

石家庄做网站设计,站长工具seo综合,抛丸机网站排名优化,短故事网站模板文章目录 导入所需的模块坐标主游戏循环模块得分 贪吃蛇小游戏,那个曾经陪伴着00后和90后度过无数欢笑时光的熟悉身影,仿佛是一把打开时光之门的钥匙。它不仅是游戏世界的经典之一,更是我们童年岁月中不可或缺的一部分,一个承载回…

文章目录

    • 导入所需的模块
    • 坐标
    • 主游戏循环模块
    • 得分

贪吃蛇小游戏,那个曾经陪伴着00后和90后度过无数欢笑时光的熟悉身影,仿佛是一把打开时光之门的钥匙。它不仅是游戏世界的经典之一,更是我们童年岁月中不可或缺的一部分,一个承载回忆的游乐场。

在这里插入图片描述

今天,我怀着对过去美好时光的怀念,决定带领大家一同重温这个经典之作。十分钟的时间,足够我们完成一个贪吃蛇小游戏的从零到一的制作过程,唤起那些沉淀在记忆深处的片段,重新演绎一场美好的回忆之旅。

导入所需的模块

# 导入所需模块
import random
import sys
import time
import pygame
from pygame.locals import *
from collections import deque

坐标

游戏开发的过程就像是一场时光穿越,从屏幕宽度、高度到小方格大小的定义,每一行代码都是对童年时光的致敬。这个过程不仅是技术的挑战,更是对那段充满激情和无限可能的年华的致敬。

贪吃蛇的身影在初始化的瞬间重新出现,如同见到了一个久违的老友。我们为它赋予了起始的位置和长度,仿佛重新揭开了小时候那个属于自己的游戏序幕。这个小小的蛇身,是我们成长路上最初的探险者,见证了我们一步步蜕变的过程。

SCREEN_WIDTH = 600      # 屏幕宽度
SCREEN_HEIGHT = 480     # 屏幕高度
SIZE = 20               # 小方格大小
LINE_WIDTH = 1          # 网格线宽度
SCOPE_X = (0, SCREEN_WIDTH // SIZE - 1)  # 游戏区域的坐标范围
SCOPE_Y = (2, SCREEN_HEIGHT // SIZE - 1)FOOD_STYLE_LIST = [(10, (255, 100, 100)), (20, (100, 255, 100)), (30, (100, 100, 255))]
LIGHT = (100, 100, 100)
DARK = (200, 200, 200)      # 蛇的颜色
BLACK = (0, 0, 0)           # 网格线颜色
RED = (200, 30, 30)         # 红色,GAME OVER 的字体颜色
BGCOLOR = (40, 40, 60)      # 背景色def print_text(screen, font, x, y, text, fcolor=(255, 255, 255)):imgText = font.render(text, True, fcolor)screen.blit(imgText, (x, y))def init_snake():snake = deque()snake.append((2, SCOPE_Y[0]))snake.append((1, SCOPE_Y[0]))snake.append((0, SCOPE_Y[0]))return snakedef create_food(snake):food_x = random.randint(SCOPE_X[0], SCOPE_X[1])food_y = random.randint(SCOPE_Y[0], SCOPE_Y[1])while (food_x, food_y) in snake:food_x = random.randint(SCOPE_X[0], SCOPE_X[1])food_y = random.randint(SCOPE_Y[0], SCOPE_Y[1])return food_x, food_ydef get_food_style():return FOOD_STYLE_LIST[random.randint(0, 2)]

接下来,食物的生成成为游戏中一个不可或缺的元素。通过随机生成食物的位置、不同食物的分值和颜色,我们为游戏注入了更多的趣味性和挑战性。这一步仿佛是在重新编织那些小时候追逐食物的记忆,让我们感受到小时候的单纯和快乐。

主游戏循环模块

游戏循环的设计,通过键盘输入监听实现蛇的移动和游戏的暂停,让我们仿佛回到了小时候手持小键盘的场景。这个过程不仅是对技术的实践,更是一次与自己童年亲密互动的体验。

# 主游戏循环模块
def main():pygame.init()screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))pygame.display.set_caption('贪吃蛇')font1 = pygame.font.SysFont('SimHei', 24)  # 得分的字体font2 = pygame.font.Font(None, 72)  # GAME OVER 的字体fwidth, fheight = font2.size('GAME OVER')b = True  # 防止蛇在移动时方向被覆盖的标志snake = init_snake()food = create_food(snake)food_style = get_food_style()pos = (1, 0)game_over = Truestart = Falsescore = 0orispeed = 0.5speed = orispeedlast_move_time = Nonepause = Falsewhile True:for event in pygame.event.get():if event.type == QUIT:sys.exit()elif event.type == KEYDOWN:if event.key == K_RETURN:if game_over:start = Truegame_over = Falseb = Truesnake = init_snake()food = create_food(snake)food_style = get_food_style()pos = (1, 0)score = 0last_move_time = time.time()elif event.key == K_SPACE:if not game_over:pause = not pauseelif event.key in (K_w, K_UP):if b and not pos[1]:pos = (0, -1)b = Falseelif event.key in (K_s, K_DOWN):if b and not pos[1]:pos = (0, 1)b = Falseelif event.key in (K_a, K_LEFT):if b and not pos[0]:pos = (-1, 0)b = Falseelif event.key in (K_d, K_RIGHT):if b and not pos[0]:pos = (1, 0)b = Falsescreen.fill(BGCOLOR)for x in range(SIZE, SCREEN_WIDTH, SIZE):pygame.draw.line(screen, BLACK, (x, SCOPE_Y[0] * SIZE), (x, SCREEN_HEIGHT), LINE_WIDTH)for y in range(SCOPE_Y[0] * SIZE, SCREEN_HEIGHT, SIZE):pygame.draw.line(screen, BLACK, (0, y), (SCREEN_WIDTH, y), LINE_WIDTH)if not game_over:curTime = time.time()if curTime - last_move_time > speed:if not pause:b = Truelast_move_time = curTimenext_s = (snake[0][0] + pos[0], snake[0][1] + pos[1])if next_s == food:snake.appendleft(next_s)score += food_style[0]speed = orispeed - 0.03 * (score // 100)food = create_food(snake)food_style = get_food_style()else:if SCOPE_X[0] <= next_s[0] <= SCOPE_X[1] and SCOPE_Y[0] <= next_s[1] <= SCOPE_Y[1] \and next_s not in snake:snake.appendleft(next_s)snake.pop()else:game_over = Trueif not game_over:pygame.draw.rect(screen, food_style[1], (food[0] * SIZE, food[1] * SIZE, SIZE, SIZE), 0)for s in snake:pygame.draw.rect(screen, DARK, (s[0] * SIZE + LINE_WIDTH, s[1] * SIZE + LINE_WIDTH,SIZE - LINE_WIDTH * 2, SIZE - LINE_WIDTH * 2), 0)print_text(screen, font1, 30, 7, f'速度: {score//100}')print_text(screen, font1, 450, 7, f'得分: {score}')if game_over:if start:print_text(screen, font2, (SCREEN_WIDTH - fwidth) // 2, (SCREEN_HEIGHT - fheight) // 2, 'GAME OVER', RED)pygame.display.update()if __name__ == '__main__':main()

得分

得分、速度、游戏结束的提示,这些细节的设计仿佛勾勒出了那个属于00后和90后的美好时光。每一分每一秒,都是成长路上的一个标记,而游戏结束的时候,又是对那段失落感和温馨感的深刻回味。

十分钟的开发过程,从零到一,我们完成了一个小小的贪吃蛇游戏。这不仅是对技术的挑战,更是对自己童年回忆的一次丰富的续写。这个小小的游戏带着我们穿越时光的隧道,让我们再次感受到小时候单纯而美好的游戏时光。

在这短短的时间里,我们不仅仅是在屏幕上编写代码,更是在搭建一个关于童年回忆的小世界。贪吃蛇小游戏的复刻,不仅是对技术能力的锻炼,更是对那段纯真岁月的怀念。或许,这个小游戏成为了一个时光机,将我们带回了那个曾经充满梦想和勇气的时代。

全部代码:

# 贪吃蛇游戏模块
import random
import sys
import time
import pygame
from pygame.locals import *
from collections import dequeSCREEN_WIDTH = 600      # 屏幕宽度
SCREEN_HEIGHT = 480     # 屏幕高度
SIZE = 20               # 小方格大小
LINE_WIDTH = 1          # 网格线宽度
SCOPE_X = (0, SCREEN_WIDTH // SIZE - 1)  # 游戏区域的坐标范围
SCOPE_Y = (2, SCREEN_HEIGHT // SIZE - 1)FOOD_STYLE_LIST = [(10, (255, 100, 100)), (20, (100, 255, 100)), (30, (100, 100, 255))]
LIGHT = (100, 100, 100)
DARK = (200, 200, 200)      # 蛇的颜色
BLACK = (0, 0, 0)           # 网格线颜色
RED = (200, 30, 30)         # 红色,GAME OVER 的字体颜色
BGCOLOR = (40, 40, 60)      # 背景色def print_text(screen, font, x, y, text, fcolor=(255, 255, 255)):imgText = font.render(text, True, fcolor)screen.blit(imgText, (x, y))def init_snake():snake = deque()snake.append((2, SCOPE_Y[0]))snake.append((1, SCOPE_Y[0]))snake.append((0, SCOPE_Y[0]))return snakedef create_food(snake):food_x = random.randint(SCOPE_X[0], SCOPE_X[1])food_y = random.randint(SCOPE_Y[0], SCOPE_Y[1])while (food_x, food_y) in snake:food_x = random.randint(SCOPE_X[0], SCOPE_X[1])food_y = random.randint(SCOPE_Y[0], SCOPE_Y[1])return food_x, food_ydef get_food_style():return FOOD_STYLE_LIST[random.randint(0, 2)]# 主游戏循环模块
def main():pygame.init()screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))pygame.display.set_caption('贪吃蛇')font1 = pygame.font.SysFont('SimHei', 24)  # 得分的字体font2 = pygame.font.Font(None, 72)  # GAME OVER 的字体fwidth, fheight = font2.size('GAME OVER')b = True  # 防止蛇在移动时方向被覆盖的标志snake = init_snake()food = create_food(snake)food_style = get_food_style()pos = (1, 0)game_over = Truestart = Falsescore = 0orispeed = 0.5speed = orispeedlast_move_time = Nonepause = Falsewhile True:for event in pygame.event.get():if event.type == QUIT:sys.exit()elif event.type == KEYDOWN:if event.key == K_RETURN:if game_over:start = Truegame_over = Falseb = Truesnake = init_snake()food = create_food(snake)food_style = get_food_style()pos = (1, 0)score = 0last_move_time = time.time()elif event.key == K_SPACE:if not game_over:pause = not pauseelif event.key in (K_w, K_UP):if b and not pos[1]:pos = (0, -1)b = Falseelif event.key in (K_s, K_DOWN):if b and not pos[1]:pos = (0, 1)b = Falseelif event.key in (K_a, K_LEFT):if b and not pos[0]:pos = (-1, 0)b = Falseelif event.key in (K_d, K_RIGHT):if b and not pos[0]:pos = (1, 0)b = Falsescreen.fill(BGCOLOR)for x in range(SIZE, SCREEN_WIDTH, SIZE):pygame.draw.line(screen, BLACK, (x, SCOPE_Y[0] * SIZE), (x, SCREEN_HEIGHT), LINE_WIDTH)for y in range(SCOPE_Y[0] * SIZE, SCREEN_HEIGHT, SIZE):pygame.draw.line(screen, BLACK, (0, y), (SCREEN_WIDTH, y), LINE_WIDTH)if not game_over:curTime = time.time()if curTime - last_move_time > speed:if not pause:b = Truelast_move_time = curTimenext_s = (snake[0][0] + pos[0], snake[0][1] + pos[1])if next_s == food:snake.appendleft(next_s)score += food_style[0]speed = orispeed - 0.03 * (score // 100)food = create_food(snake)food_style = get_food_style()else:if SCOPE_X[0] <= next_s[0] <= SCOPE_X[1] and SCOPE_Y[0] <= next_s[1] <= SCOPE_Y[1] \and next_s not in snake:snake.appendleft(next_s)snake.pop()else:game_over = Trueif not game_over:pygame.draw.rect(screen, food_style[1], (food[0] * SIZE, food[1] * SIZE, SIZE, SIZE), 0)for s in snake:pygame.draw.rect(screen, DARK, (s[0] * SIZE + LINE_WIDTH, s[1] * SIZE + LINE_WIDTH,SIZE - LINE_WIDTH * 2, SIZE - LINE_WIDTH * 2), 0)print_text(screen, font1, 30, 7, f'速度: {score//100}')print_text(screen, font1, 450, 7, f'得分: {score}')if game_over:if start:print_text(screen, font2, (SCREEN_WIDTH - fwidth) // 2, (SCREEN_HEIGHT - fheight) // 2, 'GAME OVER', RED)pygame.display.update()if __name__ == '__main__':main()
http://www.hkea.cn/news/928809/

相关文章:

  • 竹溪县县建设局网站短视频营销
  • 好的网站有哪些搜索引擎seo是什么意思
  • 做音乐网站赚钱吗做小程序的公司
  • 坪地网站建设域名流量查询工具
  • 网站建设部署万能推广app
  • 网站的重要性怎么做个网站
  • 做网站的经验百度旗下有哪些app
  • 化工网站开发推广点击器
  • 怎么访问日本竹中建设网站外贸seo推广
  • 惠阳建设局网站引流推广接单
  • 北京通州网站建设公司如何建立公司网站网页
  • 网站换程序301seo优化按天扣费
  • html5 网站自适应长尾关键词挖掘爱站工具
  • 网站设计公司(信科网络)潍坊网站定制模板建站
  • 番禺网站开发报价百度竞价排名软件
  • 做企业网站接单seo网站优化技术
  • 建设网站行业云网络推广理实一体化软件
  • 如何用自己公司网站做邮箱关键字是什么意思
  • 古典网站建设欣赏马鞍山网站seo
  • 商城网站建设报价方案免费建网站软件下载
  • 中国做美国酒店的网站好竞价托管收费标准
  • 网站开发与设计静态网页源代码站长之家app下载
  • 松原做网站app运营推广是干什么
  • 做简单的网站链接2024新闻热点摘抄
  • 百度网站站长环球网疫情最新
  • 颍上做网站西安seo网站关键词优化
  • 有没有兼职做设计的网站吗知名网络软文推广平台
  • 数据百度做网站好用吗米拓建站
  • 网站维护运营怎么做搜索引擎优化通常要注意的问题有
  • 圆梦科技专业网站建设恶意点击软件有哪些