Python程序:用Pygame Zero做雷霆战机小游戏

import pgzrun
from pgzhelper import * 
import random

WIDTH=500
HEIGHT=500

MyPlane=Actor('plane',(WIDTH/2, HEIGHT/2))
Enemies=[]
bullets=[]
game_over = False 

def draw():
    global Enemies, game_over
    screen.clear()
    MyPlane.draw()
    for Enemy in Enemies:
        Enemy.draw()
    for bullet in bullets:
        bullet.draw()

    # 游戏结束弹窗
    if game_over:
        # 半透明遮罩层
        screen.draw.filled_rect(
            Rect(0, 0, WIDTH, HEIGHT),(0, 0, 0, 200)  # RGBA最后一位是透明度(0-255)
        )
        screen.draw.text(
            "GAME OVER",
            center=(WIDTH//2, HEIGHT//2),
            fontsize=60,
            color="red",
            gcolor="yellow"  # 渐变颜色
        )

def update():
    global MyPlane,Enemies, game_over, bullets

    if random.randint(1, 60) == 1:
        spawn_enemy()
    for Enemy in Enemies[:]:
        Enemy.y += 1
        if Enemy.top > HEIGHT:
            Enemies.remove(Enemy)

    shoot_bullet()
    for bullet in bullets[:]:
        bullet.y -= 5
        if bullet.bottom < 0:
            bullets.remove(bullet)
            continue

        collision_index = bullet.collidelist(Enemies)
        if collision_index != -1:
            Enemies.pop(collision_index)
            bullets.remove(bullet)

    if MyPlane.collidelist(Enemies)!=-1:
        MyPlane.image='alien_hurt'
        game_over=True

def on_mouse_move(pos):
    MyPlane.pos = pos

    MyPlane.left = max(MyPlane.left, 0)
    MyPlane.right = min(MyPlane.right, WIDTH)
    MyPlane.top = max(MyPlane.top, 0)
    MyPlane.bottom = min(MyPlane.bottom, HEIGHT)

def spawn_enemy():
    global Enemies
    Enemy=Actor('enemy',(random.uniform(0,WIDTH),0))    
    Enemy.scale=0.05
    Enemies.append(Enemy)

def shoot_bullet():
    global bullets
    bullet=Actor('bullet',MyPlane.pos)
    bullet.scale=0.01
    bullets.append(bullet)

pgzrun.go()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值