import pygame
from pygame.locals import*
import random
import time
class herobullet():
def __init__(self,x,y,windows):
self.x=x
self.y=y
self.pic=pygame.image.load("e:\\學習筆記\\day18\\img\\bullet2.png")
self.windows=windows
def draw(self):
self.windows.blit(self.pic,(self.x,self.y))
self.move()
def move(self):
self.y-=5
class enemybullet():
def __init__(self,x,y,windows):
self.x=x
self.y=y
self.pic=pygame.image.load("e:\\學習筆記\\day18\\img\\bullet3.png")
self.windows=windows
def draw(self):
self.windows.blit(self.pic,(self.x,self.y))
self.move()
def move(self):
self.y+=5
windows=pygame.display.set_mode((600,800),0,32)
bg=pygame.image.load("e:\\學習筆記\\day18\\img\\background2.jpeg")
pygame.display.set_caption("打灰機")
icon=pygame.image.load("e:\\學習筆記\\day18\\img\\plane.png")
pygame.display.set_icon(icon)
heroplane1=pygame.image.load("e:\\學習筆記\\day18\\img\\feiji1.png")
heroplane2=pygame.image.load("e:\\學習筆記\\day18\\img\\feiji2.png")
heroplanex=600//2-117//2
heroplaney=800-80
enemyplane=pygame.image.load("e:\\學習筆記\\day18\\img\\feiji3.png")
enemyplanex=600//2-100//2
enemyplaney=0
herobomblist=["e:\\學習筆記\\day18\\img\\feiji1bomb1.png",
"e:\\學習筆記\\day18\\img\\feiji1bomb2.png",
"e:\\學習筆記\\day18\\img\\feiji1bomb3.png"]
enemybomblist=["e:\\學習筆記\\day18\\img\\feiji3bomb1.png",
"e:\\學習筆記\\day18\\img\\feiji3bomb2.png",
"e:\\學習筆記\\day18\\img\\feiji3bomb3.png"]
pygame.key.set_repeat(5,10)
direct="左"
herobiulist=
enemybiulist=
heroindexshift=0
hero_isbomb=false
enemy_isbomb=false
hero_bombindex=0
enemy_bombindex=0
hero_hp=100
enemy_hp=2000
while true:
windows.blit(bg,(0,0))
if hero_isbomb==false:
if heroindexshift==0:
windows.blit(heroplane1,(heroplanex,heroplaney))
heroindexshift=1
elif heroindexshift==1:
windows.blit(heroplane2,(heroplanex,heroplaney))
heroindexshift=0
else:
if hero_bombindex==len(herobomblist):
time.sleep(1)
exit(0)
pic=pygame.image.load(herobomblist[hero_bombindex])
windows.blit(pic,(heroplanex,heroplaney))
hero_bombindex+=1
time.sleep(1)
if enemy_isbomb==false:
windows.blit(enemyplane,(enemyplanex,enemyplaney))
else:
if enemy_bombindex==len(enemybomblist):
time.sleep(1)
exit(0)
pic=pygame.image.load(enemybomblist[enemy_bombindex])
windows.blit(pic,(enemyplanex,enemyplaney))
enemy_bombindex+=1
time.sleep(1)
for biu in herobiulist:
biu.draw()
herobiulist.remove(biu) if biu.y<0 else ""
for biu in enemybiulist:
biu.draw()
enemybiulist.remove(biu) if biu.y>800 else ""
for event in pygame.event.get():
if event.type==quit:
print("關閉了")
exit(0)
elif event.type==keydown:
if event.key==k_left:
heroplanex=heroplanex-10 if heroplanex>10 else 0
elif event.key==k_right:
heroplanex=heroplanex+10 if heroplanex<600-117-10 else 600-117
elif event.key==k_up:
heroplaney=heroplaney-10 if heroplaney>10 else 0
elif event.key==k_down:
heroplaney=heroplaney+10 if heroplaney<800-80-10 else 800-80
elif event.key==k_space:
zerobiu=herobullet(heroplanex,heroplaney-71,windows)
onebiu=herobullet(heroplanex+117-14,heroplaney-71,windows)
if direct=="左":
enemyplanex-=5
if enemyplanex==0:
direct="右"
elif direct=="右":
enemyplanex+=5
if enemyplanex==600-100:
direct="左"
x=random.randint(0,30)
if x==10:
twobiu=enemybullet(enemyplanex+100//2-9/2,enemyplaney+76+21,windows)
enemyrect=rect(enemyplanex,enemyplaney,100,76)
for biu in herobiulist:
biurect=rect(biu.x,biu.y,14,71)
if biurect.colliderect(enemyrect):
enemy_hp-=10
herobiulist.remove(biu)
herorect=rect(heroplanex+30,heroplaney,57,80)
for biu in enemybiulist:
biurect=rect(biu.x,biu.y,9,21)
if biurect.colliderect(herorect):
hero_hp-=20
enemybiulist.remove(biu)
if hero_hp==0:
hero_isbomb=true
if enemy_hp==0:
enemy_isbomb=true
pygame.display.update()
飛機大戰基礎
include include include include 函式外部全域性變數定義 int position x,position y 飛機位置 int bullet x,bullet y 子彈位置 int enemy x,enemy y 敵機位置 int high,width 遊戲畫面尺寸 i...
Python飛機大戰
玩家飛機可以移動,可以發射子彈,敵機隨機產生,自由墜落。未新增擊落敵機的功能。主要用來練習類的封裝與繼承。coding utf 8 import pygame,random from pygame.locals import class base object def init self,x,y,s...
Python 飛機大戰
新建專案 在選中主韙專案的情況下,tab鍵,會自動的出來乙個下級分支 在選中當前分支的情況五,點enter,會出來乙個同級的分支 第一次背景精靈組,呼叫 update方法。結果就是,背景精靈組中的所有背景物件,呼叫自身的update方法 初始化的時候,生成了 兩個背景物件 並且,建立了乙個背景圖精靈...