猫史档案馆


【Python作品分享】飞机大战【教程贴】

用户:羞涩的花卷卷CAgv羞涩的花卷卷CAgv查看:2 回复:1 评论:2 创建时间:2021-03-21T16:13:15


【作品展示】

center_image

 

【作品介绍】

一个用python做的特别简单的飞机大战。

 

【作品源代码】

#2283918618
import pgzrun
import random

WIDTH = 500
HEIGHT = 1000
TITLE = 'Python飞机大战'

background1 = Actor('background')
background1.x = WIDTH/2
background1.y = HEIGHT/2
background2 = Actor('background')
background2.x = WIDTH/2
background2.y = -HEIGHT/2

hero = Actor('hero')
hero.x = WIDTH/2
hero.y = HEIGHT*2/3


bullet = Actor('bullet')
bullet.x = WIDTH/2
bullet.y = -HEIGHT

enemy = Actor('enemy')
enemy.x = WIDTH/2
enemy.y = 0
enemy2 = Actor('enemy2')
enemy2.x = WIDTH/3
enemy.y = 0

score = 0

isLoose = False

live = 3

def draw():
    background1.draw()
    background2.draw()
    hero.draw()
    bullet.draw()
    enemy.draw()
    enemy2.draw()
    screen.draw.text("score:"+str(score), (200, 10),
                     fontsize=30, color='green')
    screen.draw.text("live:"+str(live), (20, 10),
                     fontsize=30, color='black')

    if isLoose:
        screen.draw.text("game over!",(50,HEIGHT/2),fontsize=90,color='red')

def on_mouse_move(pos,rel,buttons):
    if isLoose:
        return
    hero.x = pos[0]
    hero.y = pos[1]

def on_mouse_down():
    if isLoose:
        return
    bullet.x = hero.x
    bullet.y = hero.y - 70

def update():
    global score,isLoose,live
    if score >=30:
        enemy2.y += 5
        enemy.y += 5

    if hero.colliderect(enemy):
        live = live -1
        hero.y = 800
        hero.x = WIDTH/2
        enemy.y = 100

    if hero.colliderect(enemy2):
        live = live - 1
        hero.y = 800
        hero.x = WIDTH/2
        enemy.y = 100

    if live <= 0:
        isLoose = True

    if isLoose:
        return

    #以下代码为背景循环滚动
    if background1.y > 700/2 + 700:
        background1.y = -700/2
    if background2.y > 700/2 + 700:
        background2.y = -700/2
    background1.y += 3
    background2.y += 3

    if bullet.y > -HEIGHT:
        bullet.y = bullet.y - 20

    enemy.y +=3
    if enemy.y > HEIGHT:
        enemy.y = 0
        enemy.x = random.randint(50,WIDTH-50)

    enemy2.y += 3
    if enemy2.y > HEIGHT:
        enemy2.y = 0
        enemy2.x = random.randint(50, WIDTH-50)
    #以下为碰撞检测
    if bullet.colliderect(enemy):
        enemy.y = 0
        enemy.x = random.randint(0,WIDTH)
        score = score + 1
        bullet.y = -HEIGHT
    if bullet.colliderect(enemy2):
        enemy2.y = 0
        enemy2.x = random.randint(0, WIDTH)
        score = score + 1
        bullet.y = -HEIGHT


pgzrun.go()

 

【提示】

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

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
CodekpyCodekpy

我跟你讲,我的海龟编辑器特别坑,因为老是下载不了第三方库

点赞0


评论