当前位置: 首页>Python>正文

用python入侵別人手機,python外星人入侵游戲圖片_Python外星人入侵游戲編程完整版

用python入侵別人手機,python外星人入侵游戲圖片_Python外星人入侵游戲編程完整版

PYTHON游戲編程外星人入侵的完整實現思路,具體內容如下

準備工作:下載python,比如Anaconda3(64 bit),導入pygame游戲包

201704181156515.png

201704181156516.png

201704181156517.png

201704181156518.png

201704181156529.png

用python入侵別人手機、1.外星人設置,alien.py,代碼:

import pygame

from pygame.sprite import Sprite

python編程小游戲代碼。class Alien(Sprite):

"""表示單個外星人的類"""

def __init__(self,ai_settings,screen):

pygame外星人入侵,"""初始化外星人并設置其他位置"""

super(Alien,self).__init__()

self.screen = screen

self.ai_settings = ai_settings

#加載外星人圖像,并設置其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

#存儲外星人的準確位置

self.x = float(self.rect.x)

def blitme(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 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

2.游戲主程序,alien_invasion.py,代碼:

import pygame

from settings import Settings

from game_stats import GameStats

from button import Button

from ship import Ship

from pygame.sprite import Group

import game_functions as gf

from scoreboard import Scoreboard

def run_game():

pygame.init() # 初始化背景設置

ai_settings = Settings() # 全局設置

screen = pygame.display.set_mode( # 創建screen顯示窗口

(ai_settings.screen_width,ai_settings.screen_height)

)

pygame.display.set_caption('Alien Invasion') # 標題

#新建Play按鈕

play_button = Button(ai_settings,screen,"Play")

#創建一個用于存儲游戲統計信息的實例,并創建記分牌

stats = GameStats(ai_settings)

sb = Scoreboard(ai_settings, screen, stats)

# 創建飛船

ship = Ship(ai_settings,screen)

# 創建子彈編組

bullets = Group()

#創建一個外星人

aliens = Group()

#創建外星人群

gf.create_fleet(ai_settings,screen,ship,aliens)

# 開始游戲主循環

while True:

# 監視鍵盤和鼠標事件

gf.check_events(ai_settings,screen,stats,sb,play_button,ship,aliens,bullets)

if stats.game_active:

# 移動飛船

gf.update_ship(ship)

# 更新子彈位置

gf.update_bullets(ai_settings,screen,stats,sb,ship,aliens,bullets)

#更新外星人

gf.update_aliens(ai_settings,stats,screen,sb,ship,aliens,bullets)

# 更新屏幕

gf.update_screen(ai_settings,screen,stats,sb,ship,aliens,bullets,play_button)

run_game()

3.設置子彈,bullet.py,代碼:

import pygame

from pygame.sprite import Sprite

import time

class Bullet(Sprite):

'''飛船子彈進行管理'''

def __init__(self,ai_settings,screen,ship):

super(Bullet,self).__init__()

self.screen = screen

# 創建子彈矩形初始位置(0,0,3,15)分別對應lef,top,寬,高

self.rect = pygame.Rect(0,0,

ai_settings.bullet_width, ai_settings.bullet_height)

self.rect.centerx = ship.rect.centerx # 設置中心點x軸坐標跟飛船一致

self.rect.top = ship.rect.top # 設置y軸坐標頂部跟飛船一致

# 設置成小數進行計算

self.top = float(self.rect.top)

self.color = ai_settings.bullet_color

self.speed_factor = ai_settings.bullet_speed_factor

def update(self):

self.top -=self.speed_factor

self.rect.top = self.top

print(self.rect.top)

def draw_bullet(self):

pygame.draw.rect(self.screen,self.color,self.rect)

4.設置Play按鈕,button.py,代碼:

import pygame.font

class Button():

def __init__(self,ai_settings,screen,msg):

"""初始化按鈕屬性"""

self.screen = screen

self.screen_rect = screen.get_rect()

#設置按鈕的尺寸和其他屬性

self.width,self.height = 200,50

self.button_color = (0,255,0)

self.text_color = (255,255,255)

self.font = pygame.font.SysFont(None,48)

#創建按鈕的rect對象,并使其居中

self.rect = pygame.Rect(0,0,self.width,self.height)

self.rect.center = self.screen_rect.center

#按鈕的標簽只需創建一次

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.center

def draw_button(self):

#繪制一個用顏色填充的按鈕,再繪制文本

self.screen.fill(self.button_color,self.rect)

self.screen.blit(self.msg_image,self.msg_image_rect)

5.設置游戲功能,game_functions.py,代碼:

import sys

import pygame

from bullet import Bullet

from alien import Alien

from time import sleep

def check_events(ai_settings,screen,stats,sb,play_button,ship,aliens,bullets):

# 監視鍵盤和鼠標事件

for event in pygame.event.get():

if event.type == pygame.QUIT: # 關閉窗口退出

sys.exit()

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):

