有趣的Python代码分享 您所在的位置:网站首页 python绘制乌龟 有趣的Python代码分享

有趣的Python代码分享

2023-03-29 20:19| 来源: 网络整理| 查看: 265

小猪佩奇# coding:utf-8 from turtle import* def nose(x,y):#鼻子 pu() goto(x,y) pd() seth(-30) begin_fill() a=0.4 for i in range(120): if 0import turtle import time # 画心形圆弧 def hart_arc(): for i in range(200): turtle.right(1) turtle.forward(2) def move_pen_position(x, y): turtle.hideturtle() # 隐藏画笔(先) turtle.up() # 提笔 turtle.goto(x, y) # 移动画笔到指定起始坐标(窗口中心为0,0) turtle.down() # 下笔 turtle.showturtle() # 显示画笔 love = input("请输入表白话语,默认为‘I Love You’:") signature = input("请签署你的大名,不填写默认不显示:") if love == '': love = 'I Love You' time.sleep(1) # 初始化 turtle.setup(width=800, height=500) # 窗口(画布)大小 turtle.color('red', 'pink') # 画笔颜色 turtle.pensize(3) # 画笔粗细 turtle.speed(1) # 描绘速度 # 初始化画笔起始坐标 move_pen_position(x=0,y=-180) # 移动画笔位置 turtle.left(140) # 向左旋转140度 turtle.begin_fill() # 标记背景填充位置 # 画心形直线( 左下方 ) turtle.forward(224) # 向前移动画笔,长度为224 # 画爱心圆弧 hart_arc() # 左侧圆弧 turtle.left(120) # 调整画笔角度 hart_arc() # 右侧圆弧 # 画心形直线( 右下方 ) turtle.forward(224) turtle.end_fill() # 标记背景填充结束位置 # 在心形中写上表白话语 move_pen_position(0,0) # 表白语位置 turtle.hideturtle() # 隐藏画笔 turtle.color('#CD5C5C', 'pink') # 字体颜色 # font:设定字体、尺寸(电脑下存在的字体都可设置) align:中心对齐 turtle.write(love, font=('Arial', 30, 'bold'), align="center") # 签写署名 if signature != '': turtle.color('red', 'pink') time.sleep(2) move_pen_position(180, -180) turtle.hideturtle() # 隐藏画笔 turtle.write(signature, font=('Arial', 20), align="center") # 点击窗口关闭程序 window = turtle.Screen() window.exitonclick()

7.华容道游戏(瓷砖游戏)

