python實(shí)現(xiàn)飛船游戲的縱向移動(dòng)
本文實(shí)例為大家分享了python實(shí)現(xiàn)飛船游戲的縱向移動(dòng),供大家參考,具體內(nèi)容如下
我是跟著書里一步步寫到橫向移動(dòng)后 我就想怎么縱向移動(dòng),放上自己寫的代碼,如果有問(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)視鍵盤和鼠標(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í)的屏幕可見(刷新)
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模塊編寫飛機(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使用BeautifulSoup進(jìn)行XPath和CSS選擇器定位
在 Python 中,BeautifulSoup 是一個(gè)常用的 HTML 和 XML 解析庫(kù),它允許我們輕松地定位和提取網(wǎng)頁(yè)中的特定元素,本文將詳細(xì)介紹如何在 BeautifulSoup 中使用 XPath 和 CSS 選擇器定位 HTML 元素,并提供示例代碼以幫助新手理解這些概念,需要的朋友可以參考下2024-11-11
Python比較文件夾比另一同名文件夾多出的文件并復(fù)制出來(lái)的方法
這篇文章主要介紹了Python比較文件夾比另一同名文件夾多出的文件并復(fù)制出來(lái)的方法,涉及Python針對(duì)文件與文件夾的操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03
python中opencv支持向量機(jī)的實(shí)現(xiàn)
本文主要介紹了python中opencv支持向量機(jī)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
使用matplotlib繪制熱圖(heatmap)全過(guò)程
這篇文章主要介紹了使用matplotlib繪制熱圖(heatmap)全過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
淺析python中while循環(huán)和for循環(huán)
在本篇文章里小編給各位整理的是關(guān)于python中while和for循環(huán)知識(shí)點(diǎn)詳解,有興趣的朋友們可以學(xué)習(xí)下。2019-11-11
Python 用turtle實(shí)現(xiàn)用正方形畫圓的例子
今天小編就為大家分享一篇Python 用turtle實(shí)現(xiàn)用正方形畫圓的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11
Django項(xiàng)目如何獲得SSL證書與配置HTTPS
本文總結(jié)了如何獲得SSL證書并給Django項(xiàng)目配置HTTPS,建議先收藏再閱讀,將來(lái)有一天你很可能會(huì)用到它。2021-04-04

