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

住房和城乡建设部网站八大员企业营销策划及推广

住房和城乡建设部网站八大员,企业营销策划及推广,wordpress 商品页面,网站策划怎么做内容目录 一.运行效果 二.使用说明 三.完整代码 四.代码讲解 1.按钮类: 2.游戏前选择游戏难度界面: 2.1显示游戏名称、不同难度按钮、开始游戏按钮: 2.2难度选择时动画: 3.游戏界面历史最高分显示: 4.游戏结束界面…

     

目录

一.运行效果

二.使用说明

三.完整代码

四.代码讲解

1.按钮类:

2.游戏前选择游戏难度界面:

  2.1显示游戏名称、不同难度按钮、开始游戏按钮:

  2.2难度选择时动画:

3.游戏界面历史最高分显示:

4.游戏结束界面:

  4.1本局游戏分数、历史最高分数显示:

  4.2退出游戏和再玩一次功能实现:

五.总结


      本文的贪吃蛇小游戏根据从零开始用 Python 打造经典贪吃蛇游戏:代码+详解_pygame贪吃蛇游戏代码-CSDN博客进行改进优化,在原有的基础功能上增加了通过界面按钮可选择难度、历史最高分显示、结束游戏可选择继续游戏等功能,进一步优化了小游戏的体验感受。

一.运行效果

游戏开始界面:

游戏界面:

游戏结束界面:

二.使用说明

  首先我们打开Pycharm,没有安装的小伙伴可以看这个安装教程。新建一个项目,在项目里新建一个main.py文件,再把完整代码粘贴到main.py文件中,注意:

    ①.在终端输入以下命令安装pygame库:

pip install pygame

    ②.修改第31行的save_path为“你项目的地址/score.txt”;

再运行main.py即可。

三.完整代码

  话不多少,直接上完整代码:

