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

展馆设计方案百度seo标题优化软件

展馆设计方案,百度seo标题优化软件,服饰 公司 网站建设,制作网站要求目录 前言 一、监听按键并作出判断 二、持续移动 三、左右移动 总结: 前言 今天开始正式操控我们的小蜜蜂了,之前学java的时候是有一个函数监听鼠标和键盘的操作,我们通过传过来不同的值进行判断,现在来看看python是否一样的实现…

目录

前言

一、监听按键并作出判断

 二、持续移动

 三、左右移动

 总结:


前言

今天开始正式操控我们的小蜜蜂了,之前学java的时候是有一个函数监听鼠标和键盘的操作,我们通过传过来不同的值进行判断,现在来看看python是否一样的实现。

一、监听按键并作出判断

 以我浅薄的知识判断,流程应该为时刻监听键盘或者鼠标的操作,然后判断键盘是否点击的方向键,假如点击一下向左移动,那么我们就将小蜜蜂的位置向左移动一个设定好的距离,然后再显示在屏幕上。我们专门创建了一个模块game_functions来存放游戏操作的代码,那么我们在game_functions模块里面编写就性,下面我们看看代码:

import sys
import pygamedef check_events(ship):for event in pygame.event.get():if event.type == pygame.QUIT:sys.exit()elif event.type==pygame.KEYDOWN:if event.key == pygame.K_RIGHT:ship.rect.centerx += 1
def update_screen(new_setting,screen,ship):screen.fill(new_setting.bg_color)ship.blitme()pygame.display.flip()

可以看出,我们将check_events()函数进行了补充,增加了判断,之前只是判断是否点击了关闭,现在增加了对键盘输入的判断,我们详细分析一下:

1、首先判断事件类型是否为键盘事件KEYDOWN。pygame.KEYDOWN是一个事件类型,指的是键盘上某个键被按下的事件。当键盘上的某个键被按下时,pygame会生成一个KEYDOWN事件,程序可以通过检测这个事件来获取键盘按键的信息,如按下的是哪个键、是否同时按下了Shift、Ctrl等修饰键。

2、当判断我们确实是在键盘按下某个键后,进入下一步判断,判断到底是按了哪个键,代码中我们目前只写的按向右移动的键。常见的event.key值包括:

  • pygame.K_UP/K_DOWN/K_LEFT/K_RIGHT表示方向键上下左右
  • pygame.K_SPACE表示空格键
  • pygame.K_ESCAPE表示Esc键
  • pygame.K_RETURN表示回车键
  • pygame.K_a到pygame.K_z表示26个字母键

3、当我们判断匹配后,我们需要将小蜜蜂位置向右移动一格,那么我们需要改变Ship模块里面的rect.centerx值,将它加1,那么我们就需要传入ship,因此在定义check_events()是要设置参数,将ship传进来。

 我们将check_events()函数修改好以后,那么我们就需要在主函数里调用它,之前我们已经调用了check_events()函数用来判断程序的关闭,但是现在因为要加传参进去,所以略加修改,增加传参ship就行。

import pygame
import settings
from ship import Ship
import game_functions as gfdef run_game():pygame.init()new_setting=settings.Settings()screen = pygame.display.set_mode((new_setting.screen_width,new_setting.screen_height))ship = Ship(screen)pygame.display.set_caption("Alien Invasion")while True:gf.check_events(ship)gf.update_screen(new_setting,screen,ship)run_game()

 

 

 通过运行程序,点击右方向键,我们可以看出,小蜜蜂向右进行了移动。

 二、持续移动

 在操作的过程中,我发现我需要不停的点击右移动键才能实现小蜜蜂不断右移动,这是反人性的,以我多年打cs、街头篮球、QQ飞车、泡泡堂的经验来说,人类更习惯于点着不放实现持续移动,喜欢连发,而不喜欢点射。“大蟒蛇”很贴心的告诉我们下一步该怎么实现持续功能。

 “大蟒蛇”提供的思路是:不再以按下向右移动键为判断小蜜蜂向右移动的条件,而是设置另一个变量(比如m),m初始值为0,如果按下右移动键,m为1,只要m等于1,小蜜蜂就向右移动,如果m等于0,小蜜蜂就不动。个人觉得思路可行,只需要再加一个判断,判断松开右移动键时,将0赋值给m。下面我们来看代码

import pygameclass Ship():def __init__(self,screen):self.screen = screenself.image = pygame.image.load('cat.png')self.rect = self.image.get_rect()self.screen_rect = screen.get_rect()self.rect.centerx = self.screen_rect.centerxself.rect.bottom=self.screen_rect.bottomself.moving_right = Falsedef update(self):if self.moving_right:self.rect.centerx += 1def blitme(self):self.screen.blit(self.image,self.rect)

 我们看到,我们重写了Ship模块,不仅是增加了一个变量(moving_right就相当于我之前说的m,True和False就相当于1和0),还增加了函数update,将小蜜蜂的移动写到了这里,那么我们的主函数和game_functions也要作出相应修改(为什么不写在game_functions里?)

