猫史档案馆


【Python作品分享】消灭病毒,飞机大战【作品秀】

用户:Liu_HaiLiu_Hai查看:0 回复:1 评论:0 创建时间:2023-01-01T17:08:34


【作品展示】

center_image

 

【作品介绍】

这是个游戏,W或D,←或→,空格操作。

制作不易,请各位大佬三连🤗🤗😘。

作者:艾弗森脚裸终结者(Python,C++,Java,C等语言的编程大神,比尔•盖茨的忠实粉丝)。

 

【作品源代码】

# 导入pygame库
import pygame
import sys
import time
import random
from loadImg import *



# 初始化窗口
pygame.init()
screen = pygame.display.set_mode([480, 670])

iconImg = pygame.image.load('飞机大战图片/飞机.png')
pygame.display.set_icon(iconImg)
pygame.display.set_caption('超级消灭喵')

bulletList = []
virusList = []
ADDVIRUS = 24
pygame.time.set_timer(ADDVIRUS, 800)
# 病毒信息,函数保存


def getVirus():
    vrect = virusImg.get_rect()
    x = random.randint(0, 480 - vrect.width)
    vrius = {'img': virusImg, 'x': x, 'y': -vrect.height,
             'width': vrect.width, 'height': vrect.height, 'state': True}
    return vrius


# 飞机信息
prect = planeImg.get_rect()
plane = {'img': planeImg, 'x': 200, 'y': 670 - prect.height,
         'width': prect.width, 'height': prect.height}


# 子弹信息
def getBullet():
    zidan = bulletImg.get_rect()
    x = plane['x'] + plane['width'] / 2
    y = plane['y'] - zidan.height
    bullet = {'img': bulletImg, 'x': x, 'y': y,
              'width': zidan.width, 'height': zidan.height, 'state': True}
    return bullet


# 检测病毒是否碰到了飞机
def checkHit(v, p):
    top = p['y'] - v['height']
    bottom = p['y'] + p['height']
    left = p['x'] - v['width']
    right = p['x'] - v['width']

    if v['x'] >= left and v['y'] <= right:
        if v['y'] >= top and v['y'] <= bottom:
            return True

# 绘制函数


def draw():
    screen.blit(bgImg, [0, 0])
    for virus in virusList:
        screen.blit(virus['img'], [virus['x'], virus['y']])
    screen.blit(plane['img'], [plane['x'], plane['y']])
    for bullet in bulletList:
        screen.blit(bullet['img'], [bullet['x'], bullet['y']])


# 移动函数
def move():
    for vrius in virusList:
        vrius['y'] += 1
    for bullet in bulletList:
        bullet['y'] -= 2


# 事件的判断函数
def eventListen():
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == ADDVIRUS:
            virusList.append(getVirus())
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                plane['x'] -= 20
                if plane['x'] < 0:
                    plane['x'] = 0
            elif event.key == pygame.K_a:
                plane['x'] -= 20
                if plane['x'] < 0:
                    plane['x'] = 0
            elif event.key == pygame.K_RIGHT:
                plane['x'] += 20
                if plane['x'] > 480 - plane['width']:
                    plane['x'] = 480 - plane['width']
            elif event.key == pygame.K_d:
                plane['x'] += 20
                if plane['x'] > 480 - plane['width']:
                    plane['x'] = 480 - plane['width']
            elif event.key == pygame.K_SPACE:
                bulletList.append(getBullet())

# 删除屏幕外的病毒


def delete():
    global virusList, bulletList
    new_virusList = []
    for virus in virusList:
        if virus['y'] < 670 and virus['state']:
            new_virusList.append(virus)
    virusList = new_virusList

    new_bulletList = []
    for bullet in bulletList:
        if bullet['y'] > -bullet['height'] and bullet['state']:
            new_bulletList.append(bullet)
    bulletList = new_bulletList


# 证实是否飞机碰到了病毒
def check():
    for virus in virusList:
        if checkHit(virus, plane):
            pygame.time.set_timer(ADDVIRUS, 0)
            print("GameOver")
    for virus in virusList:
        for bullet in bulletList:
            if checkHit(virus, bullet):
                virus['state'] = False
                bullet['state'] = False
                break


# 主循环
while True:
    eventListen()
    draw()
    pygame.display.update()
    move()
    delete()
    check()
    pygame.display.update()


#谢谢观看

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
虚伪_der_面具虚伪_der_面具

哇哦

点赞0


评论