用户:
ZCM张宸铭小作坊查看:0 回复:1 评论:0 创建时间:2023-08-19T11:27:13
这是我的早期作品,大概两三年前的样子吧,拿出来开源了
顺便说一下海龟的screen.tracer(0)用法,简单理解为去除海龟运动过程直接显示结果,但是要加上screen.update()刷新画面

非常烂,凑合看吧,在线版本的海龟编辑器运行起来会卡,本地环境运行丝般顺滑
import turtle as t
import time
import random
pen = t.Pen()
pen.speed(0)
pen.hideturtle()
pen.pensize(5)
player = t.Pen()
player.speed(0)
player.hideturtle()
player.pensize(5)
player.up()
ui = t.Pen()
ui.speed(0)
ui.hideturtle()
ui.pensize(2)
ui.up()
ui.pencolor('white')
ui_plyer = t.Pen()
ui_plyer.speed(0)
ui_plyer.hideturtle()
ui_plyer.pensize(2)
ui_plyer.up()
ui_plyer.pencolor('white')
sc = t.Screen()
sc.setup(1010, 810)
# sc.screensize(1000, 800)
sc.bgpic('bg.png')
sc.tracer(False)
nowRoom = 41
enRoom = 99
player.goto(-20,70)
player_x = 0
player_y = 0
testMode = False
def restart():
player.goto(-20,70)
player_x = 0
player_y = 0
'''
rooms = {11: '1122', 12: '1221', 13: '1221', 14: '1212', 15: '3112',
21: '2122', 22: '1212', 23: '1122', 24: '2211', 25: '2112',
31: '2111', 32: '2112', 33: '2121', 34: '1222', 35: '2211',
41: '1112', 42: '2122', 43: '1222', 44: '2221', 45: '1212',
51: '2121', 52: '2221', 53: '2221', 54: '1213', 55: '2111'}
'''
rooms = {11: '1312', 12: '1122', 13: '1221', 14: '1221', 15: '1212', 16: '1122', 17: '1221', 18: '1212', 19: '1112',
21: '2112', 22: '2112', 23: '1122', 24: '1211', 25: '2121', 26: '2211', 27: '1122', 28: '2211', 29: '2112',
31: '2121', 32: '2211', 33: '2112', 34: '1122', 35: '1222', 36: '1211', 37: '2112', 38: '1122', 39: '2212',
41: '1121', 42: '1222', 43: '2212', 44: '2112', 45: '2121', 46: '1212', 47: '2112', 48: '2112', 49: '2112',
51: '1122', 52: '2211', 53: '2112', 54: '2121', 55: '1212', 56: '2112', 57: '2121', 58: '2211', 59: '2112',
61: '2112', 62: '1121', 63: '2221', 喵: '1212', 65: '2112', 66: '2122', 67: '1221', 68: '1211', 69: '2112',
71: '2112', 72: '1122', 73: '1222', 74: '1221', 75: '2112', 76: '2121', 77: '1212', 78: '1122', 79: '2211',
81: '2122', 82: '2211', 83: '2121', 84: '1221', 85: '2211', 86: '1112', 87: '2112', 88: '2122', 89: '1212',
91: '2121', 92: '1221', 93: '1221', 94: '1221', 95: '1221', 96: '2211', 97: '2121', 98: '2211', 99: '2111'}
#x:-20,100
#y:-250,-370
def map_up_wall():
ui.down()
ui.goto(ui.xcor()+11,ui.ycor())
ui.up()
def map_up_out():
ui.down()
ui.pencolor('green')
ui.goto(ui.xcor()+11,ui.ycor())
ui.pencolor('white')
ui.up()
def map_up_door():
ui.up()
ui.down()
ui.goto(ui.xcor()+3,ui.ycor())
ui.up()
ui.goto(ui.xcor()+5,ui.ycor())
ui.down()
ui.goto(ui.xcor()+3,ui.ycor())
ui.up()
def map_right_wall():
ui.down()
ui.goto(ui.xcor(),ui.ycor()-11)
ui.up()
def map_right_out():
ui.down()
ui.pencolor('green')
ui.goto(ui.xcor(),ui.ycor()-11)
ui.pencolor('white')
ui.up()
def map_right_door():
ui.down()
ui.goto(ui.xcor(),ui.ycor()-3)
ui.up()
ui.goto(ui.xcor(),ui.ycor()-5)
ui.down()
ui.goto(ui.xcor(),ui.ycor()-3)
ui.up()
def map_down_wall():
ui.down()
ui.goto(ui.xcor()-11,ui.ycor())
ui.up()
def map_down_out():
ui.down()
ui.pencolor('green')
ui.goto(ui.xcor()-11,ui.ycor())
ui.pencolor('white')
ui.up()
def map_down_door():
ui.down()
ui.goto(ui.xcor()-3,ui.ycor())
ui.up()
ui.goto(ui.xcor()-5,ui.ycor())
ui.down()
ui.goto(ui.xcor()-3,ui.ycor())
ui.up()
def map_left_wall():
ui.down()
ui.goto(ui.xcor(),ui.ycor()+11)
ui.up()
def map_left_out():
ui.down()
ui.pencolor('green')
ui.goto(ui.xcor(),ui.ycor()+11)
ui.pencolor('white')
ui.up()
def map_left_door():
ui.down()
ui.goto(ui.xcor(),ui.ycor()+3)
ui.up()
ui.goto(ui.xcor(),ui.ycor()+5)
ui.down()
ui.goto(ui.xcor(),ui.ycor()+3)
ui.up()
def draw_map_room(room):
ui.up()
ui_plyer.clear()
try:
roomPos = int(room)
except:
print('Map err!')
roomToDraw = list(rooms[roomPos])
roomPos = list(str(room))
x_pos = roomPos[1]
y_pos = roomPos[0]
x_start_add = int(roomPos[1])*11
y_start_add = int(roomPos[0])*11
x_start = 11+x_start_add
y_start = -261-y_start_add
# x_end = (int(roomPos[0])+1)*10
# y_end = (int(roomPos[1])+1)*10
ui.goto(x_start,y_start)
ui.down()
if roomToDraw[0] == '1':
map_up_wall()
elif roomToDraw[0] == '2':
map_up_door()
elif roomToDraw[0] == '3':
map_up_out()
else:
pass
if roomToDraw[2] == '1':
map_right_wall()
elif roomToDraw[2] == '2':
map_right_door()
elif roomToDraw[2] == '3':
map_right_out()
else:
pass
if roomToDraw[3] == '1':
map_down_wall()
elif roomToDraw[3] == '2':
map_down_door()
elif roomToDraw[3] == '3':
map_down_out()
else:
pass
if roomToDraw[1] == '1':
map_left_wall()
elif roomToDraw[1] == '2':
map_left_door()
elif roomToDraw[1] == '3':
map_left_out()
else:
pass
ui_plyer.goto(ui.xcor()+5,ui.ycor()-5)
ui_plyer.pencolor('red')
ui_plyer.dot(5)
ui_plyer.pencolor('white')
def left_wall():
pen.up()
pen.goto(-315, 104)
pen.down()
pen.goto(-349, -35)
def left_out():
pen.pensize(15)
pen.pencolor('green')
pen.up()
pen.goto(-315, 104)
pen.down()
pen.goto(-349, -35)
pen.pencolor('black')
pen.pensize(5)
def left_door():
pen.up()
pen.goto(-316, 104)
pen.down()
pen.goto(-316, 209)
pen.up()
pen.goto(-349, -35)
pen.down()
pen.goto(-349, 72)
def right_wall():
pen.up()
pen.goto(282, 102)
pen.down()
pen.goto(316, -33)
def right_out():
pen.pensize(15)
pen.pencolor('green')
pen.up()
pen.goto(282, 102)
pen.down()
pen.goto(316, -33)
pen.pencolor('black')
pen.pensize(5)
def right_door():
pen.up()
pen.goto(282, 102)
pen.down()
pen.goto(282, 215)
pen.up()
pen.goto(316, -33)
pen.down()
pen.goto(316, 88)
def front_wall():
pen.up()
pen.goto(-98, 190)
pen.down()
pen.goto(74, 190)
def front_out():
pen.pensize(15)
pen.pencolor('green')
pen.up()
pen.goto(-98, 190)
pen.down()
pen.goto(74, 190)
pen.pencolor('black')
pen.pensize(5)
def front_door():
pen.up()
pen.goto(-98, 190)
pen.down()
pen.goto(-98, 285)
pen.up()
pen.goto(74, 190)
pen.down()
pen.goto(74, 285)
def back_wall():
pen.up()
pen.goto(-144, -170)
pen.down()
pen.goto(119, -170)
def back_out():
pen.pencolor('green')
pen.pensize(15)
pen.up()
pen.goto(-144, -170)
pen.down()
pen.goto(119, -170)
pen.pencolor('black')
pen.pensize(5)
def back_door():
pen.up()
pen.goto(-144, -170)
pen.down()
pen.goto(-144, -56)
pen.up()
pen.goto(119, -170)
pen.down()
pen.goto(119, -56)
def show_room():
pen.clear()
roomType = list(rooms[nowRoom])
if roomType[0] == '1':
front_wall()
elif roomType[0] == '2':
front_door()
elif roomType[0] == '3':
front_out()
else:
front_wall()
if roomType[1] == '1':
left_wall()
elif roomType[1] == '2':
left_door()
elif roomType[1] == '3':
left_out()
else:
left_wall()
if roomType[2] == '1':
right_wall()
elif roomType[2] == '2':
right_door()
elif roomType[2] == '3':
right_out()
else:
right_wall()
if roomType[3] == '1':
back_wall()
elif roomType[3] == '2':
back_door()
elif roomType[3] == '3':
back_out()
else:
back_wall()
sc.update()
def finish():
pen.clear()
player.clear()
sc.bgpic('finish.png')
sc.update()
sc.exitonclick()
def move(state):
global rooms,nowRoom
room = str(rooms[nowRoom])
if state == 'up' and room[0] == '2':
nowRoom -= 10
elif state == 'left' and room[1] == '2':
nowRoom -= 1
elif state == 'right' and room[2] == '2':
nowRoom += 1
elif state == 'down' and room[3] == '2':
nowRoom += 10
elif state == 'up' and room[0] == '3':
finish()
elif state == 'left' and room[1] == '3':
finish()
elif state == 'right' and room[2] == '3':
finish()
elif state == 'down' and room[3] == '3':
finish()
else:
return
player_move(state)
draw_map_room(nowRoom)
show_room()
player_back_home(state)
sc.update()
def draw_player():
player.goto(player.xcor(),player.ycor()+10)
player.dot(15)
player.goto(player.xcor(),player.ycor()-10)
player.pensize(10)
player.down()
player.goto(player.xcor(),player.ycor()-10)
player.up()
player.goto(player.xcor(),player.ycor()+10)
draw_player()
def player_move(turn):
global player_x,player_y
restart()
if turn == 'up':
while player.ycor() < 190:
player.clear()
player.goto(player.xcor(),player.ycor()+10)
draw_player()
time.sleep(0.005)
sc.update()
elif turn == 'left':
while player.xcor() > -320:
player.clear()
player.goto(player.xcor()-10,player.ycor())
draw_player()
time.sleep(0.005)
sc.update()
elif turn == 'right':
while player.xcor() < 290:
player.clear()
player.goto(player.xcor()+10,player.ycor())
draw_player()
time.sleep(0.005)
sc.update()
elif turn == 'down':
while player.ycor() > -170:
player.clear()
player.goto(player.xcor(),player.ycor()-10)
draw_player()
time.sleep(0.005)
sc.update()
def player_back_home(_from):
if _from == 'down':
player.goto(-20,190)
while player.ycor() > 70:
player.clear()
player.goto(player.xcor(),player.ycor()-10)
draw_player()
time.sleep(0.005)
sc.update()
player.clear()
player.goto(-20,70)
draw_player()
sc.update()
elif _from == 'up':
player.goto(-20,-170)
while player.ycor() < 70:
player.clear()
player.goto(player.xcor(),player.ycor()+10)
draw_player()
time.sleep(0.005)
sc.update()
player.clear()
player.goto(-20,70)
draw_player()
sc.update()
elif _from == 'right':
player.goto(-320,70)
while player.xcor() < -20:
player.clear()
player.goto(player.xcor()+10,player.ycor())
draw_player()
time.sleep(0.005)
sc.update()
player.clear()
player.goto(-20,70)
draw_player()
sc.update()
elif _from == 'left':
player.goto(290,70)
while player.xcor() > -20:
player.clear()
player.goto(player.xcor()-10,player.ycor())
draw_player()
time.sleep(0.005)
sc.update()
player.clear()
player.goto(-20,70)
draw_player()
sc.update()
def tp():
global nowRoom
tpPos = int(t.textinput('jump','输入房间代码'))
nowRoom = tpPos
draw_map_room(nowRoom)
show_room()
sc.update()
def main(x,y):
if -315 < x < -273 and -308 < y < -267:
move('up')
elif -370 < x < -330 and -360 < y < -320:
move('left')
elif -263 < x < -221 and -360 < y < -320:
move('right')
elif -315 < x < -273 and -360 < y < -320:
move('down')
elif 217 < x < 456 and -377 < y < -276 and testMode == True:
tp()
else:
pass
def check(x, y):
print(x, y)
show_room()
draw_map_room(nowRoom)
sc.onscreenclick(main)
t.done()
顺带一提,如果要在本地运行的话,为了达到最佳效果,请把sc.screensize那一行取消注释,在线运行会报错。