import pygame
import random
import os
import sys# 初始化 pygame
pygame.init()# 屏幕大小
WIDTH, HEIGHT = 600, 400
CELL_SIZE = 20# 定义颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
TURQUOISE=(187,255,255)
CYAN=(0,229,238)
CYANA=(0,255,255)# 创建屏幕
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('贪吃蛇')# 初始化时钟
clock = pygame.time.Clock()#最高分保存地址
save_path = 'D:/pycharm/project/test/score.txt'#按钮类
class Button:def __init__(self, text, x, y, width, height, font_size, font_color):self.text = textself.rect = pygame.Rect(x, y, width, height)self.font = pygame.font.Font(None, font_size)self.text_surf = self.font.render(self.text, True, font_color)self.text_rect = self.text_surf.get_rect(center=self.rect.center)def draw(self, screenn,bg_color):pygame.draw.rect(screenn, bg_color, self.rect)screen.blit(self.text_surf, self.text_rect)def is_clicked(self, pos):if self.rect.collidepoint(pos):return True#创建按钮
play_again_button = Button('Play Again', WIDTH // 2 - 100, HEIGHT // 2 + 70, 200, 50, 30, WHITE)
game_over_button = Button('Exit', WIDTH // 2 - 100, HEIGHT // 2 + 20, 200, 50, 30, WHITE)
easy_button = Button('Easy', WIDTH // 2 - 100, HEIGHT // 2 - 50, 200, 30, 30, WHITE)
common_button = Button('Common', WIDTH // 2 - 100, HEIGHT // 2 -20, 200, 30, 30, WHITE)
difficult_button = Button('Diffcult', WIDTH // 2 - 100, HEIGHT // 2 +10, 200, 30, 30, WHITE)
play_button = Button('play', WIDTH // 2 - 100, HEIGHT // 2 +50, 200, 50, 30, WHITE)def draw_snake(snake_body):"""绘制蛇的身体"""for segment in snake_body:pygame.draw.rect(screen, GREEN, pygame.Rect(segment[0], segment[1], CELL_SIZE, CELL_SIZE))def show_message(text, color, size, position):"""显示信息"""font = pygame.font.Font(None, size)message = font.render(text, True, color)screen.blit(message, position)def game():#初始化本局得分和最高纪录score=0maxscore=0# 检查score文件是否存在if not os.path.exists(save_path):with open(save_path, 'w'):passelse:try:with open(save_path, 'r') as file:maxscore = int(file.read().strip())except ValueError:maxscore = 0except FileNotFoundError:maxscore = 0#选择游戏难度界面rundevel = Truenodickdevel = TrueSCR = 3while rundevel:screen.fill(BLACK)show_message("Greedy Snake", RED, 50, (WIDTH // 2 - 110, HEIGHT // 2 - 130))easy_button.draw(screen,TURQUOISE)common_button.draw(screen,TURQUOISE)difficult_button.draw(screen,TURQUOISE)play_button.draw(screen,GREEN)pygame.display.flip()if nodickdevel == True:if easy_button.is_clicked(pygame.mouse.get_pos()):easy_button.draw(screen, CYAN)common_button.draw(screen, TURQUOISE)difficult_button.draw(screen, TURQUOISE)pygame.display.flip()elif common_button.is_clicked(pygame.mouse.get_pos()):easy_button.draw(screen, TURQUOISE)common_button.draw(screen, CYAN)difficult_button.draw(screen, TURQUOISE)pygame.display.flip()elif difficult_button.is_clicked(pygame.mouse.get_pos()):easy_button.draw(screen, TURQUOISE)common_button.draw(screen, TURQUOISE)difficult_button.draw(screen, CYAN)pygame.display.flip()for event in pygame.event.get():if event.type == pygame.QUIT:pygame.quit()sys.exit()elif event.type == pygame.MOUSEBUTTONDOWN:if easy_button.is_clicked(event.pos):SCR =0movespeed=4nodickdevel=Falseelif common_button.is_clicked(event.pos):SCR=1movespeed=7nodickdevel = Falseelif difficult_button.is_clicked(event.pos):SCR=2movespeed = 10nodickdevel = Falseelif play_button.is_clicked(event.pos) and (SCR==0 or SCR==1 or SCR==2):rundevel=Falseif SCR==0:easy_button.draw(screen, CYANA)common_button.draw(screen, TURQUOISE)difficult_button.draw(screen, TURQUOISE)pygame.display.flip()elif SCR==1:easy_button.draw(screen, TURQUOISE)common_button.draw(screen, CYANA)difficult_button.draw(screen, TURQUOISE)pygame.display.flip()elif SCR==2:easy_button.draw(screen, TURQUOISE)common_button.draw(screen, TURQUOISE)difficult_button.draw(screen, CYANA)pygame.display.flip()clock.tick(30)# 初始化蛇snake_pos = [100, 40]  # 蛇头初始位置snake_body = [[100, 40], [80, 40], [60, 40]]  # 蛇身体direction = 'RIGHT'  # 蛇头初始方向change_to = direction# 初始化食物food_pos = [random.randrange(1, WIDTH // CELL_SIZE) * CELL_SIZE,random.randrange(1, HEIGHT // CELL_SIZE) * CELL_SIZE]food_spawn = True#游戏进行界面running = Truewhile running:# 监听按键事件for event in pygame.event.get():if event.type == pygame.QUIT:pygame.quit()sys.exit()elif event.type == pygame.KEYDOWN:if event.key == pygame.K_UP and direction != 'DOWN':change_to = 'UP'elif event.key == pygame.K_DOWN and direction != 'UP':change_to = 'DOWN'elif event.key == pygame.K_LEFT and direction != 'RIGHT':change_to = 'LEFT'elif event.key == pygame.K_RIGHT and direction != 'LEFT':change_to = 'RIGHT'direction = change_to# 更新蛇头位置if direction == 'UP':snake_pos[1] -= CELL_SIZEelif direction == 'DOWN':snake_pos[1] += CELL_SIZEelif direction == 'LEFT':snake_pos[0] -= CELL_SIZEelif direction == 'RIGHT':snake_pos[0] += CELL_SIZE# 增加蛇头snake_body.insert(0, list(snake_pos))# 检测是否吃到食物if snake_pos == food_pos:score += 10  # 更新分数food_spawn = Falseelse:snake_body.pop()# 刷新食物位置if not food_spawn:food_pos = [random.randrange(1, WIDTH // CELL_SIZE) * CELL_SIZE,random.randrange(1, HEIGHT // CELL_SIZE) * CELL_SIZE]food_spawn = True# 游戏结束条件if (snake_pos[0] < 0 or snake_pos[0] >= WIDTH orsnake_pos[1] < 0 or snake_pos[1] >= HEIGHT):running = False# 检测蛇是否碰到自己for block in snake_body[1:]:if snake_pos == block:running = False# 绘制屏幕screen.fill(BLACK)draw_snake(snake_body)pygame.draw.rect(screen, RED, pygame.Rect(food_pos[0], food_pos[1], CELL_SIZE, CELL_SIZE))show_message(f'Score: {score}', WHITE, 20, (10, 10))  # 确保每次循环都更新分数显示show_message(f'Max_score: {maxscore}', WHITE, 20, (490, 10))pygame.display.update()clock.tick(movespeed)#游戏结束界面run=Truewhile run:# 游戏结束信息if score>maxscore:with open(save_path, 'w') as file:file.write(str(score))screen.fill(BLACK)show_message("Game Over!", RED, 50, (WIDTH // 2 - 110, HEIGHT // 2 - 130))show_message(f"Your Score: {score}", WHITE, 30, (WIDTH // 2 - 100, HEIGHT // 2 -80))show_message(f"History MaxScore: {maxscore}", WHITE, 30, (WIDTH // 2 - 100, HEIGHT // 2-30))play_again_button.draw(screen,GREEN)game_over_button.draw(screen,RED)pygame.display.flip()for event in pygame.event.get():if event.type == pygame.QUIT:pygame.quit()sys.exit()elif event.type == pygame.MOUSEBUTTONDOWN:if play_again_button.is_clicked(event.pos):game()run=Falseif game_over_button.is_clicked(event.pos):pygame.quit()run=Falseif __name__ == "__main__":game()

四.代码讲解

1.按钮类:

  增加按钮类,用于按钮创建,定义显示按钮函数draw和检查点是否在按钮內函数is_clicked。

class Button:def __init__(self, text, x, y, width, height, font_size, font_color):self.text = textself.rect = pygame.Rect(x, y, width, height)self.font = pygame.font.Font(None, font_size)self.text_surf = self.font.render(self.text, True, font_color)self.text_rect = self.text_surf.get_rect(center=self.rect.center)def draw(self, screenn,bg_color):pygame.draw.rect(screenn, bg_color, self.rect)screen.blit(self.text_surf, self.text_rect)def is_clicked(self, pos):if self.rect.collidepoint(pos):return True

2.游戏前选择游戏难度界面:

  2.1显示游戏名称、不同难度按钮、开始游戏按钮:

show_message("Greedy Snake", RED, 50, (WIDTH // 2 - 110, HEIGHT // 2 - 130))
easy_button.draw(screen,TURQUOISE)
common_button.draw(screen,TURQUOISE)
difficult_button.draw(screen,TURQUOISE)
play_button.draw(screen,GREEN)

  2.2难度选择时动画:

    效果一:当鼠标移到难度按钮上时,鼠标所在按钮背景颜色加深。nodickdevel用于实现鼠标点击某个难度按钮后再移动鼠标时,不会产生效果一。

            if easy_button.is_clicked(pygame.mouse.get_pos()):easy_button.draw(screen, CYAN)common_button.draw(screen, TURQUOISE)difficult_button.draw(screen, TURQUOISE)pygame.display.flip()elif common_button.is_clicked(pygame.mouse.get_pos()):easy_button.draw(screen, TURQUOISE)common_button.draw(screen, CYAN)difficult_button.draw(screen, TURQUOISE)pygame.display.flip()elif difficult_button.is_clicked(pygame.mouse.get_pos()):easy_button.draw(screen, TURQUOISE)common_button.draw(screen, TURQUOISE)difficult_button.draw(screen, CYAN)pygame.display.flip()

    效果二:当鼠标点击不同难度按钮时,该难度按钮背景颜色锁定。SCR用于实现锁定后的按钮背景颜色显示;movespeed用于控制游戏时屏幕刷新快慢,即蛇移动快慢;

            elif event.type == pygame.MOUSEBUTTONDOWN:if easy_button.is_clicked(event.pos):SCR =0movespeed=4nodickdevel=Falseelif common_button.is_clicked(event.pos):SCR=1movespeed=7nodickdevel = Falseelif difficult_button.is_clicked(event.pos):SCR=2movespeed = 10nodickdevel = Falseelif play_button.is_clicked(event.pos) and (SCR==0 or SCR==1 or SCR==2):rundevel=False
        if SCR==0:easy_button.draw(screen, CYANA)common_button.draw(screen, TURQUOISE)difficult_button.draw(screen, TURQUOISE)pygame.display.flip()elif SCR==1:easy_button.draw(screen, TURQUOISE)common_button.draw(screen, CYANA)difficult_button.draw(screen, TURQUOISE)pygame.display.flip()elif SCR==2:easy_button.draw(screen, TURQUOISE)common_button.draw(screen, TURQUOISE)difficult_button.draw(screen, CYANA)pygame.display.flip()

3.游戏界面历史最高分显示:

show_message(f'Max_score: {maxscore}', WHITE, 20, (490, 10))

4.游戏结束界面:

  4.1本局游戏分数、历史最高分数显示:

        show_message(f"Your Score: {score}", WHITE, 30, (WIDTH // 2 - 100, HEIGHT // 2 -80))show_message(f"History MaxScore: {maxscore}", WHITE, 30, (WIDTH // 2 - 100, HEIGHT // 2-30))

  4.2退出游戏和再玩一次功能实现:

    此处逻辑很清晰,点击退出游戏直接全部退出,点击再玩一次再次运行游戏总函数game()。

            elif event.type == pygame.MOUSEBUTTONDOWN:if play_again_button.is_clicked(event.pos):game()run=Falseif game_over_button.is_clicked(event.pos):pygame.quit()run=False

五.总结

  通过本次对贪吃蛇游戏的改进和优化,提升了游戏的可玩性和趣味性,为玩家带来更加丰富和多样的游戏体验。如果你也对这款游戏感兴趣,不妨自己动手尝试一下,相信你会在编程的过程中找到无尽的乐趣和成就感!

http://www.dtcms.com/wzjs/63198.html

相关文章:

  • 珠海网站建设哪家公司好2022年十大网络流行语发布
  • 手机网站关闭窗口代码软件定制开发公司
  • 用dw制作视频网站武汉百度seo网站优化
  • 郑州外贸网站建设商家网页优化怎么做
  • 深圳知名网站建设价格网站制作建设公司
  • 帮人做网站要怎么赚钱吗广东疫情最新消息今天又封了
  • 日本人真人做真爱免费的网站百度网站的网址是什么
  • 做网站申请域名空间百度账号登陆
  • 博客可以放自己做的网站营销伎巧第一季
  • 用网站模板给人做网站挣钱吗百度推送
  • 做网站需要准备的东西培训课程开发
  • 软文自助发稿软件开发 网站建设网络营销推广工具
  • 常营网站建设公司2024北京又开始核酸了吗今天
  • 百度免费建立网站营销软文是什么
  • 济南网站建设公司熊掌号关键词优化公司靠谱推荐
  • 慢慢来做网站多少钱网站广告制作
  • 青岛网站建设价格seo推广怎么入门
  • 建公司网站要多久口碑营销的案例
  • 沈阳唐朝网站建设网络营销策划方案书范文
  • 免费html网页模板网站企业网站推广的一般策略
  • 江西网站建设与推广怎么建立自己的企业网站
  • wordpress 页面美化seo网站课程
  • 厦门北京网站建设指数基金怎么买
  • 如何建设影视网站首页海外推广营销 平台
  • 易语言做网站后端seo公司后付费
  • 怎么做游戏网站编辑万秀服务不错的seo推广
  • 5v贵阳做网站的价格1500元个性定制首选方舟网络百度seo关键词排名推荐
  • 哪些做批发的网站比较正规武汉seo招聘网
  • ps教程自学网官网seo营销推广全程实例
  • 天空在线网站建设域名注册购买