"""在玩家單擊Play按鈕時開始游戲"""

button_clicked = play_button.rect.collidepoint(mouse_x,mouse_y)

if button_clicked and not stats.game_active:

#重置游戲設置

ai_settings.initialize_dynamic_settings()

#隱藏光標

pygame.mouse.set_visible(False)

#重置游戲統計信息

stats.reset_stats()

stats.game_active = True

#重置計分牌圖像

sb.prep_score()

sb.prep_high_score()

sb.prep_level()

sb.prep_ships()

#清空外星人列表和子彈列表

aliens.empty()

bullets.empty()

#創建一群新的外星人,并讓飛船居中

create_fleet(ai_settings,screen,ship,aliens)

ship.center_ship()

def update_screen(ai_settings,screen,stats,sb,ship,aliens,bullets,play_button):

'''更新屏幕上的圖片,并切換到新屏幕'''

screen.fill(ai_settings.bg_color) # 設置背景顏色

ship.blitme() # 繪制飛船

aliens.draw(screen)

# 循環子彈組里面的元素,進行繪制 為空時不執行

for bullet in bullets.sprites():

bullet.draw_bullet() # 繪制子彈

#顯示得分

sb.show_score()

#如果游戲處于非活躍狀態,就顯示Play按鈕

if not stats.game_active:

play_button.draw_button()

# 顯示最新屏幕,擦拭舊屏幕

pygame.display.flip()

# print('1')

def check_keydown_events(event,ai_settings,screen,ship,bullets):

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_SPACE:

fire_bullet(ai_settings,screen,ship,bullets)

elif event.key == pygame.K_q:

sys.exit()

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

def update_bullets(ai_settings,screen,stats,sb,ship,aliens,bullets):

'''更新子彈位置,刪除子彈'''

bullets.update() # 子彈組每個成員執行self.update()操作

for bullet in bullets.sprites():

if bullet.rect.bottom <= 0: # 子彈出界 刪除

bullets.remove(bullet)

check_bullet_alien_collisions(ai_settings,screen,stats,sb,ship,aliens,bullets)

def check_bullet_alien_collisions(ai_settings,screen,stats,sb,ship,aliens,bullets):

"""響應外星人和子彈的碰撞"""

#刪除發生碰撞的子彈和外星人

collisions = pygame.sprite.groupcollide(bullets,aliens,True,True)

if collisions:

for aliens in collisions.values():

stats.score += ai_settings.alien_points * len(aliens)

sb.prep_score()

check_high_score(stats,sb)

if len(aliens)==0:

#刪除現有的子彈并新建一群外星人,加快游戲進度節奏

bullets.empty()

ai_settings.increase_speed()

#提高等級

stats.level += 1

sb.prep_level()

create_fleet(ai_settings,screen,ship,aliens)

def update_ship(ship):

ship.update()

def fire_bullet(ai_settings,screen,ship,bullets):

# 創建一個子彈對象 加入到子彈組

if len(bullets) < ai_settings.bullets_allowed: # 子彈少于允許值時再生成

new_bullet = Bullet(ai_settings, screen, ship)

bullets.add(new_bullet)

