IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> HC PlaneGame -> 正文阅读

[游戏开发]HC PlaneGame

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()

  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章      下一篇文章      查看所有文章
加:2021-11-14 22:03:23  更:2021-11-14 22:06:33 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/27 22:35:00-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码