专栏/海龟大战(飞机大战)python代码(不用pygame)

海龟大战(飞机大战)python代码(不用pygame)

2024年05月02日 04:30--浏览 · --点赞 · --评论
粉丝:33文章:4

import turtle as t

import random as r


screen=t.Screen()

screen.title('飞机大战')

screen.setup(800,800)

screen.tracer(False)


wo=t.Turtle()

wo.shape('turtle')

wo.shapesize(2)

wo.penup()

wo.setx(-300)

write=t.Turtle()

write.hideturtle()

write.pencolor('red')

write.penup()



bullets=[]


enemise=[]

for i in range(20):

    di=t.Turtle()

    di.shape('square')

    di.penup()

    di.goto(r.randint(400,600),r.randint(-400,400))

    enemise.append(di)

score=write.clone()

score.sety(340)

score.count=0






def space():

    if len(bullets)<3:

        b=t.Turtle()

        b.shape('circle')

        b.penup()

        b.goto(wo.pos())

        bullets.append(b) 


def up ():

    wo.sety(wo.ycor()+10)

def down ():

    wo.sety(wo.ycor()-10)

screen.onkey(up,'Up')

screen.onkey(down,'Down')

screen.onkey(space,'space')

screen.listen()

run=True

while run:

    for di in enemise:

        di.setx(di.xcor()-5)

        if di.xcor()<-400:

            di.goto(r.randint(400,600),r.randint(-400,400))


    for b in bullets:

        b.setx(b.xcor()+3)

        if b.xcor() > 400:

            bullets.remove(b)

        

    r_=24

    R=24

    x1,y1=wo.xcor(),wo.ycor()

    for di in enemise:

        x2,y2=di.xcor(),di.ycor()

        d=(x1-x2)**2+(y1-y2)**2

        d=d**0.5

        if d <= r_+R:

            run=False

            

    r_=16

    for b in bullets:

        x1,y1=b.xcor(),b.ycor()

        for di in enemise:

            x2,y2=di.xcor(),di.ycor()

            d=(x1-x2)**2+(y1-y2)**2

            d=d**0.5

            if d<=r_+R:

                b.hideturtle()

                di.hideturtle()

                bullets.remove(b)

                enemise.remove(di)

                score.count+=1

                score.clear()

                

                

                

                break

            

        

    screen.update()

    


投诉或建议