外星人入侵游戏--python实战 (附完整代码)
生活随笔
收集整理的這篇文章主要介紹了
外星人入侵游戏--python实战 (附完整代码)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
https://blog.csdn.net/hanhanwanghaha寶藏女孩 歡迎您的關(guān)注!
歡迎關(guān)注微信公眾號:寶藏女孩的成長日記
如有轉(zhuǎn)載,請注明出處(如不注明,盜者必究)
目錄
- 一、前提準(zhǔn)備
- 二、效果展示
- 三、代碼
- alien_invasion.py
- settings.py
- game_functions.py
- ship.py
- bullet.py
- alien.py
- game_stats.py
- scoreboard.py
- button.py
- 四、圖片領(lǐng)取位置
一、前提準(zhǔn)備
就大概是這樣
https://blog.csdn.net/qq_43597899/article/details/88619736
注:也可直接在Anaconda終端輸入以下命令就可安裝好
pip install -I https://pypi.douban.com/simple pygame
二、效果展示
左上角為玩家所有的存活的飛船數(shù)量,正中央的0表示最高分,右上角的0表示得分,1表示當(dāng)前等級為1,點(diǎn)擊Play游戲開始,隨著關(guān)卡的提升,難度也會加強(qiáng)。
三、代碼
alien_invasion.py
# coding=utf-8 """使用pygame來編寫游戲的基本結(jié)構(gòu)"""# 玩家退出游戲時(shí),我們使用sys模塊來退出 # import sys 在此文件中不需要導(dǎo)入sys,因?yàn)樵趃ame_functions使用了sys import pygame from settings import Settings from ship import Ship import game_functions as gf from pygame.sprite import Group from game_stats import GameStats from button import Button from scoreboard import Scoreboarddef run_game():"""初始化游戲并創(chuàng)建一個(gè)屏幕對象"""pygame.init() # 初始化背景設(shè)置ai_settings = Settings()screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))pygame.display.set_caption('Alien Invasion')# 創(chuàng)建play按鈕play_button = Button(ai_settings, screen, "Play")# 創(chuàng)建一個(gè)用于存儲游戲統(tǒng)計(jì)信息的實(shí)例,并創(chuàng)建計(jì)分牌stats = GameStats(ai_settings)sb = Scoreboard(ai_settings, screen, stats)# 創(chuàng)建一艘飛船:注意,必須在while循環(huán)前面創(chuàng)建,以免每次循環(huán)都創(chuàng)建一艘飛船ship = Ship(ai_settings, screen)# 創(chuàng)建一個(gè)用于存儲子彈的編組bullets = Group()# 創(chuàng)建一個(gè)空編組用于存儲所有外星人aliens = Group()# 設(shè)置背景色bg_color = (230, 230, 230)# 創(chuàng)建外星人群gf.create_fleet(ai_settings, screen, ship, aliens)# 開始游戲的主循環(huán)while True:gf.check_events(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets, )if stats.game_active:ship.update()gf.update_bullets(ai_settings, screen, stats, sb, ship, aliens, bullets)gf.update_aliens(ai_settings, screen, stats, sb, ship, aliens, bullets)gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, play_button)run_game()settings.py
# coding=utf-8"""將所有的設(shè)置儲存在同一個(gè)地方"""class Settings():# 儲存外星人入侵所有設(shè)置的類def __init__(self):"""初始化游戲的設(shè)置"""# 外星人設(shè)置# 添加一個(gè)控制外星人速度的設(shè)置self.alien_speed_factor = 1self.fleet_drop_speed = 10# fleet_direction 為1表示向右移,為-1表示向左移self.fleet_direction = 1# 添加子彈設(shè)置self.bullet_speed_factor = 3self.bullet_width = 3self.bullet_height = 15self.bullet_color = 60, 60, 60# 限制子彈數(shù)量6個(gè)self.bullets_allowed = 6# 屏幕設(shè)置self.screen_width = 1200self.screen_height = 800self.bg_color = (230, 230, 230)# 飛船設(shè)置# 調(diào)整飛船的速度,將初始值1設(shè)置為1.5self.ship_speed_factor = 1.5self.ship_limit = 3# 以1.1倍速加快游戲節(jié)奏self.speedup_scale = 1.1# 提高外星人點(diǎn)數(shù)的速度self.score_scale = 1.5# 初始化隨游戲進(jìn)行而變化的屬性self.initialize_dynamic_settings()def initialize_dynamic_settings(self):"""初始化隨游戲進(jìn)行而變化的設(shè)置"""self.ship_speed_factor = 1.5self.bullet_speed_factor = 3self.alien_speed_factor = 1# fleet_direction為1表示向右,fleet_direction為-1表示向左self.fleet_direction = 1# 每擊落一個(gè)外星人將得到50個(gè)點(diǎn)self.alien_points = 50def increase_speed(self):"""提高速度和外星人點(diǎn)數(shù)設(shè)置"""self.ship_speed_factor *= self.speedup_scaleself.bullet_speed_factor *= self.speedup_scaleself.alien_speed_factor *= self.speedup_scaleself.alien_points = int(self.alien_points * self.score_scale)game_functions.py
# coding=utf-8 import sys from time import sleep import pygame from bullet import Bullet from alien import Alien"""重構(gòu):模塊game_functions將管理事件的代碼移到check_events()函數(shù)當(dāng)中再將check_events()放在一個(gè)名為game_function的模塊中 """def check_keydown_events(event, ai_settings, screen, ship, bullets):"""響應(yīng)按鍵"""if event.key == pygame.K_RIGHT:# 按下右鍵,向右移動飛船ship.moving_right = Trueelif event.key == pygame.K_LEFT:# 按下左鍵,向左移動飛船ship.moving_left = Trueelif event.key == pygame.K_SPACE:fire_bullet(ai_settings, screen, ship, bullets)def fire_bullet(ai_settings, screen, ship, bullets):# 按下空格鍵,創(chuàng)建一顆新子彈,并將其加入到bullets中if len(bullets) < ai_settings.bullets_allowed:new_bullets = Bullet(ai_settings, screen, ship)bullets.add(new_bullets)def check_keyup_events(event, ship):"""響應(yīng)松開"""if event.key == pygame.K_RIGHT:ship.moving_right = Falseelif event.key == pygame.K_LEFT:ship.moving_left = Falsedef check_events(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets):"""響應(yīng)按鍵和鼠標(biāo)事件"""for event in pygame.event.get():if event.type == pygame.QUIT:sys.exit()# 響應(yīng)按鍵elif event.type == pygame.KEYDOWN:check_keydown_events(event, ai_settings, screen, ship, bullets)elif event.type == pygame.KEYUP:check_keyup_events(event, ship)elif event.type == pygame.MOUSEBUTTONDOWN:mouse_x, mouse_y = pygame.mouse.get_pos()check_play_button(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets, mouse_x, mouse_y)def check_play_button(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets, mouse_x, mouse_y):"""在玩家點(diǎn)擊play按鈕時(shí)才開始新游戲"""button_clicked = play_button.rect.collidepoint(mouse_x, mouse_y)if button_clicked and not stats.game_active:# 重置游戲設(shè)置,需要將發(fā)生變化了的設(shè)置重置為初始值,否則新游戲開始時(shí),速度設(shè)置將是前一次游戲增加了的值ai_settings.initialize_dynamic_settings()# 隱藏光標(biāo)pygame.mouse.set_visible(False)# 重置游戲統(tǒng)計(jì)信息stats.reset_stats()stats.game_active = True# 重置記分牌圖像sb.prep_score()sb.prep_high_score()sb.prep_level()sb.prep_ships()# 清空外星人和子彈列表aliens.empty()bullets.empty()# 創(chuàng)建一群新的外星人,并讓飛船居中create_fleet(ai_settings, screen, ship, aliens)ship.center_ship()def update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, play_button):"""更新屏幕上的圖像,并切換到新屏幕,每次循環(huán)都要重繪屏幕"""# 每次循環(huán)都重新繪屏幕,用背景色填充屏幕,這個(gè)方法只接受一個(gè)實(shí)參,一種顏色screen.fill(ai_settings.bg_color)# 在飛船和外星人后面重新繪制所有子彈for bullet in bullets.sprites():bullet.draw_bullet()# 填充背景后,調(diào)用ship.blitem()將飛船繪制到屏幕上,確保它出現(xiàn)在背景前面ship.blitem()# 在屏幕上繪制編組中的每個(gè)外星人aliens.draw(screen)# 顯示得分sb.show_score()# 如果游戲處于非活動狀態(tài),就繪制play按鈕if not stats.game_active:play_button.draw_button()# 讓最近繪制的屏幕可見pygame.display.flip()def update_bullets(ai_settings, screen, stats, sb, ship, aliens, bullets):"""更新子彈位置,并且刪除已消失的子彈"""# 更新子彈的位置bullets.update()# 刪除已經(jīng)消失的子彈(不刪除的話會占用內(nèi)存)for bullet in bullets.copy():if bullet.rect.bottom <= 0:bullets.remove(bullet)check_bullet_alien_conllsions(ai_settings, screen, stats, sb, ship, aliens, bullets)def check_bullet_alien_conllsions(ai_settings, screen, stats, sb, ship, aliens, bullets):"""響應(yīng)子彈和外星人的碰撞"""# 刪除發(fā)生碰撞的子彈和外星人collisions = pygame.sprite.groupcollide(bullets, aliens, True, True)if collisions:for aliens in collisions.values():"""將消滅的每個(gè)外星人的點(diǎn)數(shù)都計(jì)入得分"""stats.score += ai_settings.alien_points * len(aliens)sb.prep_score()check_high_score(stats, sb)if len(aliens) == 0:# 刪除現(xiàn)有子彈并且新建一群外星人,加快游戲節(jié)奏bullets.empty()ai_settings.increase_speed()# 外星人都被消滅,就提高一個(gè)等級stats.level += 1sb.prep_level()create_fleet(ai_settings, screen, ship, aliens)def get_number_aliens_x(ai_settings, alien_width):"""計(jì)算每行可容納多少個(gè)外星人"""# 放置外星人的水平空間為屏幕寬度減去外星人寬度的兩倍(需要在屏幕兩邊留下一定的間距)available_space_x = ai_settings.screen_width - 2 * alien_width# 確定一行可容納多少個(gè)外星人,用空間除以外星人寬度的兩倍(為什么是兩倍?因?yàn)橥庑侨俗陨淼膶挾冗€要算兩個(gè)外星人之間的間距,間距也算一個(gè)外星人)number_alien_x = int(available_space_x / (2 * alien_width))return number_alien_xdef get_number_rows(ai_settings, ship_height, alien_hight):"""計(jì)算屏幕可容納多少外星人"""# 可用垂直空間=屏幕高度-第一行外星人的高度,最初外星人高度,外星人間距,飛船高度available_space_y = (ai_settings.screen_height - (3 * alien_hight) - ship_height)# 可容納的函數(shù)number_rows = int(available_space_y / (2 * alien_hight))return number_rowsdef create_alien(ai_settings, screen, aliens, alien_number, row_number):"""創(chuàng)建一個(gè)外星人,并將其放入當(dāng)前行"""# 創(chuàng)建一個(gè)外星人,并計(jì)算一行可容納多少個(gè)外星人# 外星人間距為外星人寬度alien = Alien(ai_settings, screen)alien_width = alien.rect.widthalien.x = alien_width + 2 * alien_width * alien_numberalien.rect.x = alien.xalien.rect.y = alien.rect.height + 2 * alien.rect.height * row_numberaliens.add(alien)def create_fleet(ai_settings, screen, ship, aliens):"""創(chuàng)建外星人群"""# 創(chuàng)建一個(gè)外星人,并計(jì)算一行可容納多少個(gè)外星人# 外星人間距為外星人寬度alien = Alien(ai_settings, screen)number_aliens_x = get_number_aliens_x(ai_settings, alien.rect.width)number_rows = get_number_rows(ai_settings, ship.rect.height, alien.rect.height)# 創(chuàng)建外星人群for row_number in range(number_rows):for alien_number in range(number_aliens_x):# 創(chuàng)建一個(gè)外星人并且添加到當(dāng)前行create_alien(ai_settings, screen, aliens, alien_number, row_number)def check_fleet_edges(ai_settings, aliens):"""確定是否有外星人位于屏幕邊緣,有外星人到達(dá)邊緣時(shí)采取相應(yīng)措施"""for alien in aliens.sprites():if alien.check_edges():change_fleet_direction(ai_settings, aliens)breakdef change_fleet_direction(ai_settings, aliens):"""將整群外星人下移,并改變它們的方向"""for alien in aliens.sprites():alien.rect.y += ai_settings.fleet_drop_speedai_settings.fleet_direction *= -1def ship_hit(ai_settings, screen, stats, sb, ship, aliens, bullets):"""響應(yīng)被外星人撞到的飛船"""if stats.ship_left > 0:# 將ship_left-1stats.ship_left -= 1# 更新計(jì)分牌sb.prep_ships()else:stats.game_active = Falsepygame.mouse.set_visible(True)# 清空外星人列表和子彈列表aliens.empty()bullets.empty()# 創(chuàng)建一群新的外星人,并將飛船放到屏幕底端中央create_fleet(ai_settings, screen, ship, aliens)ship.center_ship()# 暫停sleep(0.5)def check_aliens_bottom(ai_settings, screen, stats, sb, ship, aliens, bullets):"""檢查是否有外星人到達(dá)了屏幕底端"""screen_rect = screen.get_rect()for alien in aliens.sprites():if alien.rect.bottom >= screen_rect.bottom:"""像飛船被撞到一樣處理"""ship_hit(ai_settings, screen, stats, sb, ship, aliens, bullets)breakdef update_aliens(ai_settings, screen, stats, sb, ship, aliens, bullets):"""檢查是否有外星人位于屏幕邊緣,并更新外星人群中所有外星人的位置"""check_fleet_edges(ai_settings, aliens)aliens.update()# 檢查外星人和飛船之間的碰撞if pygame.sprite.spritecollideany(ship, aliens):ship_hit(ai_settings, screen, stats, sb, ship, aliens, bullets)check_aliens_bottom(ai_settings, screen, stats, sb, ship, aliens, bullets)"""檢查是否有外星人到達(dá)屏幕底端"""def check_high_score(stats, sb):"""檢查是否誕生了最高得分"""if stats.score > stats.high_score:stats.high_score = stats.scoresb.prep_high_score()ship.py
# coding=utf-8 import pygame from pygame.sprite import Spriteclass Ship(Sprite):"""控制游戲外觀和飛船速度的屬性"""def __init__(self, ai_settings, screen):"""初始化飛船并設(shè)置初始位置"""super(Ship, self).__init__()self.screen = screenself.ai_settings = ai_settings# 加載飛船圖像并獲取其外接矩形self.image = pygame.image.load('images/ship.bmp')# get_rect獲取相應(yīng)的surface屬性rectself.rect = self.image.get_rect()"""我們要將飛船放在屏幕底部中央,為此,首先將表示屏幕的矩形存儲在self.screen_rect中"""self.screen_rect = screen.get_rect()# 將每艘新飛船放在屏幕底部中央self.rect.centerx = self.screen_rect.centerxself.rect.bottom = self.screen_rect.bottom# 在飛船屬性center中存儲小數(shù)值,這里是因?yàn)槲覀冊趕ettings中將初始值1設(shè)置為1.5self.center = float(self.rect.centerx)""" 允許不斷移動結(jié)合使用KEYDOWN和KEYUP事件,及一個(gè)名為moving_right的標(biāo)志來實(shí)現(xiàn)持續(xù)移動飛船不動時(shí),moving_right為False,玩家按下右箭頭鍵時(shí),將這個(gè)標(biāo)志設(shè)置為True,玩家松開時(shí),將這個(gè)標(biāo)志設(shè)置為False------見game_function line16-30 (左同)用update方法來檢查標(biāo)志moving_right的狀態(tài),如果這個(gè)標(biāo)志為True,就調(diào)用飛船的位置"""# 移動標(biāo)志self.moving_right = Falseself.moving_left = Falsedef update(self):"""根據(jù)移動標(biāo)志調(diào)整飛船的位置,這里用兩個(gè)if,而沒有用elif,是因?yàn)槿绻婕彝瑫r(shí)按下左右鍵,將增大center,再減少center的值,即飛船的位置保持不變但如果用elif,右箭頭始終保持優(yōu)先地位,當(dāng)左移動切換到右移動時(shí),玩家可能同時(shí)按住左右箭頭鍵,這種情況下,前面的做法更加準(zhǔn)確"""# if限制飛船的活動范圍(因?yàn)樵谶\(yùn)行游戲的時(shí)候,飛船可能飛出屏幕外)if self.moving_right and self.rect.right < self.screen_rect.right:self.center += self.ai_settings.ship_speed_factorif self.moving_left and self.rect.left > 0:self.center -= self.ai_settings.ship_speed_factor# 根據(jù)self.center更新rect對象self.rect.centerx = self.centerdef blitem(self):"""在指定位置繪制飛船"""self.screen.blit(self.image, self.rect)def center_ship(self):"""讓飛船在屏幕上居中"""self.center = self.screen_rect.centerxbullet.py
# coding=utf-8 import pygamefrom pygame.sprite import Spriteclass Bullet(Sprite):"""一個(gè)對飛船發(fā)射的子彈進(jìn)行管理的類"""def __init__(self, ai_settings, screen, ship):"""在飛船所處的位置創(chuàng)建一個(gè)子彈對象"""super(Bullet, self).__init__()self.screen = screen# 在(0,0)處創(chuàng)建一個(gè)表示子彈的矩形,再設(shè)置正確的位置self.rect = pygame.Rect(0, 0, ai_settings.bullet_width, ai_settings.bullet_height)self.rect.centerx = ship.rect.centerxself.rect.top = ship.rect.top# 儲存用小數(shù)表示的子彈位置self.y = float(self.rect.y)self.color = ai_settings.bullet_colorself.speed_factor = ai_settings.bullet_speed_factordef update(self):"""向上移動子彈"""# 更新表示子彈位置的小數(shù)值(子彈向上移動,意味著y坐標(biāo)將不斷減小)self.y -= self.speed_factor# 更新表示子彈的rect的位置self.rect.y = self.ydef draw_bullet(self):"""在屏幕上繪制子彈"""pygame.draw.rect(self.screen, self.color, self.rect)alien.py
# coding=utf-8 import pygame from pygame.sprite import Spriteclass Alien(Sprite):"""表示單個(gè)外星人的類"""def __init__(self, ai_settings, screen):"""初始化外星人并設(shè)置起始位置"""super(Alien, self).__init__()self.screen = screenself.ai_settings = ai_settings# 加載外星人圖像,并設(shè)置rect屬性self.image = pygame.image.load('images/alien.bmp')self.rect = self.image.get_rect()# 每個(gè)外星人最初都在屏幕左上角附近(左邊距設(shè)置為外星人的寬度,上邊距設(shè)置為外星人的高度)self.rect.x = self.rect.widthself.rect.y = self.rect.height# 存儲外星人的準(zhǔn)確位置self.x = float(self.rect.x)def blitem(self):"""在指定位置繪制外星人"""self.screen.blit(self.image, self.rect)def check_edges(self):"""如果外星人位于屏幕邊緣就返回True"""screen_rect = self.screen.get_rect()if self.rect.right >= screen_rect.right:return Trueelif self.rect.left <= 0:return Truedef update(self):"""向右或向右移動外星人"""# fleet_direction為1,就將外星人當(dāng)前的x坐標(biāo)增大alien_speed_factor,從而外星人向右移,如果fleet_direction為-1,就將外星人當(dāng)前的x坐標(biāo)減去alien_speed_factorself.x += (self.ai_settings.alien_speed_factor *self.ai_settings.fleet_direction)# 使用self.x的值來更新rect的位置self.rect.x = self.xgame_stats.py
# coding=utf-8class GameStats():"""跟蹤游戲統(tǒng)計(jì)信息"""def __init__(self, ai_settings):"""初始化統(tǒng)計(jì)信息"""self.ai_settings = ai_settingsself.reset_stats()# 讓游戲一開始處于非活動狀態(tài)self.game_active = False# 任何情況下都不應(yīng)重置最高得分self.high_score = 0# 顯示等級self.level = 1def reset_stats(self):"""初始化在游戲運(yùn)行期間可能變化的統(tǒng)計(jì)信息"""self.ship_left = self.ai_settings.ship_limit# 為每次在開始游戲時(shí)都重置得分,我們在reset_stats()而不是__init__中初始化scoreself.score = 0scoreboard.py
# coding=utf-8import pygame.font from pygame.sprite import Group from ship import Shipclass Scoreboard():"""顯示得分信息的類"""def __init__(self, ai_settings, screen, stats):"""初始化顯示得分涉及的屬性"""self.screen = screenself.screen_rect = screen.get_rect()self.ai_settings = ai_settingsself.stats = stats# 顯示得分信息時(shí)使用的字體設(shè)置self.text_color = (30, 30, 30)self.font = pygame.font.SysFont(None, 48)# 準(zhǔn)備好初始得分圖像self.prep_score()# 用于包含最高得分的圖像self.prep_high_score()# 在當(dāng)前得分下顯示等級self.prep_level()self.prep_ships()def prep_score(self):"""將得分轉(zhuǎn)換為一幅渲染的圖像"""# 讓python將stats.score的值圓整到最近的10的整數(shù)倍rounded_score = int(round(self.stats.score, -1))# 讓python將數(shù)值轉(zhuǎn)換為字符串時(shí)在其中插入逗號score_str = "{:,}".format(rounded_score)# 再將這個(gè)字符串傳遞給創(chuàng)建圖像的render(),為了清晰地顯示得分,我們向render傳遞了屏幕背景色,以及文本顏色self.score_image = self.font.render(score_str, True, self.text_color, self.ai_settings.bg_color)# 將得分放于屏幕右上角self.score_rect = self.score_image.get_rect()self.score_rect.right = self.screen_rect.right - 20self.score_rect.top = 20def show_score(self):"""在屏幕上顯示飛船和得分"""self.screen.blit(self.score_image, self.score_rect)self.screen.blit(self.high_score_image, self.high_score_rect)self.screen.blit(self.level_image, self.level_rect)#繪制飛船self.ships.draw(self.screen)def prep_high_score(self):"""將最高得分轉(zhuǎn)換為渲染的圖像"""# 最高分圓整到10的整數(shù)倍high_score = int(round(self.stats.high_score, -1))# 添加用逗號表示的千分位分隔符high_score_str = "{:,}".format(high_score)# 根據(jù)最高分生成一幅圖像self.high_score_image = self.font.render(high_score_str, True, self.text_color, self.ai_settings.bg_color)# 將最高得分放在屏幕頂端中央self.high_score_rect = self.high_score_image.get_rect()self.high_score_rect.centerx = self.screen_rect.centerxself.high_score_rect.top = self.screen_rect.topdef prep_level(self):"""將等級轉(zhuǎn)換為渲染的圖像"""self.level_image = self.font.render(str(self.stats.level), True, self.text_color, self.ai_settings.bg_color)# 將等級放在得分下方self.level_rect = self.level_image.get_rect()self.level_rect.right = self.score_rect.right# 將top屬性設(shè)置為比得分圖像的bottom屬性大10像素,以便在得分和等級之間留出一定的空間self.level_rect.top = self.score_rect.bottom + 10def prep_ships(self):"""顯示還剩下多少艘飛船"""self.ships = Group()for ship_number in range(self.stats.ship_left):ship = Ship(self.ai_settings, self.screen)ship.rect.x = 10 + ship_number * ship.rect.widthship.rect.y = 10self.ships.add(ship)button.py
# coding=utf-8import pygame.fontclass Button():def __init__(self, ai_settings, screen, msg):"""初始化按鈕的屬性"""self.screen = screenself.screen_rect = screen.get_rect()# 設(shè)置按鈕的屬性和其它屬性self.width, self.height = 200, 50self.button_color = (0, 255, 0)self.text_color = (255, 255, 255)self.font = pygame.font.SysFont(None, 48)# 創(chuàng)建按鈕的rect對象,并使其居中self.rect = pygame.Rect(0, 0, self.width, self.height)self.rect.center = self.screen_rect.center# 按鈕的標(biāo)簽只需創(chuàng)建一次self.prep_msg(msg)def prep_msg(self, msg):"""將msg渲染為圖像,并使其在按鈕上居中"""self.msg_image = self.font.render(msg, True, self.text_color, self.button_color)self.msg_image_rect = self.msg_image.get_rect()self.msg_image_rect.center = self.rect.centerdef draw_button(self):"""繪制一個(gè)顏色填充的按鈕,再繪制文本"""self.screen.fill(self.button_color, self.rect)self.screen.blit(self.msg_image, self.msg_image_rect)四、圖片領(lǐng)取位置
鏈接:https://pan.baidu.com/s/1rOU0Py5HOECxloOvKxtcoA
提取碼:qkhf
學(xué)習(xí)《Python編程從小白到入門》這本書之后,自己動手寫了這個(gè)游戲,總體還是不錯噠,如果有什么問題,歡迎給我留言傲!!!有什么不足歡迎指出。
整個(gè)項(xiàng)目下載
https://download.csdn.net/download/hanhanwanghaha/12708738
免費(fèi)不要C幣的傲哈哈哈哈!
總結(jié)
以上是生活随笔為你收集整理的外星人入侵游戏--python实战 (附完整代码)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python中的回文详解
- 下一篇: 杭电2051