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

二次元网站开发的意义南宁seo推广优化

二次元网站开发的意义,南宁seo推广优化,常熟公司网站建设电话,免费可以做旅游海报 的网站项目介绍 使用Pygame做一个乒乓球游戏。左侧为电脑#xff0c;右侧为玩家。 视频地址-YT 视频搬运-B站 视频教程约90分钟。 代码地址 环境#xff1a;需要pygame库#xff0c;可用pip安装#xff1a;pip install pygame 1. 基础版本 首先进行一些初始化#xff0c;初始…项目介绍 使用Pygame做一个乒乓球游戏。左侧为电脑右侧为玩家。 视频地址-YT 视频搬运-B站 视频教程约90分钟。 代码地址 环境需要pygame库可用pip安装pip install pygame 1. 基础版本 首先进行一些初始化初始化pygame以及物体的初始状态。 然后是主循环游戏的主循环主要包含3个内容 处理事件这里主要是键盘按键更新物体的状态在屏幕上绘制 # 基础 ping pang游戏 import sys import random import pygame# 初始化 pygame.init() clock pygame.time.Clock()screen_width 1280 screen_height 720 screen pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption(PingPang) # 使用长方形表示球和球拍 ball pygame.Rect(screen_width // 2 - 15, screen_height // 2 - 15, 30, 30) player pygame.Rect(screen_width - 20, screen_height // 2 - 70, 10, 140) opponent pygame.Rect(10, screen_height // 2 - 70, 10, 140)bg_color pygame.Color(grey12) light_grey (200, 200, 200)ball_speed_x 7 * random.choice((1, -1)) ball_speed_y 7 * random.choice((1, -1)) player_speed 0 opponent_speed 7while True:for event in pygame.event.get():if event.type pygame.QUIT:pygame.quit()sys.exit()if event.type pygame.KEYDOWN:if event.key pygame.K_DOWN:player_speed 7if event.key pygame.K_UP:player_speed - 7if event.type pygame.KEYUP:if event.key pygame.K_DOWN:player_speed - 7if event.key pygame.K_UP:player_speed 7# update#ball_animation()#player_animation()#opponent_animation()# drawscreen.fill(bg_color)pygame.draw.rect(screen, light_grey, player)pygame.draw.rect(screen, light_grey, opponent)pygame.draw.ellipse(screen, light_grey, ball)pygame.draw.aaline(screen, light_grey, (screen_width / 2, 0), (screen_width / 2, screen_height))pygame.display.flip()clock.tick(60)然后我们实现上面的三个更新逻辑更新物体状态。 ball_animation()player_animation()opponent_animation() def ball_animation():更新球的运动global ball_speed_x, ball_speed_yball.x ball_speed_xball.y ball_speed_yif ball.top 0 or ball.bottom screen_height:ball_speed_y * -1if ball.left 0 or ball.right screen_width:ball_speed_x * -1ball_restart()if ball.colliderect(player) or ball.colliderect(opponent):ball_speed_x * -1def player_animation():更新玩家的运动player.y player_speedif player.top 0:player.top 0if player.bottom screen_height:player.bottom screen_heightdef opponent_animation():更新对手的运动if opponent.top ball.y:opponent.top opponent_speedif opponent.bottom ball.y:opponent.bottom - opponent_speedif opponent.top 0:opponent.top 0if opponent.bottom screen_height:opponent.bottom screen_heightdef ball_restart():重置球的位置global ball_speed_x, ball_speed_yball.center (screen_width // 2, screen_height // 2)ball_speed_y * random.choice((1, -1))ball_speed_x * random.choice((1, -1))实现了这3个函数后记得在主循环中的# update 处调用这个三个函数。 2. 添加分数和时间 为游戏添加分数显示添加字体并渲染出分数。发球时有3秒倒计时通过pygame.time.get_ticks() 获得时间。 # 添加得分和计时器 import sys import random import pygamepygame.init() clock pygame.time.Clock()screen_width 1280 screen_height 720 screen pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption(PingPang)ball pygame.Rect(screen_width // 2 - 15, screen_height // 2 - 15, 30, 30) player pygame.Rect(screen_width - 20, screen_height // 2 - 70, 10, 140) opponent pygame.Rect(10, screen_height // 2 - 70, 10, 140)bg_color pygame.Color(grey12) light_grey (200, 200, 200)ball_speed_x 7 * random.choice((1, -1)) ball_speed_y 7 * random.choice((1, -1)) player_speed 0 opponent_speed 7# Text Variables player_score 0 opponent_score 0 # 创建字体 game_font pygame.font.Font(freesansbold.ttf, 32)# Timer score_time Truedef ball_animation():global ball_speed_x, ball_speed_yglobal player_score, opponent_scoreglobal score_timeball.x ball_speed_xball.y ball_speed_yif ball.top 0 or ball.bottom screen_height:ball_speed_y * -1if ball.left 0 or ball.right screen_width: if ball.left 0:player_score 1if ball.right screen_width:opponent_score 1score_time pygame.time.get_ticks()if ball.colliderect(player) or ball.colliderect(opponent):ball_speed_x * -1def player_animation():player.y player_speedif player.top 0:player.top 0if player.bottom screen_height:player.bottom screen_heightdef opponent_animation():if opponent.top ball.y:opponent.top opponent_speedif opponent.bottom ball.y:opponent.bottom - opponent_speedif opponent.top 0:opponent.top 0if opponent.bottom screen_height:opponent.bottom screen_heightdef ball_restart():global ball_speed_x, ball_speed_yglobal score_timeball.center (screen_width // 2, screen_height // 2)# 计算耗时并显示剩余时间# 获得当前时间current_time pygame.time.get_ticks()# 与上次得分时间比较if current_time - score_time 700:number_three game_font.render(3, False, light_grey)screen.blit(number_three, (screen_width // 2 - 10, screen_height // 2 20))if 700 current_time - score_time 1400:number_two game_font.render(2, False, light_grey)screen.blit(number_two, (screen_width // 2 - 10, screen_height // 2 20))if 1400 current_time - score_time 2100:number_one game_font.render(1, False, light_grey)screen.blit(number_one, (screen_width // 2 - 10, screen_height // 2 20))if current_time - score_time 2100:ball_speed_x, ball_speed_y 0, 0else:ball_speed_y 7 * random.choice((1, -1))ball_speed_x 7 * random.choice((1, -1))score_time Nonewhile True:for event in pygame.event.get():if event.type pygame.QUIT:pygame.quit()sys.exit()if event.type pygame.KEYDOWN:if event.key pygame.K_DOWN:player_speed 7if event.key pygame.K_UP:player_speed - 7if event.type pygame.KEYUP:if event.key pygame.K_DOWN:player_speed - 7if event.key pygame.K_UP:player_speed 7ball_animation()player_animation()opponent_animation()# update# drawscreen.fill(bg_color)pygame.draw.rect(screen, light_grey, player)pygame.draw.rect(screen, light_grey, opponent)pygame.draw.ellipse(screen, light_grey, ball)pygame.draw.aaline(screen, light_grey, (screen_width / 2, 0), (screen_width / 2, screen_height))# 显示得分player_text game_font.render(f{player_score}, False, light_grey)screen.blit(player_text, (660, 360))opponent_text game_font.render(f{opponent_score}, False, light_grey)screen.blit(opponent_text, (600, 360))if score_time:ball_restart()pygame.display.flip()clock.tick(60)3. 优化碰撞逻辑、添加声音 如果你运行了第2节的程序你会发现有时候球的反弹有时很奇怪比如有时候会黏在球拍上。 本节我们将 优化碰撞逻辑在ball_animation()通过判断球与球拍的位置修改球的运动。添加碰撞和得分音效: pygame.mixer.Sound # 添加得分和计时器 # 基础 ping pang游戏 import sys import random import pygame# setup pygame.init() pygame.mixer.pre_init(44100, -16, 2, 512) clock pygame.time.Clock()screen_width 1280 screen_height 720 screen pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption(PingPang)# Reactangles ball pygame.Rect(screen_width // 2 - 15, screen_height // 2 - 15, 30, 30) player pygame.Rect(screen_width - 20 , screen_height // 2 - 70, 10, 140) opponent pygame.Rect(10, screen_height // 2 - 70, 10, 140)bg_color pygame.Color(grey12) light_grey (200, 200, 200)ball_speed_x 7 * random.choice((1, -1)) ball_speed_y 7 * random.choice((1, -1)) player_speed 0 opponent_speed 7# Text Variables player_score 0 opponent_score 0 game_font pygame.font.Font(freesansbold.ttf, 32)# Timer score_time True# Sound pong_sound pygame.mixer.Sound(pong.ogg) score_sound pygame.mixer.Sound(score.ogg)def ball_animation():global ball_speed_x, ball_speed_yglobal player_score, opponent_scoreglobal score_timeball.x ball_speed_xball.y ball_speed_yif ball.top 0 or ball.bottom screen_height:pong_sound.play()ball_speed_y * -1# score if ball.left 0 or ball.right screen_width: score_sound.play()if ball.left 0:player_score 1if ball.right screen_width:opponent_score 1score_time pygame.time.get_ticks()if ball.colliderect(player) and ball_speed_x 0: pong_sound.play()if abs(ball.right - player.left) 10 :ball_speed_x * -1elif abs(ball.bottom - player.top) 10 and ball_speed_y 0:ball_speed_y * -1elif abs(ball.top - player.bottom) 10 and ball_speed_y 0:ball_speed_y * -1if ball.colliderect(opponent) and ball_speed_x 0:pong_sound.play()if abs(ball.left - opponent.right) 10:ball_speed_x * -1elif abs(ball.bottom - opponent.top) 10 and ball_speed_y 0:ball_speed_y * -1 elif abs(ball.top - opponent.bottom) 10 and ball_speed_y 0:ball_speed_y * -1def player_animation():player.y player_speedif player.top 0:player.top 0if player.bottom screen_height:player.bottom screen_heightdef opponent_animation():if opponent.top ball.y:opponent.top opponent_speedif opponent.bottom ball.y:opponent.bottom - opponent_speedif opponent.top 0:opponent.top 0if opponent.bottom screen_height:opponent.bottom screen_heightdef ball_restart():global ball_speed_x, ball_speed_yglobal score_timeball.center (screen_width // 2, screen_height // 2)current_time pygame.time.get_ticks()if current_time - score_time 700:number_three game_font.render(3, False, light_grey)screen.blit(number_three, (screen_width // 2 - 10, screen_height // 2 20))if 700 current_time - score_time 1400:number_two game_font.render(2, False, light_grey)screen.blit(number_two, (screen_width // 2 - 10, screen_height // 2 20))if 1400 current_time - score_time 2100:number_one game_font.render(1, False, light_grey)screen.blit(number_one, (screen_width // 2 - 10, screen_height // 2 20))if current_time - score_time 2100:ball_speed_x, ball_speed_y 0, 0else:ball_speed_y 7 * random.choice((1, -1))ball_speed_x 7 * random.choice((1, -1))score_time Nonewhile True:for event in pygame.event.get():if event.type pygame.QUIT:pygame.quit()sys.exit()if event.type pygame.KEYDOWN:if event.key pygame.K_DOWN:player_speed 7if event.key pygame.K_UP:player_speed - 7if event.type pygame.KEYUP:if event.key pygame.K_DOWN:player_speed - 7if event.key pygame.K_UP:player_speed 7ball_animation()player_animation()opponent_animation()# update# drawscreen.fill(bg_color)pygame.draw.rect(screen, light_grey, player)pygame.draw.rect(screen, light_grey, opponent)pygame.draw.ellipse(screen, light_grey, ball)pygame.draw.aaline(screen, light_grey, (screen_width / 2, 0), (screen_width / 2, screen_height))player_text game_font.render(f{player_score}, False, light_grey)screen.blit(player_text, (660, 360))opponent_text game_font.render(f{opponent_score}, False, light_grey)screen.blit(opponent_text, (600, 360))if score_time:ball_restart()pygame.display.flip()clock.tick(60)
http://www.hkea.cn/news/14386036/

