您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關教你利用python編寫一個飛機大戰小游戲,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
1.首先安裝庫
pip install pygame
2.主要python代碼
import pygame from pygame.locals import * import random #https://blog.csdn.net/qq_36079986/article/details/110395731 class HeroPlane(object):# 創建一個飛機的英雄類 def __init__(self,screen_temp): # 始化方法 self.x = 200 self.y = 500 self.image = pygame.image.load('./img/hero1.png') self.screen = screen_temp# 因為在display方法中使用到了screen方法# 但是在類中沒有 也不能調用 # 所以說 以傳參的方法傳遞到類中 self.bullet_list = [] def display(self):# 創建一個展現飛機的方法 herp_x1, herp_y1, herp_x2, herp_y2, herp_x3, herp_y3 = -100, -100, -100, -100, -100, -100 self.screen.blit(self.image,(self.x, self.y)) for bullet in self.bullet_list:###遍歷子彈類對象,賦值給bullet herp_x1, herp_y1, herp_x2, herp_y2, herp_x3, herp_y3 = bullet.display()###### bullet對象調用display方法, 顯示子彈 bullet.move() if self.bullet_list[0].y < 0: del self.bullet_list[0] return herp_x1, herp_y1, herp_x2, herp_y2, herp_x3, herp_y3 def move_left(self): self.x -= 50 def move_right(self): self.x += 50 def move_up(self): self.y -= 50 def move_down(self): self.y += 50 def fire(self): print(len(self.bullet_list),'個子彈##########################################') self.bullet_list.append(HeroBullet(self.screen,self.x, self.y)) class HeroBullet(object): def __init__(self,screen_temp,x_temp,y_temp): self.x = x_temp self.y = y_temp self.image = pygame.image.load('./img/bullet.png') self.screen = screen_temp def display(self): self.screen.blit(self.image,(self.x - 22, self.y-22)) self.screen.blit(self.image, (self.x + 39, self.y-22)) self.screen.blit(self.image, (self.x + 100, self.y-22)) return self.x - 22, self.y - 22, self.x + 39, self.y - 22, self.x + 100, self.y - 22 def move(self): self.y -= 1 class EnemyPlane(object): def __init__(self,screen_temp): self.ra = random.randint(0,2) self.x = random.randint(0,400) self.y = 0 self.image = pygame.image.load('./img/enemy1.png') if 0<=self.x<=540: if self.ra == 0: self.flag = "left" elif self.ra == 1: self.flag = "right" else: self.flag = 'middle' self.screen.blit(self.image, (self.x, self.y)) if self.x < 0: self.flag = "left" elif self.x > 450: self.flag = 'right' if self.flag == "left": self.x += 2 elif self.flag == 'right': self.x -= 2 self.y += 0.1 if (random.randint(0,50) == 1): self.bullet_list.append(EnemyBullet(self.screen, self.x, self.y)) # print(len(self.bullet_list),'顆敵方子彈') for bullet in self.bullet_list: bullet.display() if bullet.y > 700: del bullet class EnemyBullet(object): def __init__(self,screen_temp,x,y): self.x = x + 30 self.y = y + 90 self.image = pygame.image.load('./img/bullet-1.gif') self.y+= 10 def key_control(hero_temp): for event in pygame.event.get(): # 判斷輸入類型 if event.type == QUIT:#判斷是否是點擊了退出按鈕 print("exit") exit() elif event.type == KEYDOWN:#判斷是否是按下了鍵# 如果是鍵盤事件(鍵盤按下事件) if event.key == K_a or event.key == K_LEFT: hero_temp.move_left() elif event.key == K_d or event.key == K_RIGHT: hero_temp.move_right() elif event.key == K_w or event.key == K_UP: hero_temp.move_up() elif event.key == K_s or event.key == K_DOWN: hero_temp.move_down() elif event.key == K_SPACE:#檢測按鍵是否是空格鍵 hero_temp.fire() def main(): screen = pygame.display.set_mode((480, 650), 0, 32) # 使用screen資源來創建背景# 返回值是一個資源 pygame.display.set_caption("aircraft game") # 設置窗口標題 background = pygame.image.load('./img/background.png')## ./ 當前路徑 ../ 上一級路徑 / 根目錄 hero = HeroPlane(screen) enemy_list = [] j = 0 while True: screen.blit(background, (0, 0)) # 資源中的blit方法能夠加載游戲的畫面 herp_x1, herp_y1, herp_x2, herp_y2, herp_x3, herp_y3 = hero.display() #####刷新 if len(enemy_list) <= 10: enemy_list.append(EnemyPlane(screen)) if enemy_list[0].y >= 700: del enemy_list[0] for enemy_each in enemy_list : # enemy_each.fire() enemy_each.move() enemy_each.display() # print(len(enemy_list), '個敵機') if (enemy_each.x <= herp_x1 -10 <= enemy_each.x + 80 and enemy_each.y -10<= herp_y1<= enemy_each.y + 90) or (enemy_each.x -10 <= herp_x2<= enemy_each.x + 80 and enemy_each.y -10<= herp_y2<= enemy_each.y + 90) or (enemy_each.x-10 <= herp_x3<= enemy_each.x + 80 and enemy_each.y -10<= herp_y3<= enemy_each.y + 90): enemy_list.remove(enemy_each) # print(len(enemy_list), '個敵機') print('jizhon') # print(hero.x,type(hero.x)) if j % 100 == 5: print(len(enemy_list),'個敵機') pygame.display.update() ### 加載游戲 key_control(hero)# 調用鍵盤事件函數 j += 1 # print(herp_x1, herp_y1, herp_x2, herp_y2, herp_x3, herp_y3) if __name__ == '__main__': main()
三、說明:代碼功能都在注釋里面,我就不做過多解釋了;與本項目有關的圖片數據我傳到資源里面了,大家可以免費下載,我把代碼和數據都打包到一起了,大家下載下來導入庫就可以直接一些了。
實例補充
import pygame class MyPlane(pygame.sprite.Sprite): def __init__(self,bg_size): pygame.sprite.Sprite.__init__(self) self.image1 = pygame.image.load(r'E:\learn pygame\image\me1.png').convert_alpha() self.image2 = pygame.image.load(r'E:\learn pygame\image\me2.png').convert_alpha() self.image3 = pygame.image.load(r'E:\learn pygame\image\me_protect.png').convert_alpha() self.destroy_images = [] self.destroy_images.extend([\ pygame.image.load(r'E:\learn pygame\image\me_destroy_1.png').convert_alpha(), \ pygame.image.load(r'E:\learn pygame\image\me_destroy_2.png').convert_alpha(), \ pygame.image.load(r'E:\learn pygame\image\me_destroy_3.png').convert_alpha(), \ pygame.image.load(r'E:\learn pygame\image\me_destroy_4.png').convert_alpha() \ ]) self.rect = self.image1.get_rect() self.width,self.height = bg_size[0],bg_size[1] self.rect.left,self.rect.top = (self.width - self.rect.width) // 2,self.height - self.rect.height - 60 self.speed = 10 self.active = True self.invincible = False self.mask = pygame.mask.from_surface(self.image1) def moveUp(self): if self.rect.top > 0: self.rect.top -= self.speed else: self.rect.top = 0 def moveDown(self): if self.rect.bottom < self.height - 30: self.rect.top += self.speed else: self.rect.bottom = self.height - 30 def moveLeft(self): if self.rect.left > 0: self.rect.left -= self.speed else: self.rect.left = 0 def moveRight(self): if self.rect.right < self.width: self.rect.left += self.speed else: self.rect.right = self.width def reset(self): self.rect.left,self.rect.top = (self.width - self.rect.width) // 2,self.height - self.rect.height - 30 self.active = True self.invincible = True
以上就是教你利用python編寫一個飛機大戰小游戲,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。