def get_number_aliens_x(ai_settings,alien_width):

"""計算每行可容納多少個外星人"""

available_space_x = ai_settings.screen_width - 2 * alien_width

number_aliens_x = int(available_space_x / (2 * alien_width))

return number_aliens_x

def get_number_rows(ai_settings,ship_height,alien_height):

"""計算屏幕可容納多少行外星人"""

available_space_y = (ai_settings.screen_height - (3 * alien_height) - ship_height)

number_rows = int(available_space_y / (2 * alien_height))

return number_rows

def create_aliens(ai_settings,screen,aliens,alien_number,row_number):

"""創建一個外星人并將其放在當期行"""

alien = Alien(ai_settings,screen)

alien_width = alien.rect.width

alien.x = alien_width + 2 * alien_width * alien_number

alien.rect.x = alien.x

alien.rect.y = alien.rect.height + 2 * alien.rect.height * row_number

aliens.add(alien)

def create_fleet(ai_settings,screen,ship,aliens):

"""創建外星人群"""

#創建一個外星人,并計算一行可以容納多少個外星人

#外星人間距為外星人寬度

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)

#創建第一行外星人

for row_number in range(number_rows):

for alien_number in range(number_aliens_x):

#創建一個外星人并將其加入當前行

create_aliens(ai_settings,screen,aliens,alien_number,row_number)

def check_fleet_edges(ai_settings,aliens):

"""有外星人到達邊緣時采取相應措施"""

for alien in aliens.sprites():

if alien.check_edges():

change_fleet_direction(ai_settings,aliens)

break

def change_fleet_direction(ai_settings,aliens):

"""將整群外星人下移,并改變他們的運動方向"""

for alien in aliens.sprites():

alien.rect.y += ai_settings.fleet_drop_speed

ai_settings.fleet_direction *= -1

def ship_hit(ai_settings,stats,screen,sb,ship,aliens,bullets):

"""響應被外星人撞到的飛船"""

if stats.ships_left > 0:

#將ship_left減1

stats.ships_left -= 1

#更新記分牌

sb.prep_ships()

#清空外星人列表和子彈列表

aliens.empty()

bullets.empty()

#創建一群新的外星人,并將飛船放到屏幕低端中央

create_fleet(ai_settings,screen,ship,aliens)

ship.center_ship()

#暫停

sleep(0.5)

else:

stats.game_active = False

pygame.mouse.set_visible(True)

def check_aliens_bottom(ai_settings,stats,screen,sb,ship,aliens,bullets):

"""檢查是否有外星人到達屏幕低端"""

screen_rect = screen.get_rect()

for alien in aliens.sprites():

if alien.rect.bottom >= screen_rect.bottom:

#像飛船被撞到一樣進行處理

ship_hit(ai_settings,stats,screen,sb,ship,aliens,bullets)

break

def update_aliens(ai_settings,stats,screen,sb,ship,aliens,bullets):

"""更新外星人群中所有外星人的位置"""

check_fleet_edges(ai_settings,aliens)

aliens.update()

#檢測外星人和飛船之間的碰撞

if pygame.sprite.spritecollideany(ship,aliens):

ship_hit(ai_settings,stats,screen,sb,ship,aliens,bullets)

#檢查是否有外星人到達屏幕低端

check_aliens_bottom(ai_settings,stats,screen,sb,ship,aliens,bullets)

def check_high_score(stats,sb):

"""檢查是否誕生了新的最高紀錄"""

if stats.score > stats.high_score:

stats.high_score = stats.score

sb.prep_high_score()

6.游戲統計信息,game_stats.py,代碼:

class GameStats():

"""跟蹤游戲的統計信息"""

def __init__(self,ai_settings):

"""初始化統計信息"""

self.ai_settings = ai_settings

self.reset_stats()

#游戲剛啟動時處于非活動狀態

self.game_active = False

#在任何情況下不應該重置最高分

self.high_score = 0

self.level = 1

def reset_stats(self):

"""初始化在游戲運行期間可能變化的統計信息"""

