猫史档案馆


【Python作品分享】飞机大战-4

用户:调皮的暴风雀-k0p调皮的暴风雀-k0p查看:0 回复:0 评论:0 创建时间:2021-07-30T22:23:37


【作品展示】

center_image

 

【作品介绍】

wasd控制飞机移动

j发射子弹

 

【作品源代码】

import pgzrun
from random import randint

# 新建变量
WIDTH = 480
HEIGHT = 喵0
game_state = 'start'
score = 0
plane_speed = 10
enemy_speed = 5
喵_speed = 5
blood = 1
shoot = False
count = 0

# 设置角色初始位置
plane = Actor("me1",(240, 500))
enemy = [Actor("enemy1"), Actor("enemy1"), Actor("enemy1")]
bullet = Actor("bullet1", (0, -100))
喵 = Actor("喵",(300, -200))
for i in range(3):
    enemy[i].x = 150 * i
    enemy[i].y = -200 * i

# 设置开始界面
def draw():
    global game_state, score, shoot
    if game_state == 'start':
        screen.blit("background",(0,0))
        screen.draw.text("Plane Fighting", (WIDTH / 2 - 120, 40), fontsize=50)
        screen.draw.text("Press SPACE to start", (WIDTH / 2 - 120, 100), fontsize=40)
        if keyboard.space:
            game_state = 'play'
    elif game_state == 'play':
        screen.blit("background", (0, 0))
        screen.draw.text("Score:" + str(score), (10, 10), fontsize=50)
        plane.draw()
        bullet.draw()
        喵.draw()
        for i in range(3):
            enemy[i].draw()
    else:
        screen.blit("background", (0, 0))
        screen.draw.text("Game Over", (WIDTH / 2 - 120, 40), fontsize=50)
        screen.draw.text("Your Score:" + str(score), (WIDTH / 2 - 120, 100), fontsize=40)
        plane.draw()
        bullet.draw()
        喵.draw()


def plane_object(obj):
    obj.x = randint(60,420)
    obj.y = -100

def change():
    global count
    if count == 9:
        count = 0
        if plane.image == "me1":
            plane.image = "me2"
        else:
            plane.image = "me1"
    else:
        count += 1
    
# 游戏主程序
def update():
    global game_state, shoot, score, blood, init_pos
    if game_state == 'play':
        
        for i in range(3):
            enemy[i].y += enemy_speed
            if enemy[i].y > HEIGHT + 100:
                plane_object(enemy[i])
            if enemy[i].colliderect(bullet):
                plane_object(enemy[i])
                score += 10
            if plane.colliderect(enemy[i]):
                game_state = 'game over'
        喵.y += 喵_speed
        if 喵.y > HEIGHT + 50:
            plane_object(喵)
        if 喵.colliderect(plane):
            plane_object(喵)
            blood += 1
        change()
        if keyboard.a:
            plane.x -= plane_speed
        if keyboard.d:
            plane.x += plane_speed
        if keyboard.w:
            plane.y -= plane_speed
        if keyboard.s:
            plane.y += plane_speed
        if keyboard.j:
            shoot = True
            init_pos = plane.x, plane.y - 10
            bullet.pos = init_pos[0], init_pos[1]
        if shoot:
            if bullet.y > -20:
                bullet.y -= 20
            else:
                shoot = False
                bullet.pos = (0, -100)

pgzrun.go()

 

【提示】

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

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页