欢迎光临散文网 会员登陆 & 注册

陨石掉落,从零开始用Python制作飞机大战【教程第4集】

2023-07-28 15:07 作者:Scratch_V同学  | 我要投稿

turtle库的飞机大战:简单好理解,

完全自制,可打包




from turtle import Screen, Turtle

import time,random

import winsound

import os


screen = Screen()


screen.register_shape(os.path.join('rock.gif'))

screen.register_shape(os.path.join('player__p.gif'))

screen.register_shape(os.path.join('bullet.gif'))

screen.register_shape(os.path.join('background.gif'))


screen.bgpic(os.path.join('background.gif'))

screen.setup(600, 800)

screen.bgcolor('white')

screen.tracer(0)


player = Turtle()

player.penup()

player.goto(0,-360)

player.shape(os.path.join('player__p.gif'))


zd = Turtle()

zd.color('red')

zd.pu()

zd.seth(90)

zd.hideturtle()

zd.shape(os.path.join('bullet.gif'))


class Scoreboard(Turtle):

    def __init__(self):

        super().__init__()

        self.score = 0

        self.penup()

        self.goto(0,250)

        self.pencolor('black')

        self.score_write()

        self.hideturtle()

    def score_write(self):

                self.clear()

                self.write(f'Your scord: {self.score}',align='center',font=('Arial',25,'normal'))

    def score_(self):

        self.score += 1

        self.score_write()


scoreboard = Scoreboard()


def dis(a,b,c):

    if a.distance(b) < c:

        return True

    else:

        return False

def left():

    player.goto(player.xcor()-8,player.ycor())

def right():

    player.goto(player.xcor()+8,player.ycor())

def fs():      

    zd.goto(player.xcor(),-360)

    winsound.PlaySound(os.path.join("fire.wav"), winsound.SND_ASYNC)

    zd.showturtle()


a = []


screen.listen()

screen.onkey(left,'Left')

screen.onkey(right,'Right')

screen.onkey(fs,'space')


run = True

while run:

    screen.update()

    if random.randint(1,300) == 1:

        new = Turtle()

        new.shape(os.path.join('rock.gif'))

        new.shapesize(1,1)

        new.pu()

        new.seth(270)

        new.goto(random.randint(-290,290),390)

        a.append(new)

    for a_i in a:

        a_i.goto(a_i.xcor(),a_i.ycor() - 0.5)

        if dis(a_i,zd,20):

            a_i.hideturtle()

            zd.hideturtle()

            zd.goto(-30000,280)

            winsound.PlaySound(os.path.join("hit.wav"), winsound.SND_ASYNC)

            a_i.goto(-3000,290)

            scoreboard.score_()

        if dis(a_i,player,50):

            winsound.PlaySound(os.path.join("end.wav"), winsound.SND_ASYNC)

            time.sleep(2)

            run = False

        a_i.left(random.randint(10,30) / 100)

    zd.fd(1)

    if zd.xcor() > 390:

        zd.hideturtle()

陨石掉落,从零开始用Python制作飞机大战【教程第4集】的评论 (共 条)

分享到微博请遵守国家法律