import pygame
import math
import os, sys
from pygame.constants import K_e
os.chdir(sys.path[0])
from random import randint
import random
pygame.init()
screen = pygame.display.set_mode((480, 700))
done = True
class Plane():
def __init__(self,x,y,imagefile):
self.x=x
self.y=y
image=pygame.image.load(imagefile)
self.icon = pygame.transform.scale(image,(80,80))
def move(self,fx):
if fx=='left':
self.x=self.x-2
elif fx=='right':
self.x=self.x+2
elif fx == 'up':
self.y -= 2
elif fx == 'down':
self.y += 2
else:
pass
class Bullet():
def __init__(self,x,y,imagefile):
self.x=x
self.y=y
self.icon = pygame.image.load(imagefile).convert_alpha()
def move(self):
self.y=self.y-3
screen.blit(self.icon,(self.x,self.y))
def __del__(self):
del self
class Enemy():
def __init__(self,x,y,step,imagefile):
self.x=x
self.y=y
self.step=step
self.icon = pygame.image.load(imagefile).convert_alpha()
def move(self):
self.y=self.y+self.step
screen.blit(self.icon,(self.x,self.y))
def __del__(self):
del self
class EnemyBoss:
def __init__(self,x,y,step,imagefile):
self.x=x
self.y=y
self.step=step
self.icon = pygame.image.load(imagefile).convert_alpha()
def move(self):
self.y=self.y+self.step
screen.blit(self.icon,(self.x,self.y))
def back(self):
self.y-=self.step
screen.blit(self.icon,(self.x,self.y))
class Coin:
def __init__(self,x,y,imagefile):
self.x=x
self.y=y
self.step=1
self.icon = pygame.image.load(imagefile).convert_alpha()
def move(self):
self.y += self.step
screen.blit(self.icon,(self.x,self.y))
def initdestrop():
for i in range(10):
enemy=Enemy(randint(50,200),10,random.random(),r'images/enemy1_down2.png')
destroy_list.append(enemy)
def PlayerBorken(x,y,lenlife):
if lenlife == 2:
borken1 = Plane(0,0,r"images/me_destroy_1.png")
screen.blit(borken1.icon,(x,y))
if lenlife == 1:
borken2 = Plane(0,0,r"images/me_destroy_2.png")
screen.blit(borken2.icon,(x,y))
if lenlife == 0:
borken3 = Plane(0,0,r"images/me_destroy_3.png")
screen.blit(borken3.icon,(x,y))
sys.exit()
def ChangePlayer(x,y,Changekey):
if Changekey:
SecondLooked = Plane(0,0,r"images/me3.jpg")
screen.blit(SecondLooked.icon,(x,y))
def distance(x1,y1,x2,y2):
p1 = (x1-x2)**2
p2 = (y1-y2)**2
return math.sqrt(p1+p2)
life = 20
font = pygame.font.Font(r"images/Gill.ttf",32)
class PlayLife:
def __init__(self,x,y,imagefile):
self.x=x
self.y=y
image=pygame.image.load(imagefile)
self.icon = pygame.transform.scale(image,(40,40))
def show_life(self):
screen.blit(self.icon,(self.x,self.y))
score = 0
def show_Score():
text = f"Score:{score}"
score_render = font.render(text,True,(255,0,0))
screen.blit(score_render,(10,650))
def ShowMoney():
text = f"Money:{MyMoney}"
Money_render = font.render(text,True,(255,255,0))
screen.blit(Money_render,(350,10))
import time
is_newcoin = False
myPlane=None
bullet=None
bullet_list=list()
enemy_list=list()
destroy_list=list()
destroy_list=[]
Enemybosslist=[]
playlifelist=[]
bullet_listForenemys = []
Coinlist = []
jishi=0
initdestrop()
HitCount = 0
scoreCopy=0
scoreCopy2=0
MyMoney = 0
StartTime = 0
myPlane=Plane(240,600,r'images\me1.png')
playlife = PlayLife(360,650,r"images/life.png")
playlife2 = PlayLife(400,650,r"images/life.png")
playlife3 = PlayLife(440,650,r"images/life.png")
playlifelist.append(playlife)
playlifelist.append(playlife2)
playlifelist.append(playlife3)
is_Change = False
def is_exit():
game_over = pygame.image.load("images/GameisOver.png")
screen.blit(game_over, (0, 0))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
game_restart = pygame.image.load("images/again.png")
screen.blit(game_restart, (100, 500))
game_quit = pygame.image.load("images/gameover.png")
screen.blit(game_quit, (100, 600))
if pygame.mouse.get_pressed()[0]:
# 提取x、y
x, y = pygame.mouse.get_pos()
# 判断是否点击了"重新开始"
if 100 <= x <= 400 and 500 <= y <= 540:
break
elif 100 <= x <= 400 and 600 <= y <= 640:
sys.exit()
pygame.display.update()
while done:
jishi=jishi+1
if jishi%1000 == 0:
coin = Coin(randint(50,430),0,r'images/coin1.png')
Coinlist.append(coin)
if jishi%500==0:
enemy=Enemy(randint(50,430),0,random.random(),r'images/enemy1.png')
enemy_list.append(enemy)
if scoreCopy==10:
scoreCopy=0
enemyboss = EnemyBoss(randint(50,400),0,0.5,r'images/enemy2.png')
Enemybosslist.append(enemyboss)
if scoreCopy2==5 and len(playlifelist) < 3:
scoreCopy2=0
playlifelist.append(PlayLife(SaveLife,650,r"images/life.png"))
image = pygame.image.load(r'images\background.png').convert_alpha()
screen.blit(image,(0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type==pygame.KEYUP:
if event.key==pygame.K_LCTRL:
if not is_newcoin:
bullet_list = []
bullet=Bullet(myPlane.x+(myPlane.icon.get_width())/2,myPlane.y-10,r'images\bullet1.png')
else:
bullet_list = []
bullet=Bullet(myPlane.x+(myPlane.icon.get_width())/2,myPlane.y-10,r'images\bullet2.png')
bullet_list.append(bullet)
keyvalue = pygame.key.get_pressed()
if keyvalue[pygame.K_LEFT] and myPlane.x >= 0:
myPlane.move('left')
if keyvalue[pygame.K_RIGHT] and myPlane.x <= 400:
myPlane.move('right')
if keyvalue[pygame.K_UP] and myPlane.y >= 0:
myPlane.move('up')
if keyvalue[pygame.K_DOWN] and myPlane.y <= 615:
myPlane.move('down')
if keyvalue[pygame.K_g]:
StartTime = jishi
if keyvalue[pygame.K_e] and MyMoney >= 300:
MyMoney = 0
is_newcoin = True
if keyvalue[pygame.K_r]:
is_Change = True
if keyvalue[pygame.K_q]:
is_exit()
if jishi - StartTime < 400 and StartTime!=0:
ShowMoney()
for i,item in enumerate(Coinlist):
item.move()
if abs(myPlane.x+(myPlane.icon.get_width())/2-item.x)<(myPlane.icon.get_width()/2+item.icon.get_width()/2) and abs(myPlane.y+myPlane.icon.get_height()/2-item.y)<(myPlane.icon.get_height()/2+item.icon.get_height()/2):
del Coinlist[i]
MyMoney += 20
for i,item in enumerate(enemy_list):
item.move()
if distance(item.x,item.y,myPlane.x,myPlane.y) < 30:
playlifelist.pop()
scoreCopy2=0
del enemy_list[i]
if item.y>650:
for baozhao in destroy_list:
screen.blit(baozhao.icon,(item.x,item.y))
del enemy_list[i]
del item
else:
pass
for i,item in enumerate(Enemybosslist):
item.move()
if distance(item.x,item.y,myPlane.x,myPlane.y) < 40:
playlifelist.pop()
playlifelist.pop()
del Enemybosslist[i]
if item.y > 650:
for baozhao in destroy_list:
screen.blit(baozhao.icon,(item.x,item.y))
for i,item in enumerate(bullet_list):
item.move()
if item.y<=0:
del bullet_list[i]
else:
for e,enemy in enumerate(enemy_list):
if abs(enemy.x+(enemy.icon.get_width())/2-item.x)<(enemy.icon.get_width()/2+item.icon.get_width()/2) and abs(enemy.y+enemy.icon.get_height()/2-item.y)<(enemy.icon.get_height()/2+item.icon.get_height()/2):
for baozhao in destroy_list:
screen.blit(baozhao.icon,(enemy.x,enemy.y))
MyMoney+=10
score+=1
scoreCopy+=1
scoreCopy2+=1
del enemy_list[e]
del e
del bullet_list[i]
for k,killer in enumerate(Enemybosslist):
if HitCount==3:
HitCount=0
score+=10
del Enemybosslist[k]
del k
del bullet_list[i]
if abs(killer.x+(killer.icon.get_width())/2-item.x)<(killer.icon.get_width()/2+item.icon.get_width()/2) and abs(killer.y+killer.icon.get_height()/2-item.y)<(killer.icon.get_height()/2+item.icon.get_height()/2):
HitCount+=1
del bullet_list[i]
screen.blit(myPlane.icon,(myPlane.x,myPlane.y))
PlayerBorken(myPlane.x,myPlane.y,len(playlifelist))
for l,liem in enumerate(playlifelist):
liem.show_life()
if len(playlifelist) > 0:
SaveLife = playlifelist[-1].x + 40
show_Score()
pygame.display.update()
|