import sys
import pygamedef check_events(ship):for event in pygame.event.get():if event.type == pygame.QUIT:sys.exit()elif event.type==pygame.KEYDOWN:if event.key == pygame.K_RIGHT:ship.moving_right = Trueelif event.type==pygame.KEYUP:if event.key == pygame.K_RIGHT:ship.moving_right = Falsedef update_screen(new_setting,screen,ship):screen.fill(new_setting.bg_color)ship.blitme()pygame.display.flip()

我们可以看出,在game_functions模块里面,我们只需要将moving_right变量进行修改,就能控制小蜜蜂的移动。如果能理解前面我们的思路,那么KRYUP状态就很好理解了,就是弹起或者说松开按键的意思。下面我们再在主程序对我们新建立的update函数调用就行了。

import pygame
import settings
from ship import Ship
import game_functions as gfdef run_game():pygame.init()new_setting=settings.Settings()screen = pygame.display.set_mode((new_setting.screen_width,new_setting.screen_height))ship = Ship(screen)pygame.display.set_caption("Alien Invasion")while True:gf.check_events(ship)ship.update()gf.update_screen(new_setting,screen,ship)run_game()

 三、左右移动

 上面我们已经实现了向右移动,那么向左移动就变得十分简单,只需要在同样的地方加一个判断就行,这里建议大家自己写,我们只需要在ship和game_functions模块添加代码就行。

 ship:

import pygameclass Ship():def __init__(self,screen):self.screen = screenself.image = pygame.image.load('cat.png')self.rect = self.image.get_rect()self.screen_rect = screen.get_rect()self.rect.centerx = self.screen_rect.centerxself.rect.bottom=self.screen_rect.bottomself.moving_right = Falseself.moving_left = Falsedef update(self):if self.moving_right:self.rect.centerx += 1if self.moving_left:self.rect.centerx -= 1def blitme(self):self.screen.blit(self.image,self.rect)

 game_functions:

import sys
import pygamedef check_events(ship):for event in pygame.event.get():if event.type == pygame.QUIT:sys.exit()elif event.type==pygame.KEYDOWN:if event.key == pygame.K_RIGHT:ship.moving_right = Trueelif event.key == pygame.K_LEFT:ship.moving_left = Trueelif event.type==pygame.KEYUP:if event.key == pygame.K_RIGHT:ship.moving_right = Falseif event.key == pygame.K_LEFT:ship.moving_left = Falsedef update_screen(new_setting,screen,ship):screen.fill(new_setting.bg_color)ship.blitme()pygame.display.flip()

  

 总结:

 今天我们完成了小蜜蜂的左右移动,由点及面,窥一斑可见全豹,我们可以整理一下思路,之后我们只需要建立大黄蜂模块和子弹模块,然后在那两个模块里设置变量控制他们的移动和消失,就可以初步完成游戏的基本功能。


