猫史档案馆


【Python作品分享】收集赛【作品秀】

用户:L0_0L0_0查看:0 回复:0 评论:0 创建时间:2021-02-11T13:13:14


【作品展示】

center_image

 

【作品介绍】

收集能量,获取胜利。

 

【作品源代码】

import time
import random
import easygui
import turtle as t
from math import ceil, floor

t.bgcolor('yellow')

head = [90, 180, 270, 360]

car1 = t.Turtle()
car1.color('blue')
car1.shape('喵')
car1.shapesize(3, 6, 3)
car1.penup()
car1.goto(random.randint(-200, 200), random.randint(-200, 200))
car1.setheading(head[random.randint(0, 3)])

car2 = t.Turtle()
car2.color('red')
car2.shape('喵')
car2.shapesize(3, 6, 3)
car2.penup()
car2.goto(random.randint(-200, 200), random.randint(-200, 200))
car2.setheading(head[random.randint(0, 3)])

electric = t.Turtle()
electric.color('green')
electric_shape = ((0, 0), (20, 0), (10, -20), (30, -20), (-10, -40), (0, -20), (-10, -20))
t.register_shape('electric', electric_shape)
electric.shape('electric')
electric.left(90)
electric.penup()
electric.hideturtle()

def see_play():
    easygui.msgbox('The red car is controlled by A, D keys, while the blue car is controlled by ←, → keys. Encounter lightning icon can score, at the same time can speed up, run out of the window of the player failed, after the end of the time, score more win.', 'How to play?', 'OK')

while car1.distance(car2) < 100:
    car1.goto(random.randint(-200, 200), random.randint(-200, 200))
    car2.goto(random.randint(-200, 200), random.randint(-200, 200))
    car1.setheading(head[random.randint(0, 3)])
    car2.setheading(head[random.randint(0, 3)])

car1.hideturtle()
car2.hideturtle()

game_started = False
start_game_text = t.Turtle()
start_game_text.write('Press SPACE to start\n\nHow to play? Press P to see', align='center', font=('Arial', 20, 'bold'))
start_game_text.hideturtle()
score_text = t.Turtle()
time_text = t.Turtle()
over = t.Turtle()
score_text.hideturtle()
score_text.penup()
over.hideturtle()
car1_score = 0
car2_score = 0

def write_time(the_time):
    if ceil(the_time) > the_time + 0.5:
        time_text.clear()
    time_text.hideturtle()
    time_text.penup()
    time_text.goto(-t.window_width() / 2 + 50, t.window_height() / 2 - 70)
    time_text.write((str(round(the_time)) + 's'), align='center', font=('Arial', 40, 'bold'))

def write_score(red, blue):
    score_text.clear()
    score_text.goto(-t.window_width() / 2 + 520, t.window_height() / 2 - 70)
    score_text.write(('red : blue = ' + str(red) + ' : ' + str(blue)), align='right', font=('Arial', 40, 'bold'))

def place_electric():
    electric.ht()
    electric.goto(random.randint(-200, 200), random.randint(-200, 200))
    electric.st()

def car1_outside_window():
    x = car1.xcor()
    y = car1.ycor()
    outside = x > t.window_width() / 2 or x < -t.window_width() / 2 or y < -t.window_height() / 2 or y > t.window_height() / 2
    return outside

def car2_outside_window():
    x = car2.xcor()
    y = car2.ycor()
    outside = x > t.window_width() / 2 or x < -t.window_width() / 2 or y < -t.window_height() / 2 or y > t.window_height() / 2
    return outside

def start_game():
    global car1_score
    global car2_score
    global game_started
    if game_started:
        return
    game_started = True
    start_time = time.time()
    car1.showturtle()
    car2.showturtle()
    start_game_text.clear()
    Time = 60 - time.time() + start_time
    write_time(Time)
    place_electric()
    write_score(0, 0)
    while True:
        Time = 60 - time.time() + start_time + 1
        write_time(Time)
        car1.forward((car1_score + 1) * 5)
        car2.forward((car2_score + 1) * 5)
        if car1.distance(electric) < 50:
            car1_score += 1
            place_electric()
            write_score(car2_score, car1_score)
        if car2.distance(electric) < 50:
            car2_score += 1
            place_electric()
            write_score(car2_score, car1_score)
        if Time < 0:
            electric.hideturtle()
            car1.hideturtle()
            car2.hideturtle()
            if car1_score > car2_score:
                over.write('Game over!\n\nWinner: \n\nblue car.', align='center', font=('Arial', 30, 'normal'))
            elif car1_score < car2_score:
                over.write('Game over!\n\nWinner: \n\nred car.', align='center', font=('Arial', 30, 'normal'))
            else:
                over.write('Game over!\n\nIn ends in a draw.', align='center', font=('Arial', 30, 'normal'))
            break
        if car1_outside_window():
            electric.hideturtle()
            car1.hideturtle()
            car2.hideturtle()
            over.write('Game over!\n\nWinner: \n\nred car.', align='center', font=('Arial', 30, 'normal'))
            break
        if car2_outside_window():
            electric.hideturtle()
            car1.hideturtle()
            car2.hideturtle()
            over.write('Game over!\n\nWinner: \n\nblue car.', align='center', font=('Arial', 30, 'normal'))
            break
        if car1.distance(car2) < 100:
            car1.right(180)
            car2.right(180)

def car1_turn_left():
    car1.left(90)

def car1_turn_right():
    car1.right(90)

def car2_turn_left():
    car2.left(90)

def car2_turn_right():
    car2.right(90)

t.onkey(see_play, 'p')
t.onkey(start_game, 'space')
t.onkey(car1_turn_left, 'Left')
t.onkey(car1_turn_right, 'Right')
t.onkey(car2_turn_left, 'a')
t.onkey(car2_turn_right, 'd')
t.listen()

t.mainloop()

 

【提示】

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

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页