日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python3 用pygame 简单建一个飞船模型

發布時間:2024/4/18 python 53 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python3 用pygame 简单建一个飞船模型 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文所用到的ship.bmp和alien.bmp的下載? ?鏈接: https://pan.baidu.com/s/1Dn5yaQGZLIsN0BohM8PzoA ?密碼: 38ie

第一部分:認識pygame的簡單的一些函數,利用命令簡單地構造畫面

我用我個人的理解逐漸剖析如何在python3中建一個窗口,里面放置一個飛船。

首先我們先來分析一些事情:

?事情1:用python3,創建游戲窗口需要以下代碼:

? ? ? ?導入pygame,就是執行import pygame

? ? ? ?導入sys,就是執行import sys,這個主要是一會調用sys.exit()結束游戲用的

? ? ? ?首先是執行pygame.init()? ? 這個語句會初始化pygame,使得pygame可用,里面初始化了很多組建,大約9個以內,具體什么也忘記了

? ? ? ?其次是設置窗口大小,執行pygame.display.set_mode((1000,800)),這個執行完畢后,以后入職建立無限循環,那么就可以把它畫進去。

? ? ? ?然后值設置窗口說明,執行pygame.display.set_caption("some caption like.....")

? ? ? ?如果要設置窗口的背景顏色,那么還需要定義一個顏色,例如(255,0,255)

? ? ? 接下來建立一個無限循環,就可以畫出剛才定義好的窗口,代碼如下:

import sys import pygame pygame.init() screen = pygame.display.set_mode((1000,900)) pygame.display.set_caption("alien invasion") color = (0,255,255) while True:for event in pygame.event.get():if event.type == pygame.QUIT:sys.exit()screen.fill(color)pygame.display.flip()

事情2:

畫一個飛船,并且顯示在事情1定義的screen中:

import sys import pygamepygame.init() screen = pygame.display.set_mode((1000,900))#create ship screen_rect = screen.get_rect() ship_image = pygame.image.load('image/ship.bmp') ship_rect = ship_image.get_rect() ship_rect.centerx = screen_rect.centerx ship_rect.bottom = screen_rect.bottompygame.display.set_caption("alien invasion") color = (0,255,255) while True:for event in pygame.event.get():if event.type == pygame.QUIT:sys.exit()#這里一定注意,先執行填充顏色,再執行填充位圖,很好理解,否則飛船會被顏色覆蓋#先執行screen.fill(color)screen.fill(color)#后執行填充位圖screen.blit(....)screen.blit(ship_image,ship_rect)pygame.display.flip()

執行結果:

其實,代碼可以簡寫為如下形式:

import sys import pygamepygame.init() screen = pygame.display.set_mode((1000,900))#create ship screen_rect = screen.get_rect() ship_image = pygame.image.load('image/ship.bmp') ship_rect = ship_image.get_rect() #以下兩句是改變飛船位置的,如果不改變位置,則使用默認位置,也就是左上角 #ship_rect.centerx = screen_rect.centerx #ship_rect.bottom = screen_rect.bottompygame.display.set_caption("alien invasion") color = (0,255,255) while True:for event in pygame.event.get():if event.type == pygame.QUIT:sys.exit()screen.fill(color)screen.blit(ship_image,ship_rect)pygame.display.flip()

執行結果如下:

?

更簡單的情況,畫一個飛船,直接傳給screen去顯示:

import sys import pygame pygame.init() screen = pygame.display.set_mode((1000,800)) ship = pygame.image.load('ship.bmp') ship_rect = ship.get_rect() pygame.display.set_caption("alien invasion")while True:screen.fill((255,4,245))screen.blit(ship,ship_rect)pygame.display.flip()

個人看法:在一個screen內建ship,其實只要建screen和ship,并且直接把ship的圖像和位置傳給screen即可。screen.blit(圖片,圖片位置)

?

接下來,心血來潮,再在screen底部中央畫一個alien.bmp,先把alien.bmp放置在python3代碼文件同一個文件夾下,然后編寫如下代碼:

import sys import pygame pygame.init() screen = pygame.display.set_mode((1000,800)) pygame.display.set_caption("alien invasion") photo = pygame.image.load('alien.bmp') photo_rect = photo.get_rect() screen_rect = screen.get_rect()#set the ship on the center of the screen photo_rect.centerx = screen_rect.centerx photo_rect.bottom = screen_rect.bottom while True:for event in pygame.event.get():if event.type == pygame.QUIT:sys.exit()#screen.blit(photo,photo_rect)screen.fill((255,34,54))screen.blit(photo,photo_rect)pygame.display.flip()

執行結果:

第二部分:進階

接下來,我們熟悉pygame的簡單構造過程后,把上述代碼放在類和函數中實現,實現模塊化編程,如下:

ship.py文件:

#ship.py import pygame class Ship(): """創建ship"""def __init__(self,screen):self.screen = screenself.screen_rect = screen.get_rect()self.image = pygame.image.load('image/ship.bmp')self.image_rect = self.image.get_rect()self.image_rect.centerx = self.screen_rect.centerxself.image_rect.bottom = self.screen_rect.bottomdef blitme(self):"""在screen中畫創建出的ship"""self.screen.blit(self.image,self.image_rect)~

?下面是文件3.py

#文件3.py import sys import pygame from ship import Ship def run_game(): """創建動畫"""pygame.init()screen = pygame.display.set_mode((1000,800))pygame.display.set_caption("ship is come here")#create a exampleship = Ship(screen)color = (255,255,255)while True:for event in pygame.event.get():if event.type == pygame.QUIT:sys.exit()screen.fill(color)#screen.blit(ship.image,ship.image_rect)ship.blitme()pygame.display.flip() run_game()

然后把ship.bmp,放在當前文件夾的image文件夾下?,準備運行

在ubuntu中執行命令(在windows中執行命令是python 3.py)

python3 3.py

運行結果:

接下來,繼續優化程序,把顏色,caption,screen的寬度和高度等信息單獨放在一個設置文件中,那么現在我們一共有3個程序文件了,還有一個圖片文件,所以要運行程序共需要4個程序,

文件ship.py

#文件ship.py import pygame class Ship():"""創建ship"""def __init__(self,screen):self.screen = screenself.screen_rect = screen.get_rect()self.image = pygame.image.load('image/ship.bmp')self.image_rect = self.image.get_rect()self.image_rect.centerx = self.screen_rect.centerxself.image_rect.bottom = self.screen_rect.bottomdef blitme(self):"""在screen中畫ship"""self.screen.blit(self.image,self.image_rect)

文件settings.py

class Settings():def __init__(self):self.color = (255,255,255)self.set_width = 1000self.set_height = 800self.caption = "alien ship"

文件ship_game.py

#文件ship_game.py #它的功能是:構造動畫的主程序 import sys import pygame from settings import Settings from ship import Ship def run_game():pygame.init()myset = Settings()screen = pygame.display.set_mode((myset.set_width,myset.set_height))pygame.display.set_caption(myset.caption)#create a exampleship = Ship(screen)while True:for event in pygame.event.get():if event.type == pygame.QUIT:sys.exit()screen.fill(myset.color)#screen.blit(ship.image,ship.image_rect)ship.blitme()pygame.display.flip() run_game()

文件ship.bmp放在當前文件夾image下,

接下來運行文件,ubuntu中是:

python3 ship_game.py

windows中是(注意windows中如果相對路徑找不到ship.bmp文件,那就使用絕對路徑):

python ship_game.py

?

?

總結

以上是生活随笔為你收集整理的python3 用pygame 简单建一个飞船模型的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。