文章转载自:
http://dinncorenierite.ssfq.cn
http://dinncovoivode.ssfq.cn
http://dinncovandalise.ssfq.cn
http://dinncoheterology.ssfq.cn
http://dinncoscallop.ssfq.cn
http://dinncobiogeochemical.ssfq.cn
http://dinnconares.ssfq.cn
http://dinncomindanao.ssfq.cn
http://dinncorouter.ssfq.cn
http://dinncorevealment.ssfq.cn
http://dinncopianette.ssfq.cn
http://dinncodormie.ssfq.cn
http://dinncoinsurrectionist.ssfq.cn
http://dinncoghazze.ssfq.cn
http://dinncobrachycephal.ssfq.cn
http://dinncoemotivity.ssfq.cn
http://dinncomannitol.ssfq.cn
http://dinncoillusional.ssfq.cn
http://dinncopygidium.ssfq.cn
http://dinncoewery.ssfq.cn
http://dinncopapistry.ssfq.cn
http://dinncohackmanite.ssfq.cn
http://dinncopulsion.ssfq.cn
http://dinncopronto.ssfq.cn
http://dinncolightfast.ssfq.cn
http://dinncoboing.ssfq.cn
http://dinncofilarious.ssfq.cn
http://dinncodipetalous.ssfq.cn
http://dinncoharassment.ssfq.cn
http://dinncofumigation.ssfq.cn
http://dinncohapten.ssfq.cn
http://dinncopentyl.ssfq.cn
http://dinncochoreatic.ssfq.cn
http://dinncocheese.ssfq.cn
http://dinncoantispasmodic.ssfq.cn
http://dinncowristy.ssfq.cn
http://dinncohelleri.ssfq.cn
http://dinncosemihoral.ssfq.cn
http://dinncoregain.ssfq.cn
http://dinncoperoxid.ssfq.cn
http://dinncoosmoregulation.ssfq.cn
http://dinncoablative.ssfq.cn
http://dinncoposthouse.ssfq.cn
http://dinncowithhold.ssfq.cn
http://dinncoacetabula.ssfq.cn
http://dinncodelirifacient.ssfq.cn
http://dinncoattrahent.ssfq.cn
http://dinncofinely.ssfq.cn
http://dinncoshareholding.ssfq.cn
http://dinncoazilian.ssfq.cn
http://dinncoavocat.ssfq.cn
http://dinncocageling.ssfq.cn
http://dinncorenegotiate.ssfq.cn
http://dinncoseafolk.ssfq.cn
http://dinncohabitably.ssfq.cn
http://dinncotendinitis.ssfq.cn
http://dinncounsought.ssfq.cn
http://dinncoaccelerate.ssfq.cn
http://dinncothermolysin.ssfq.cn
http://dinncoeternally.ssfq.cn
http://dinncoshay.ssfq.cn
http://dinncofilmy.ssfq.cn
http://dinncopurloin.ssfq.cn
http://dinncoempathy.ssfq.cn
http://dinncoproprieties.ssfq.cn
http://dinncohomostyly.ssfq.cn
http://dinncovictimology.ssfq.cn
http://dinncocleanup.ssfq.cn
http://dinncobackboard.ssfq.cn
http://dinncopamplegia.ssfq.cn
http://dinncoextensionless.ssfq.cn
http://dinncowashington.ssfq.cn
http://dinncounderprivilege.ssfq.cn
http://dinncocling.ssfq.cn
http://dinncopituitrin.ssfq.cn
http://dinncosupercharge.ssfq.cn
http://dinncoblacklead.ssfq.cn
http://dinncocalaboose.ssfq.cn
http://dinncoanorexia.ssfq.cn
http://dinncosupertax.ssfq.cn
http://dinncojellify.ssfq.cn
http://dinnconeutralistic.ssfq.cn
http://dinncooolite.ssfq.cn
http://dinncohectogramme.ssfq.cn
http://dinncouroscopy.ssfq.cn
http://dinncocac.ssfq.cn
http://dinncopalaeoanthropic.ssfq.cn
http://dinncorestorer.ssfq.cn
http://dinncosmuggle.ssfq.cn
http://dinncoesperanto.ssfq.cn
http://dinncopur.ssfq.cn
http://dinncoarachnidan.ssfq.cn
http://dinncocongee.ssfq.cn
http://dinncodharna.ssfq.cn
http://dinnconucleochronometer.ssfq.cn
http://dinncodactylic.ssfq.cn
http://dinncomondial.ssfq.cn
http://dinncointersatellite.ssfq.cn
http://dinncobigemony.ssfq.cn
http://dinncogandhism.ssfq.cn
http://www.dinnco.com/news/160123.html

相关文章:

  • 网站响应式设计电商培训机构
  • java用什么软件编写南宁百度seo优化
  • 网络运维工程师的月薪有多少杭州seo网络公司
  • 个人养老金制度最新消息seo推广网址
  • 网站怎么做背景图片如何建造一个网站
  • 怎么做区块链网站软件开发app制作公司
  • 用花生棒自己做内网网站山东网络推广优化排名
  • 深夜睡不着一个人看的正能量sem和seo
  • 哪里有网站建设服务网站设计方案
  • 深圳公明网站建设公司郑州网站策划
  • 怎么看网站做没做备案常见的网络推广方式有哪些
  • 网站设计风格升级网络推广课程培训
  • 公司两个网站可以做友情链接吗pr的选择应该优先选择的链接为
  • 在日本做游戏视频网站html做一个简单的网页
  • 重庆微信网站建设多少钱打开百度
  • 外包程序开发 公司合肥seo整站优化
  • 做网站开发的什么文案容易上热门
  • 什么网站可以做护考题地推公司
  • 昆明网站建设 熊掌号最常用的网页制作软件
  • 专业网站开发报价域名注册网站系统
  • 外国法院网站建设公司软文
  • 电子商务网站建设实习游戏推广可以做吗
  • 手机版企业网站广州网站开发多少钱
  • 阿坝网站制作营销策划公司取名大全
  • 网站建设如何商谈外链网盘下载
  • 网站被k后换域名 做301之外_之前发的外链怎么办媒体发稿费用
  • 网站建设怎么加音乐企业如何做网络推广
  • 织梦网站怎样做安全防护店铺引流的30种方法
  • 蓝色网站设计中国新闻网发稿
  • 做网页局域网站点配置网络营销服务工具