"""Sliding Tile Puzzle, by Al Sweigart [email protected] Slide the numbered tiles into the correct order. This code is available at https://nostarch.com/big-book-small-python-programming Tags: large, game, puzzle""" import random, sys BLANK = ' ' # Note: This string is two spaces, not one. def main(): print(''' 使用 WASD 键移动磁贴 恢复到原来的顺序: +------+------+------+------+ | | | | | | 1 | 2 | 3 | 4 | | | | | | +------+------+------+------+ | | | | | | 5 | 6 | 7 | 8 | | | | | | +------+------+------+------+ | | | | | | 9 | 10 | 11 | 12 | | | | | | +------+------+------+------+ | | | | | | 13 | 14 | 15 | | | | | | | +------+------+------+------+ ''') input('按回车键开始...') gameBoard = getNewPuzzle() while True: displayBoard(gameBoard) playerMove = askForPlayerMove(gameBoard) makeMove(gameBoard, playerMove) if gameBoard == getNewBoard(): print('You won!') sys.exit() def getNewBoard(): """Return a list of lists that represents a new tile puzzle.""" return [['1 ', '5 ', '9 ', '13'], ['2 ', '6 ', '10', '14'], ['3 ', '7 ', '11', '15'], ['4 ', '8 ', '12', BLANK]] def displayBoard(board): """Display the given board on the screen.""" labels = [board[0][0], board[1][0], board[2][0], board[3][0], board[0][1], board[1][1], board[2][1], board[3][1], board[0][2], board[1][2], board[2][2], board[3][2], board[0][3], board[1][3], board[2][3], board[3][3]] boardToDraw = """ +------+------+------+------+ | | | | | | {} | {} | {} | {} | | | | | | +------+------+------+------+ | | | | | | {} | {} | {} | {} | | | | | | +------+------+------+------+ | | | | | | {} | {} | {} | {} | | | | | | +------+------+------+------+ | | | | | | {} | {} | {} | {} | | | | | | +------+------+------+------+ """.format(*labels) print(boardToDraw) def findBlankSpace(board): """Return an (x, y) tuple of the blank space's location.""" for x in range(4): for y in range(4): if board[x][y] == ' ': return (x, y) def askForPlayerMove(board): """Let the player select a tile to slide.""" blankx, blanky = findBlankSpace(board) w = 'W' if blanky != 3 else ' ' a = 'A' if blankx != 3 else ' ' s = 'S' if blanky != 0 else ' ' d = 'D' if blankx != 0 else ' ' while True: print(' ({})'.format(w)) print('Enter WASD (or QUIT): ({}) ({}) ({})'.format(a, s, d)) response = input('> ').upper() if response == 'QUIT': sys.exit() if response in (w + a + s + d).replace(' ', ''): return response def makeMove(board, move): """Carry out the given move on the given board.""" # Note: This function assumes that the move is valid. bx, by = findBlankSpace(board) if move == 'W': board[bx][by], board[bx][by+1] = board[bx][by+1], board[bx][by] elif move == 'A': board[bx][by], board[bx+1][by] = board[bx+1][by], board[bx][by] elif move == 'S': board[bx][by], board[bx][by-1] = board[bx][by-1], board[bx][by] elif move == 'D': board[bx][by], board[bx-1][by] = board[bx-1][by], board[bx][by] def makeRandomMove(board): """Perform a slide in a random direction.""" blankx, blanky = findBlankSpace(board) validMoves = [] if blanky != 3: validMoves.append('W') if blankx != 3: validMoves.append('A') if blanky != 0: validMoves.append('S') if blankx != 0: validMoves.append('D') makeMove(board, random.choice(validMoves)) def getNewPuzzle(moves=200): """Get a new puzzle by making random slides from a solved state.""" board = getNewBoard() for i in range(moves): makeRandomMove(board) return board # If this program was run (instead of imported), run the game: if __name__ == '__main__': main()

8.猜数字游戏(1-100随机)

# 这是一个猜数字的游戏 import random secretNumber=random.randint(1,100) print('我会想一个1到100之间的数') # 共有六次机会猜数字 for i in range(1,7): print('输入你的猜测数字:') guess=int(input()) if guesssecretNumber: print('你猜测的数字偏大') else: break if guess==secretNumber: print('做得好!你总共猜测了'+str(i)+'次') else: print('很遗憾,你失败了。我所想的数字是'+str(secretNumber)) input()

9.画红色爱心(love)

import turtle import math turtle.pen() t=turtle t.up() t.goto(0,150) t.down() t.color('red') t.begin_fill() t.fillcolor('red') t.speed(1) t.left(45) t.forward(150) t.right(45) t.forward(100) t.right(45) t.forward(100) t.right(45) t.forward(100) t.right(45) t.forward(250+math.sqrt(2)*100) t.right (90) t.speed(2) t.forward(250+100*math.sqrt(2)) t.right(45) t.forward(100) t.right(45) t.forward(100) t.right(45) t.forward(100) t.right(45) t.forward(150) t.end_fill() t.goto(-10,0) t.pencolor('white') #L t.pensize(10) t.goto(-50,0) t.goto(-50,80) t.up () #I t.goto(-100,0) t.down() t.goto(-160,0) t.goto(-130,0) t.goto(-130,80) t.goto(-160,80) t.goto(-100,80) t.up() #O t.goto(10,25) t.down() t.right(45) t.circle(25,extent=180) t.goto(60,55) t.circle(25,extent=180) t.goto(10,25) t.up() t.goto(75,80) t.down() t.goto(100,0) t.goto(125,80) t.up() t.goto(180,80) t.down() t.goto(140,80) t.goto(140,0) t.goto(180,0) t.up() t.goto(180,40) t.down() t.goto(140,40) #U t.up() t.goto(-40,-30) t.down() t.goto(-40,-80) t.circle(40,extent=180) t.goto(40,-30) t.hideturtle()

10.点击100次即可出现(其实什么也不会出现)

from tkinter import * def Call(): msg=Label(window,text="点击100次即可出现") msg.place(x=30,y=50) button["bg"]="blue" button["fg"]="red" window=Tk() window.geometry("200x110") button=Button(text="点击",command=Call) button.place(x=30,y=20,width=120,height=25) window.mainloop()



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有