第一步,增加游戏状态,炸弹(个人思路,大家不必模仿)爆炸状态
# coding:utf-8
BG_pos_y = 0
#游戏状态
game = "RUNNING"
#炸弹状态
isbong=False
第二步,导入需要的库文件,并且导入图片,创建游戏窗口
import tkinter as tk
import pygame
import sys
import os
import easygui
import random
import time
import asyncio
from pygame.locals import *
# 创建窗口
pygame.init()
# 窗口定位
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (160, 100)
# 设置一个长宽窗口
canvas = pygame.display.set_mode((1250, 600))
canvas.fill([255, 255, 255])
# 设置窗口标题
pygame.display.set_caption("太空大战")
# 加载图片
button = pygame.image.load("images/anniu.png")
BgTk = pygame.image.load("images/yz.jpg")
BgFc = pygame.image.load("images/bg-fc.jpeg")
f = pygame.image.load("images/fc.png")
fd = pygame.image.load("images/feidie.png")
bullet = pygame.image.load("images/bullet1.png")
win = pygame.image.load("images/win.png")
lose = pygame.image.load("images/lose.png")
bomb = pygame.image.load("images/zd.png")
boss=pygame.image.load("images/enemy3_1.png")
boss_img=pygame.image.load("images/bg_boss.png")
第三步,改变图片的大小和方向
button = pygame.transform.scale(button, (150, 150))
BgTk = pygame.transform.scale(BgTk, (1250, 600))
BgFc = pygame.transform.scale(BgFc, (1250, 600))
fc = pygame.transform.scale(f, (100, 100))
fc = pygame.transform.rotate(fc, 265)
fd = pygame.transform.scale(fd, (80, 30))
bullet = pygame.transform.rotate(bullet, 270)
win = pygame.transform.scale(win, (1250, 600))
lose = pygame.transform.scale(lose, (1250, 600))
bomb = pygame.transform.scale(bomb,(60,60))
boss_img=pygame.transform.scale(boss_img,(1250,600)
第四步,创建分数变量以及生命值变量
# 分数
score = 0
# 生命值
hero = 3
第五步,创建写文字方法,用于在游戏窗口显示生命值和分数
def fillText(text, position, size):
TextFont = pygame.font.Font('images/WRYH.ttf', size)
newText = TextFont.render(text, True, (255, 255, 255))
canvas.blit(newText, position)
第六步,创建时间间隔方法
def isActionTime(lastTime, interval):
if lastTime == 0:
return True
currentTime = time.time()
return currentTime - lastTime >= interval
第七步,创建玩家类,天空背景类,Npc类,子弹类,炸弹类
# 玩家类
class Player():
def __init__(self, x, y):
self.x = x
self.y = y
self.width = 100
self.height = 100
self.img = fc
def drawplayer(self):
canvas.blit(self.img, (self.x, self.y))
# 天空背景类
class Sky():
def __init__(self):
self.y = 0
self.x1 = 0
self.x2 = -1250
self.img = BgTk
self.speed = 1
def draw_sky(self):
canvas.blit(self.img, (self.x1, self.y))
canvas.blit(self.img, (self.x2, self.y))
def move(self):
self.x1 -= self.speed
self.x2 -= self.speed
if self.x1 <= -1250:
self.x1 = 1250
if self.x2 <= -1250:
self.x2 = 1250
# 创建Npc类,绘制飞碟
class Npc():
def __init__(self,y,img):
self.x = 1250
self.y = y
self.width = 80
self.height = 30
self.img = img
self.speed=2
def draw(self):
canvas.blit(self.img, (self.x, self.y))
def move(self):
self.x -= self.speed
#创建子弹类
class Bullte():
def __init__(self):
self.x = player.x + player.width
self.y = player.y + 40
self.width = 9
self.height = 21
self.img = bullet
def draw(self):
canvas.blit(self.img, (self.x, self.y))
def move(self):
self.x += 5
#炸弹类
class Bong():
def __init__(self,y):
self.x=1330
self.y=y
self.width=80
self.height=80
self.img=bomb
def draw_bomb(self):
canvas.blit(self.img,(self.x,self.y))
def move(self):
self.x-=2
第八步,创建飞碟,子弹列表,玩家,北京,飞碟实例化,定义五个时间间隔,分别用于飞碟,子弹等。
#飞碟列表
npcs = []
#创建子弹类列表
bulltes = []
#创建炸弹类列表
bongs=[]
#玩家类的实例化
player = Player(0, 0)
#背景类的实例化
sky = Sky()
#boss飞碟类的实例化
boss_y=random.randint(0,431)
nps=Npc(boss_y,boss)
#设置生命值与键盘按下状态
isSpace=False
bosshero=30
#定义时间间隔
lasttime=0
lasttime2 = time.time()
lasttime3 = time.time()
lasttime4 = time.time()
lasttime