self.moving_left =
false..
.if self.moving_left:
self.rect.centerx -=
1
更新:\game_functions.py:
def
check_events
(chicken)
:"""響應按鍵和滑鼠事件"""
for event in pygame.event.get():
if event.
type
== pygame.quit:
sys.exit(
)elif event.
type
== pygame.keydown:
if event.key == pygame.k_right:
chicken.moving_right =
true
elif event.key == pygame.k_left:
chicken.moving_left =
true
elif event.
type
== pygame.keyup:
if event.key == pygame.k_right:
chicken.moving_right =
false
elif event.key == pygame.k_left:
chicken.moving_left =
false
二、調整速度
實現方法:新增speed屬性
如果新增在chicken類中,意味著這是肥雞的屬性,而在遊戲環境中不同肥雞應該具有相同速度。為了協調,將其直接新增在settings類中。
增添:\settings.py - class settings - def _ init _ (self):
# 肥雞設定
self.chicken_speed_factor =
1.5
更新:\chicken.py:
...
class
chicken()
:def
__init__
(self, screen, fcw_settings)
:"""肥雞初始化"""
self.screen = screen
self.fcw_settings = fcw_settings
# 載入肥雞影象並獲取其外接矩形
self.image = pygame.image.load(
'images/qqchicken.png'
) self.rect = self.image.get_rect(
) self.screen_rect = screen.get_rect(
)# 將肥雞放在螢幕底部**
self.rect.centerx = self.screen_rect.centerx
self.rect.bottom = self.screen_rect.bottom
# 轉化小數值
self.center =
float
(self.rect.centerx)
# 移動標誌
self.moving_right =
false
self.moving_left =
false
defupdate
(self)
:"""根據移動標誌調整肥雞位置"""
if self.moving_right:
self.center += self.fcw_settings.chicken_speed_factor
if self.moving_left:
self.center -= self.fcw_settings.chicken_speed_factor
# 根據center更新rect
self.rect.centerx = self.center..
.
更新:\fatchickenwars.py - def run_game():
...
chicken = chicken(screen, fcw_settings)..
.
三、限制活動範圍
防止肥雞跑出螢幕。
更新:\chicken.py - def update(self):
...
if self.moving_right and self.rect.right < self.screen_rect.right:
self.center += self.fcw_settings.chicken_speed_factor
if self.moving_left and self.rect.left >0:
self.center -= self.fcw_settings.chicken_speed_factor..
.
2021.1.21 Python肥雞大戰開發記錄(二) 設定類和肥雞類
一 建立設定 settings 類 新建 settings.py class settings 儲存所有的設定 def init self 初始化遊戲設定 螢幕設定 self.screen width 1200 self.screen height 800 self.bg color 230 230...
Python肥雞大戰開發記錄(三) 初步重構
建立乙個名為game functions的模組,儲存遊戲執行相關函式。新建 game functions.py 1.使用者輸入監測 check events import sys import pygame defcheck events 響應按鍵和滑鼠事件 for event in pygame....
python飛機大戰小遊戲開發02
編寫設定類 在寫完遊戲的視窗 後,我們要將背景色改變,背景色是由pygame的screen.fill方法構成的,於是我們對run函式進行修改,增加了self.screen.fill 230,230,230 將螢幕變成灰色 def run game self while true for event ...