相关文章:

  • 自由设计师网站医药网站开发
  • wordpress外贸网站模板室内建筑设计
  • 深圳做网站哪个平台好数据网站建设哪家好
  • 销售网站开发背景wordpress社交系统主题
  • 深圳办公室装修价格表湖北优化网站建设
  • 中小企业建站系统网站建设应注意的问题有哪些
  • 设计网站公司长沙建设银行河北分行网站
  • 免费网站正能量小说网站的设计 更新
  • 网站开发汇报的ppt凡科快图免费下载
  • 福建省建设执业资格中心网站网站开发公司对比
  • 如何制作网站策划书商城网站离不开支付系统
  • 网站系统怎么做的做电话销售需要的网站
  • 一键查询注册过的网站wordpress 主题替换
  • 美工需要的网站上海工商网上公示
  • wordpress建的网站打开太慢欧莱雅网站建设与推广方案
  • jsp网站开发介绍wordpress cms主题教程
  • 企业网站建设专业公司中国十佳网站建设公司
  • 深圳定制工作装seo优化网络推广
  • 招标网最新招标公告百度网站如何优化排名
  • 云霄网站建设wordpress 谷歌字体 360
  • 如何制作推广网站哪个网站可以做一对一老师
  • wordpress 站群软件做网站要会哪些知识
  • 网站建设的主要作用wordpress等待响应
  • 网站建设必须要服务器吗昆明网站建设织梦
  • 商城网站怎么做内链劳务派遣做网站的好处
  • 网站开发课程设计参考文献网站源码商城
  • wordpress 站外调用毕业设计网站建设选题依据
  • 杭州营销型网站pc端网站建设价格明细表
  • 生态文明建设网站专题培训石家庄做网站公司
  • 品牌建设的规划与实施汕头seo公司