self.ships_left = self.ai_settings.ship_limit

self.score = 0

7.分數設置,scoreboard.py,代碼:

import pygame.font

from pygame.sprite import Group

from ship import Ship

class Scoreboard():

"""顯示得分信息的類"""

def __init__(self, ai_settings, screen, stats):

"""初始化顯示得分涉及的屬性"""

self.screen =screen

self.screen_rect = screen.get_rect()

self.ai_settings = ai_settings

self.stats = stats

#顯示得分信息時使用的字體設置

self.text_color = (30, 30, 30)

self.font = pygame.font.SysFont(None, 48)

#準備初始化得分圖像和當前最高分數

self.prep_score()

self.prep_high_score()

self.prep_level()

self.prep_ships()

def prep_score(self):

"""將得分轉換為一幅渲染的圖像"""

rounded_score = int(round(self.stats.score, -1))

score_str = "{:,}".format(rounded_score)

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 - 20

self.score_rect.top = 5

def prep_high_score(self):

"""將最高得分轉換為渲染圖像"""

high_score = int(round(self.stats.high_score, -1))

high_score_str = "{:,}".format(high_score)

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.centerx

self.high_score_rect.top = 5

def prep_level(self):

"""將等級轉換為渲染圖像"""

self.level_image = self.font.render(str(self.stats.level), True, self.text_color, self.ai_settings.bg_color)

#將得分放在右上角

self.level_rect = self.score_image.get_rect()

self.level_rect.right = self.screen_rect.right

self.level_rect.top = self.score_rect.bottom

def prep_ships(self):

"""顯示還剩下多少艘飛船"""

self.ships = Group()

for ship_number in range(self.stats.ships_left):

ship = Ship(self.ai_settings, self.screen)

ship.rect.x = 10 + ship_number * ship.rect.width

ship.rect.y = 10

self.ships.add(ship)

def 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)

8.設置,settings.py,代碼:

class Settings():

'''存儲外星人入侵中所有的設置'''

def __init__(self):

'''初始化設置'''

#屏幕設置

self.screen_width = 1200

self.screen_height = 600

self.bg_color = (230,230,230) # 設置背景色 灰色

#飛船設置

self.ship_limit = 3

self.ship_image_path = 'images/ship.bmp' # 飛船圖片路徑

#子彈設置

self.bullet_width = 3

self.bullet_height = 15

self.bullet_color = 60,60,60

self.bullets_allowed = 3 # 允許屏幕中出現子彈的數量

#外星人設置

self.fleet_drop_speed = 10

#以什么樣的速度加快游戲節奏

self.speedup_scale = 1.1

#外星人點數提高速度

self.score_scale = 1.5

self.initialize_dynamic_settings()

def initialize_dynamic_settings(self):

"""初始化隨游戲進行而變化的設置"""

self.ship_speed_factor = 1.5

self.bullet_speed_factor = 3

self.alien_speed_factor = 1

#fleet_direction為1表示向右移,為-1表示向左移

self.fleet_direction = 1

#計分

self.alien_points = 50

def increase_speed(self):

"""提高速度設置,外星人點數"""

self.ship_speed_factor *= self.speedup_scale

self.bullet_speed_factor *= self.speedup_scale

self.alien_speed_factor *= self.speedup_scale

self.alien_points = int(self.alien_points * self.score_scale)

print(self.alien_points)

9.飛船設置,ship.py,代碼:

import pygame

from pygame.sprite import Sprite

class Ship(Sprite):

'''飛船所有信息'''

def __init__(self,ai_settings,screen):

"""初始化飛船,并設置其起始位置"""

super(Ship,self).__init__()

self.screen=screen

self.ai_settings = ai_settings

# 加載飛船圖片、獲取外接矩形

self.image = pygame.image.load(self.ai_settings.ship_image_path) # 加載圖片

self.image = pygame.transform.smoothscale(self.image,(40,60))

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.rect.centerx設置不了浮點數 只能另設置一個變量進行運算

# 移動標志

self.moving_right = False

