資源簡介
信你大家在學(xué)習(xí)編程語言的過程中經(jīng)常會碰到一個問題,那就是學(xué)完了基本的語法以后不知道自己要做什么。其實,先找一些簡單的項目進(jìn)行練手是非常好的一種學(xué)習(xí)編程的方式。前段時間學(xué)習(xí)了python的基本語法,這里給大家推薦一本適合新手學(xué)習(xí)的python教程,叫《python編程:從入門到實踐》。這里給大家分享其中的一段外星人入侵的代碼(這里貼出13.1到14.1的代碼,后面會補(bǔ)充完整給大家)。
?

代碼片段和文件信息
import?pygame
from?pygame.sprite?import?Sprite
class?Alien(Sprite):
????“““表示單個外星人的類“““
????def?__init__(selfai_settingsscreen):
????????“““初始化外星人并設(shè)置其起始位置“““
????????super().__init__()
????????self.screen=screen
????????self.ai_settings=ai_settings
????????#加載外星人的圖像,并設(shè)置其rect屬性
????????self.image=pygame.image.load(‘images/alien.bmp‘)
????????self.rect=self.image.get_rect()
????????#每個外星人最初都在屏幕左上角附近
????????self.rect.x=self.rect.width
????????self.rect.y=self.rect.height
????????#存儲外星人的準(zhǔn)確位置
????????self.x=float(self.rect.x)
????def?check_edges(self):
????????“““如果外星人位于屏幕邊緣,就返回True“““
????????screen_rect=self.screen.get_rect()
????????if?self.rect.right>screen_rect.right:
????????????return?True
????????elif?self.rect.left<=0:
????????????return?True
????def?update(self):
????????“““向左或者向右移動外星人“““
????????self.x+=(self.ai_settings.alien_speed_factor*
?????????????????self.ai_settings.fleet_direction)
????????self.rect.x=self.x
????????
????def?blitme(self):
????????“““在指定位置繪制外星人“““
????????self.screen.blit(self.imageself.rect)
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2018-09-07?16:59??__pycache__\
?????文件????????1471??2018-08-30?14:15??__pycache__\alien.cpython-37.pyc
?????文件????????1244??2018-08-27?13:49??__pycache__\bullet.cpython-37.pyc
?????文件????????1233??2018-08-30?16:52??__pycache__\button.cpython-37.pyc
?????文件????????5755??2018-09-07?16:59??__pycache__\game_functions.cpython-37.pyc
?????文件?????????749??2018-09-03?17:04??__pycache__\game_stats.cpython-37.pyc
?????文件?????????859??2018-08-30?16:05??__pycache__\settings.cpython-37.pyc
?????文件????????1258??2018-08-30?16:05??__pycache__\ship.cpython-37.pyc
?????文件????????1292??2018-08-30?14:02??alien.py
?????文件????????1548??2018-08-31?14:17??alien_invasion.py
?????文件????????1134??2018-08-27?13:49??bullet.py
?????文件????????1197??2018-08-30?16:52??button.py
?????文件????????7525??2018-09-07?16:59??game_functions.py
?????文件?????????452??2018-09-03?17:03??game_stats.py
?????目錄???????????0??2018-08-28?16:36??images\
?????文件????????2144??2018-08-28?16:36??images\alien.bmp
?????文件????????6048??2018-08-28?16:35??images\alien1.bmp
?????文件????????4222??2018-08-27?10:42??images\ship.bmp
?????文件???????23467??2018-08-20?12:59??images\ship_sourceimg.bmp
?????文件?????4385142??2018-08-10?16:43??pygame-1.9.4-cp37-cp37m-win_amd64.whl
?????文件?????????651??2018-09-13?12:57??settings.py
?????文件????????1409??2018-08-30?16:04??ship.py
- 上一篇:基于python的小游戲 含源代碼
- 下一篇:python之外星人入侵完整版程序源碼
評論
共有 條評論