python實(shí)現(xiàn)飛船游戲的縱向移動(dòng)
本文實(shí)例為大家分享了python實(shí)現(xiàn)飛船游戲的縱向移動(dòng),供大家參考,具體內(nèi)容如下
我是跟著書(shū)里一步步寫(xiě)到橫向移動(dòng)后 我就想怎么縱向移動(dòng),放上自己寫(xiě)的代碼,如果有問(wèn)題的話,請(qǐng)指出來(lái),我也是剛剛學(xué)習(xí)python,希望可以跟大家多多交流。
新增的就是有關(guān)up和down的代碼了。
我自己是成功了,肯定有其他的更優(yōu)化的,那就等我在學(xué)習(xí)一段時(shí)間吧。
附上代碼:
game_function:
import sys import pygame # 監(jiān)視鍵盤(pán)和鼠標(biāo)事件 def check_keydown_events(event,ship): if event.key==pygame.K_RIGHT: ship.moving_right=True elif event.key==pygame.K_LEFT: ship.moving_left=True elif event.key==pygame.K_UP: ship.moving_up=True elif event.key==pygame.K_DOWN: ship.moving_down=True def check_keyup_events(event,ship): if event.key==pygame.K_RIGHT: ship.moving_right=False elif event.key==pygame.K_LEFT: ship.moving_left=False elif event.key==pygame.K_UP: ship.moving_up=False elif event.key==pygame.K_DOWN: ship.moving_down=False def check_events(ship): for event in pygame.event.get(): if event.type==pygame.QUIT: sys.exit() elif event.type==pygame.KEYDOWN: check_keydown_events(event,ship) elif event.type==pygame.KEYUP: check_keyup_events(event,ship) def update_screen(ai_settings,screen,ship): screen.fill(ai_settings.bg_color) ship.blitme() pygame.display.flip() #讓最近回執(zhí)的屏幕可見(jiàn)(刷新)
ship:
import pygame class Ship(): def __init__(self,ai_settings,screen): self.screen=screen self.ai_settings=ai_settings #加載飛船圖像 self.image=pygame.image.load('chengyan_ship.bmp') self.rect=self.image.get_rect() self.screen_rect=screen.get_rect() self.rect.centerx=self.screen_rect.centerx #x的坐標(biāo) self.rect.centery=self.screen_rect.centery #y的坐標(biāo) self.rect.bottom=self.screen_rect.bottom self.moving_right=False self.moving_left=False self.moving_up=False self.moving_down=False #得到飛船移動(dòng)到最下面的值(我不知道有沒(méi)有表述清楚...就是只能飛到界面的最下面) self.screen_top=self.rect.top def update(self): #橫向移動(dòng) if self.moving_right and self.rect.right<self.screen_rect.right: self.rect.centerx+=self.ai_settings.ship_speed_factor #縱向移動(dòng) if self.moving_left and self.rect.left>0: self.rect.centerx-=self.ai_settings.ship_speed_factor if self.moving_up and self.rect.top>0: self.rect.centery-=self.ai_settings.ship_speed_factor if self.moving_down and self.rect.top<self.screen_top: self.rect.centery+=self.ai_settings.ship_speed_factor self.rect.centerx=self.rect.centerx self.rect.centery=self.rect.centery def blitme(self): self.screen.blit(self.image,self.rect)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python實(shí)現(xiàn)飛機(jī)大戰(zhàn)游戲
- python pygame模塊編寫(xiě)飛機(jī)大戰(zhàn)
- python版飛機(jī)大戰(zhàn)代碼分享
- python實(shí)現(xiàn)飛機(jī)大戰(zhàn)
- python實(shí)現(xiàn)飛機(jī)大戰(zhàn)微信小游戲
- 人工智能最火編程語(yǔ)言 Python大戰(zhàn)Java!
- 星球大戰(zhàn)與Python之間的那些事
- python實(shí)現(xiàn)坦克大戰(zhàn)
- python實(shí)現(xiàn)貪吃蛇雙人大戰(zhàn)
- python實(shí)現(xiàn)簡(jiǎn)單坦克大戰(zhàn)
相關(guān)文章
python異常的傳遞知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家整理的是一篇關(guān)于python異常的傳遞知識(shí)點(diǎn)總結(jié),有興趣的朋友們可以學(xué)習(xí)下。2021-06-06Python中使用select模塊實(shí)現(xiàn)非阻塞的IO
這篇文章主要介紹了Python中使用select模塊實(shí)現(xiàn)非阻塞的IO,本文使用一個(gè)簡(jiǎn)單聊天室程序講解Python中的select模塊使用,需要的朋友可以參考下2015-02-02用Python編寫(xiě)一個(gè)簡(jiǎn)單的Lisp解釋器的教程
這篇文章主要介紹了用Python編寫(xiě)一個(gè)簡(jiǎn)單的Lisp解釋器的教程,Lisp是一種源碼簡(jiǎn)單的函數(shù)式編程語(yǔ)言,本文主要介紹對(duì)其中的一個(gè)子集Scheme的解釋器開(kāi)發(fā),需要的朋友可以參考下2015-04-04opencv+python實(shí)現(xiàn)圖像矯正
這篇文章主要為大家詳細(xì)介紹了opencv+python實(shí)現(xiàn)圖像矯正,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08哪種Python框架適合你?簡(jiǎn)單介紹幾種主流Python框架
這篇文章主要介紹了幾種主流的Python框架,幫助大家更好的理解和學(xué)習(xí)Python,感興趣的朋友可以了解下2020-08-08Python使用matplotlib實(shí)現(xiàn)基礎(chǔ)繪圖功能示例
這篇文章主要介紹了Python使用matplotlib實(shí)現(xiàn)基礎(chǔ)繪圖功能,結(jié)合實(shí)例形式分析了Python基于matplotlib實(shí)現(xiàn)正弦、余弦圖形及多軸圖的相關(guān)繪制操作技巧,需要的朋友可以參考下2018-07-07python使用正則表達(dá)式去除中文文本多余空格,保留英文之間空格方法詳解
這篇文章主要介紹了python使用正則表達(dá)式去除中文文本多余空格,保留英文之間空格方法詳解,需要的朋友可以參考下2020-02-02