self.moving_left = False

def blitme(self):

'''在指定位置繪制飛船'''

self.screen.blit(self.image,self.rect)

def update(self):

# 向右移動飛船

if self.moving_right and self.rect.right < self.screen_rect.right:

self.center +=self.ai_settings.ship_speed_factor

# 向左移動飛船

if self.moving_left and self.rect.left > self.screen_rect.left:

self.center -= self.ai_settings.ship_speed_factor

self.rect.centerx = self.center

def center_ship(self):

"""讓飛船在屏幕上居中"""

self.center = self.screen_rect.centerx

效果展示:

2017041811565210.png

游戲資源:圖片資源

2017418120021325.jpg

2017418120046186.jpg

更多關于python游戲的精彩文章請點擊查看以下專題:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

https://www.nshth.com/python/338578.html
>

相关文章:

  • 用python入侵別人手機
  • python編程小游戲代碼
  • pygame外星人入侵
  • 外星人入侵循環導入
  • python武裝飛船在手機上運行
  • 貓與老鼠外星人入侵
  • python游戲
  • python外星人入侵
  • 國二python難嗎,python字符串(二)
  • 電腦軟件下載app,在電腦上體驗了 16 款手機 App 后,我很失望
  • elasticsearch中文文檔,Elastic安全分析新利器 —— Event Query Language (EQL) 介紹
  • 數據結構與算法python,[FreeCodeCamp筆記] Python 數據結構和算法1 二分搜索 Binary Search
  • 黑蘋果macOS系統鏡像工具,MacOS Monterey 12.2.1 (21D62) OC 0.7.8 / Cl 5144 / PE 三分區原版黑蘋果鏡像
  • 51單片機畢業設計論文,【畢業設計】基于單片機無線充電的4軸飛行器 -物聯網 嵌入式 stm32
  • 數據庫基礎知識整理,數據庫筆記整理
  • python運行不報錯又無任何結果輸出,linux 正確錯誤輸出_報告錯誤的正確方法
  • 計算機組成原理第六版課后答案,杭電計算機組成原理實驗九R-I,杭電計組實驗9-實現R-I型指令的CPU設計實驗.doc
  • python面向對象,Python零基礎速成班-第10講-Python面向對象編程(下),Property屬性、特殊方法、設計模式、鏈表應用
  • 數據庫視圖是什么,【SpringMVC】SpringMVC模型數據+視圖解析器
  • mp3格式轉換器,FFmpeg支持的音頻和視頻編解碼格式
  • 音樂學校招生要求,學校的音樂樓
  • c語言輸入兩個數輸出較大數,C語言求兩個數的較大值
  • 定義一個函數求三個數的最大值,輸入兩個整數,要求輸出其中值較大者。要求用函數求出最大值
  • MySQL學習 DAY1
  • 一個眼神一個微笑就讓人滿足,看得到的微笑
  • centos7安裝MySQL,centos7下載spark連接mysql數據庫提取數據(pyspark,Scala,python獨立執行)
  • node.js開發,從零開始nodejs系列文章-nodejs到底能干什么
  • python控制軟件自動化,Python實現網站自動登錄---傻瓜教程
  • get all of,resent = msg.get_all('Resent-Date') AttributeError: 'str' object h
  • opencv人體動作識別,torchvision使用keypoint rcnn 進行人體關鍵點定位
  • 深度卷積神經網絡原理與實踐,卷積神經網絡resent網絡實踐
  • 服務器,win服務器系統路由器,Windows server 2012 之路由功能
  • 小青蛙走迷宮的問題
  • 漢諾塔-小青蛙
  • 小青蛙oracle跟蹤,在小青蛙TOAD中用oracle語句寫
  • 音頻頻譜分析儀安卓版,[Android]自定義繪制一個簡易的音頻條形圖,附上對MP3音頻波形數據的采集與展現
  • 連乘符號∏的運算法則,∏這個是什么符號?
  • 用例失敗jenkins卻構建成功,jenkins 構建異常_jenkins構建失敗的原因是什么?