python--从入门到实践--chapter 12 pygame_Alien_Invasion
生活随笔
收集整理的這篇文章主要介紹了
python--从入门到实践--chapter 12 pygame_Alien_Invasion
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
- 安裝pygame包,把安裝好的包c(diǎn)opy一份到pycharm工程目錄下,不然找不到pygame包
抄一遍書(shū)上的代碼:
settings.py
class Settings():def __init__(self):self.screen_width = 1200self.screen_height = 800self.bg_color = (255, 255, 255)self.ship_speed_factor = 1.5ship.py
import pygame class Ship():def __init__(self, ai_settings, screen):self.screen = screenself.ai_settings = ai_settingsself.image = pygame.image.load("images/ship.bmp")self.rect = self.image.get_rect() #圖像矩形self.screen_rect = screen.get_rect() #屏幕矩形,左上角原點(diǎn),右下角如1200,800self.rect.centerx = self.screen_rect.centerx #圖像x中心,在屏幕中心self.rect.bottom = self.screen_rect.bottom #圖像底邊,在屏幕底邊self.center = float(self.rect.centerx) #圖像x中心坐標(biāo)轉(zhuǎn)成浮點(diǎn)型,存在center中self.moving_right = Falseself.moving_left = Falsedef update(self):if self.moving_right and self.rect.right < self.screen_rect.right: #限制位置在屏幕內(nèi)self.center += self.ai_settings.ship_speed_factorif self.moving_left and self.rect.left > 0:self.center -= self.ai_settings.ship_speed_factorself.rect.centerx = self.centerdef blitme(self):self.screen.blit(self.image, self.rect) #blit實(shí)現(xiàn)動(dòng)畫(huà)效果game_functions.py
import sys import pygame def check_keydown_events(event, ship): #檢測(cè)鍵盤(pán)按下事件if event.key == pygame.K_RIGHT:ship.moving_right = Trueelif event.key == pygame.K_LEFT:ship.moving_left = Truedef check_keyup_events(event, ship): #檢測(cè)鍵盤(pán)松開(kāi)事件if event.key == pygame.K_RIGHT:ship.moving_right = Falseelif event.key == pygame.K_LEFT:ship.moving_left = Falsedef check_events(ship):for event in pygame.event.get():if event.type == pygame.QUIT: #退出事件sys.exit()elif event.type == pygame.KEYDOWN: #鍵盤(pán)按下事件check_keydown_events(event, ship)elif event.type == pygame.KEYUP: #鍵盤(pán)松開(kāi)事件check_keyup_events(event, ship)def update_screen(ai_settings, screen, ship):screen.fill(ai_settings.bg_color) #屏幕填充背景色ship.blitme() #ship的動(dòng)畫(huà)效果pygame.display.flip() #讓動(dòng)畫(huà)盡可能流暢alien_invasion.py
import sys import pygame from settings import Settings from ship import Ship import game_functions as gf def run_game():pygame.init()ai_settings = Settings() #創(chuàng)建游戲設(shè)置screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))pygame.display.set_caption("Alien Invasion") #設(shè)置標(biāo)題ship = Ship(ai_settings, screen) #創(chuàng)建游戲?qū)ο?/span>while True:gf.check_events(ship) #檢測(cè)事件ship.update() #更新ship位置gf.update_screen(ai_settings, screen, ship) #刷新屏幕run_game()
練習(xí)題:
1.火箭上下左右移動(dòng)
2.創(chuàng)建一個(gè)程序,顯示一個(gè)空屏幕。在事件循環(huán)中,每當(dāng)檢測(cè)到 pygame.KEYDOWN 事件時(shí)都打印屬性event.key。運(yùn)行這個(gè)程序,并按各種鍵,看看Pygame如何響應(yīng)
添加子彈模塊后的代碼
settings.py
ship.py 不變
bullet.py
game_functions.py
import sys import pygame from bullet import Bullet def fire_bullet(ai_settings, screen, ship, bullets): #發(fā)射子彈if len(bullets) < ai_settings.bullets_allowed: #子彈沒(méi)超數(shù)量new_bullet = Bullet(ai_settings, screen, ship) #生成新子彈bullets.add(new_bullet) #新子彈加入組 def check_keydown_events(event,ai_settings, screen, ship, bullets): #檢測(cè)鍵盤(pán)按下事件if event.key == pygame.K_RIGHT:ship.moving_right = Trueelif event.key == pygame.K_LEFT:ship.moving_left = Trueelif event.key == pygame.K_SPACE: #按下空格鍵,發(fā)射子彈fire_bullet(ai_settings, screen, ship, bullets)def check_keyup_events(event, ship): #檢測(cè)鍵盤(pán)松開(kāi)事件if event.key == pygame.K_RIGHT:ship.moving_right = Falseelif event.key == pygame.K_LEFT:ship.moving_left = Falsedef check_events(ai_settings, screen, ship, bullets):for event in pygame.event.get():if event.type == pygame.QUIT: #退出事件sys.exit()elif event.type == pygame.KEYDOWN: #鍵盤(pán)按下事件check_keydown_events(event, ai_settings, screen, ship, bullets)elif event.type == pygame.KEYUP: #鍵盤(pán)松開(kāi)事件check_keyup_events(event, ship)def update_screen(ai_settings, screen, ship, bullets):screen.fill(ai_settings.bg_color) #屏幕填充背景色for bullet in bullets.sprites(): #把每個(gè)子彈畫(huà)在屏幕上bullet.draw_bullet()ship.blitme() #ship的動(dòng)畫(huà)效果pygame.display.flip() #刷新,讓動(dòng)畫(huà)盡可能流暢def update_bullets(bullets):bullets.update() #更新子彈位置for bullet in bullets.copy(): #刪除飛出屏幕的子彈if bullet.rect.bottom <= 0:bullets.remove(bullet)alien_invasion.py
import sys import pygame from settings import Settings from ship import Ship import game_functions as gf from pygame.sprite import Group def run_game():pygame.init()ai_settings = Settings() #創(chuàng)建游戲設(shè)置screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))pygame.display.set_caption("Alien Invasion") #設(shè)置標(biāo)題ship = Ship(ai_settings, screen) #創(chuàng)建游戲飛船對(duì)象bullets = Group() #創(chuàng)建子彈組while True:gf.check_events(ai_settings, screen, ship, bullets) #檢測(cè)事件ship.update() #更新ship位置gf.update_bullets(bullets) #更新子彈位置,刪除飛出去的子彈# print(len(bullets)) #調(diào)試時(shí),確認(rèn)子彈飛出屏幕后被刪除gf.update_screen(ai_settings, screen, ship, bullets) #刷新屏幕run_game()
作業(yè)3:更改上述代碼,使之橫向發(fā)射子彈
添加 pygame.transform.rotozoom(surface, angle, scale) (縮放和旋轉(zhuǎn))
代碼連接 https://github.com/hitskyer/course/tree/master/python/chenmingming/pygame_project/alien_invasion
總結(jié)
以上是生活随笔為你收集整理的python--从入门到实践--chapter 12 pygame_Alien_Invasion的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python 数据平滑_数据平滑方法的原
- 下一篇: websocket python爬虫_p