'''
新手剛學python,仿著老師敲的程式碼。
1、敵方飛機只能左右徘徊(不會往下跑)並且不會發射子彈。
2、正在研究怎麼寫計分。
3、也參考了不少大佬的程式碼,但也僅僅只是參考了。
加油!
'''
import pygame
from pygame.locals import *
import time
# 子彈類
class Bullet(object):
# 初始化子彈屬性:xy座標、視窗、子彈圖片
def __init__(self, screen_temp, x, y):
self.x = x--18
self.y = y-15
self.screen = screen_temp
self.image = pygame.image.load("zd.png")
# 子彈顯示
def display(self):
self.screen.blit(self.image, (self.x, self.y))
# 子彈移動,y -= 10
def move(self):
self.y -= 10
# 判斷子彈是否越界
def judge(self):
if self.y < 0:
return True
else:
return False
# 玩家飛機類
class Aircraft_obj(object):
# 初始化玩家飛機屬性:座標、視窗、子彈列表物件
def __init__(self, screen_temp):
self.x = 190
self.y = 650
self.screen = screen_temp
self.image = pygame.image.load("FeiJi.jpeg")
self.bullet_list = []
# 顯示玩家飛機圖片,for迴圈顯示發射子彈,如越界則刪除子彈
def display(self):
self.screen.blit(self.image, (self.x, self.y))
for bullet in self.bullet_list:
bullet.display()
bullet.move()
if bullet.judge():
self.bullet_list.remove(bullet)
# 玩家飛機移動
def move_left(self):
if self.x < 10:
pass
else:
self.x -= 10
def move_right(self):
if self.x > 480-100-10:
pass
else:
self.x += 10
def move_up(self):
if self.y < 10:
pass
else:
self.y -= 10
def move_down(self):
if self.y > 650:
pass
else:
self.y += 10
# 將子彈儲存在發射列表中
def fire(self):
self.bullet_list.append(Bullet(self.screen, self.x, self.y))
# 敵機類
class EnemyPlane(object):
# 初始化敵機屬性:座標、視窗、圖片、移動反向、是否爆炸、爆炸圖片列表
def __init__(self, screen_temp, x, y):
self.x = x
self.y = y
self.screen = screen_temp
self.image = pygame.image.load("DiJi.jpg")
self.direction = 'right'
self.hit = False
self.bomb_lists = []
self.__crate_images()
self.image_num = 0
self.image_index = 0
# 敵機左右移動
def move(self):
if self.hit == True:
pass
else:
if self.direction == 'right':
self.x += 5
elif self.direction == 'left':
self.x -= 5
if self.x > 420-50:
self.direction = 'left'
elif self.x < 0:
self.direction = 'right'
# 新增爆炸圖片列表
def __crate_images(self):
self.bomb_lists.append(pygame.image.load("baozha.png"))
self.bomb_lists.append(pygame.image.load("baozha.png"))
self.bomb_lists.append(pygame.image.load("baozha.png"))
self.bomb_lists.append(pygame.image.load("baozha.png"))
# 判斷是否爆炸
def blast(self, x1, x2, y):
if((x1>=self.x and x2<=self.x+51) or x2==self.x or x1==self.x+51) and y<39:
self.hit = True
# 顯示爆炸效果或正常圖片,利用迴圈和sleep()暫停讓使用者看清
def display(self):
if self.hit == True:
self.screen.blit(self.bomb_lists[self.image_index], (self.x, self.y))
self.image_num += 1
if self.image_num == 7:
self.image_num = 0
self.image_index += 1
if self.image_index > 3:
# 暫停1秒
time.sleep(1)
exit()
else:
self.screen.blit(self.image, (self.x, self.y))
# 檢查鍵盤輸入函式
def key_control(aircraft_temp):
for event in pygame.event.get():
# 退出
if event.type == QUIT:
print("exit")
exit()
# 鍵盤按下事件
elif event.type == KEYDOWN:
# 按下a或←,呼叫玩家飛機向左移動方法
if event.key == K_a or event.key == K_LEFT:
print("left")
aircraft_temp.move_left()
# 按下d或→,,呼叫玩家飛機發射子彈方法
elif event.key == K_d or event.key == K_RIGHT:
print("right")
aircraft_temp.move_right()
elif event.key == K_w or event.key == K_UP:
print("up")
aircraft_temp.move_up()
# 按下d或→,,呼叫玩家飛機發射子彈方法
elif event.key == K_s or event.key == K_DOWN:
print("down")
aircraft_temp.move_down()
# 按下空格,呼叫玩家飛機發射子彈方法
elif event.key == K_SPACE:
print("space")
aircraft_temp.fire()
# 得分系統
def score():
pass
# 主函式
def main():
pygame.init()
pygame.display.set_caption("飛機大戰")
screen = pygame.display.set_mode((420,700), 0, 32)
background = pygame.image.load('bj.png')
# 建立玩家飛機物件
aircraft = Aircraft_obj(screen)
enemy = EnemyPlane(screen,0,0)
enemy2 = EnemyPlane(screen,100,0)
# 遊戲音效
sound = pygame.mixer.Sound("時光洪流.mp3")
sound.play()
pygame.mixer.music.load("時光洪流.mp3")
pygame.mixer.music.play(-1, 0.0)
while True:
screen.blit(background, (0, 0))
aircraft.display()
# 遍歷子彈,找到座標,判斷是否與敵機相逢
for bullet in aircraft.bullet_list:
x1 = bullet.x
x2 = bullet.x+22
y1 = bullet.y
enemy.blast(x1, x2, y1)
enemy.display()
enemy2.display()
enemy.move()
enemy2.move()
pygame.display.update()
key_control(aircraft)
# 暫停0.01秒
time.sleep(0.01)
main()