飛機大戰大家都熟悉不過了,相信每個人都有這么一段美好的回憶,作為程式員的我們是不是可以用代碼來回憶一下我們失去的青春,python語言博大精深,它目前來說已經很火熱了,在學好python的同時還要學會用python,可以使用它來開發一些專案最好不過了,
該游戲在開發之前用到Pygame庫,大家一定要記著安裝哦!
也可以先通過下列命令查看你是否安裝了第三方庫Pygame,
pip list


可以看得到我已經安裝成功了,如果沒有安裝,那就請參考文獻python在是那種不同軟體方式安裝第三方庫請點擊這里
先看看游戲進行時的場面:

當你太菜,打輸的時候的截圖:

展示一下python源代碼:
# -*- coding: utf-8 -*-
"""
Created on 2020.12.14
@author: 鵬鵬寫代碼
"""
import pygame
from sys import exit
from pygame.locals import *
from gameRole import *
import random
# 初始化游戲
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('飛機大戰')
# 載入游戲音樂
bullet_sound = pygame.mixer.Sound('resources/sound/bullet.wav')
enemy1_down_sound = pygame.mixer.Sound('resources/sound/enemy1_down.wav')
game_over_sound = pygame.mixer.Sound('resources/sound/game_over.wav')
bullet_sound.set_volume(0.3)
enemy1_down_sound.set_volume(0.3)
game_over_sound.set_volume(0.3)
pygame.mixer.music.load('resources/sound/game_music.wav')
pygame.mixer.music.play(-1, 0.0)
pygame.mixer.music.set_volume(0.25)
# 載入背景圖
background = pygame.image.load('resources/image/background.png').convert()
game_over = pygame.image.load('resources/image/gameover.png')
filename = 'resources/image/shoot.png'
plane_img = pygame.image.load(filename)
# 設定玩家相關引數
player_rect = []
player_rect.append(pygame.Rect(0, 99, 102, 126)) # 玩家精靈圖片區域
player_rect.append(pygame.Rect(165, 360, 102, 126))
player_rect.append(pygame.Rect(165, 234, 102, 126)) # 玩家爆炸精靈圖片區域
player_rect.append(pygame.Rect(330, 624, 102, 126))
player_rect.append(pygame.Rect(330, 498, 102, 126))
player_rect.append(pygame.Rect(432, 624, 102, 126))
player_pos = [200, 600]
player = Player(plane_img, player_rect, player_pos)
# 定義子彈物件使用的surface相關引數
bullet_rect = pygame.Rect(1004, 987, 9, 21)
bullet_img = plane_img.subsurface(bullet_rect)
# 定義敵機物件使用的surface相關引數
enemy1_rect = pygame.Rect(534, 612, 57, 43)
enemy1_img = plane_img.subsurface(enemy1_rect)
enemy1_down_imgs = []
enemy1_down_imgs.append(plane_img.subsurface(pygame.Rect(267, 347, 57, 43)))
enemy1_down_imgs.append(plane_img.subsurface(pygame.Rect(873, 697, 57, 43)))
enemy1_down_imgs.append(plane_img.subsurface(pygame.Rect(267, 296, 57, 43)))
enemy1_down_imgs.append(plane_img.subsurface(pygame.Rect(930, 697, 57, 43)))
enemies1 = pygame.sprite.Group()
# 存盤被擊毀的飛機,用來渲染擊毀精靈影片
enemies_down = pygame.sprite.Group()
shoot_frequency = 0
enemy_frequency = 0
player_down_index = 16
score = 0
clock = pygame.time.Clock()
running = True
while running:
# 控制游戲最大幀率為60
clock.tick(60)
# 控制發射子彈頻率,并發射子彈
if not player.is_hit:
if shoot_frequency % 15 == 0:
bullet_sound.play()
player.shoot(bullet_img)
shoot_frequency += 1
if shoot_frequency >= 15:
shoot_frequency = 0
# 生成敵機
if enemy_frequency % 50 == 0:
enemy1_pos = [random.randint(0, SCREEN_WIDTH - enemy1_rect.width), 0]
enemy1 = Enemy(enemy1_img, enemy1_down_imgs, enemy1_pos)
enemies1.add(enemy1)
enemy_frequency += 1
if enemy_frequency >= 100:
enemy_frequency = 0
# 移動子彈,若超出視窗范圍則洗掉
for bullet in player.bullets:
bullet.move()
if bullet.rect.bottom < 0:
player.bullets.remove(bullet)
# 移動敵機,若超出視窗范圍則洗掉
for enemy in enemies1:
enemy.move()
# 判斷玩家是否被擊中
if pygame.sprite.collide_circle(enemy, player):
enemies_down.add(enemy)
enemies1.remove(enemy)
player.is_hit = True
game_over_sound.play()
break
if enemy.rect.top > SCREEN_HEIGHT:
enemies1.remove(enemy)
# 將被擊中的敵機物件添加到擊毀敵機Group中,用來渲染擊毀影片
enemies1_down = pygame.sprite.groupcollide(enemies1, player.bullets, 1, 1)
for enemy_down in enemies1_down:
enemies_down.add(enemy_down)
# 繪制背景
screen.fill(0)
screen.blit(background, (0, 0))
# 繪制玩家飛機
if not player.is_hit:
screen.blit(player.image[player.img_index], player.rect)
# 更換圖片索引使飛機有影片效果
player.img_index = shoot_frequency // 8
else:
player.img_index = player_down_index // 8
screen.blit(player.image[player.img_index], player.rect)
player_down_index += 1
if player_down_index > 47:
running = False
# 繪制擊毀影片
for enemy_down in enemies_down:
if enemy_down.down_index == 0:
enemy1_down_sound.play()
if enemy_down.down_index > 7:
enemies_down.remove(enemy_down)
score += 1000
continue
screen.blit(enemy_down.down_imgs[enemy_down.down_index // 2], enemy_down.rect)
enemy_down.down_index += 1
# 繪制子彈和敵機
player.bullets.draw(screen)
enemies1.draw(screen)
# 繪制得分
score_font = pygame.font.Font(None, 36)
score_text = score_font.render(str(score), True, (128, 128, 128))
text_rect = score_text.get_rect()
text_rect.topleft = [10, 10]
screen.blit(score_text, text_rect)
# 更新螢屏
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
# 監聽鍵盤事件
key_pressed = pygame.key.get_pressed()
# 若玩家被擊中,則無效
if not player.is_hit:
if key_pressed[K_w] or key_pressed[K_UP]:
player.moveUp()
if key_pressed[K_s] or key_pressed[K_DOWN]:
player.moveDown()
if key_pressed[K_a] or key_pressed[K_LEFT]:
player.moveLeft()
if key_pressed[K_d] or key_pressed[K_RIGHT]:
player.moveRight()
font = pygame.font.Font(None, 48)
text = font.render('Score: '+ str(score), True, (255, 0, 0))
text_rect = text.get_rect()
text_rect.centerx = screen.get_rect().centerx
text_rect.centery = screen.get_rect().centery + 24
screen.blit(game_over, (0, 0))
screen.blit(text, text_rect)
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
pygame.display.update()
看代碼感覺很簡單,但是要切實際的運行出來是非常難得,本游戲用到的游戲場景照片均參考自python專案案例開發到入門,大家如果感興趣可以自己買了多看看哦,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/234958.html
標籤:其他
上一篇:Linux驅動開發——按鍵為例介紹Linux內核中斷
下一篇:第三次周賽題解
