[关闭]
@wudawufanfan 2016-09-25T11:48:26.000000Z 字数 2412 阅读 636

平移图形

python


Introduction

Usually,we think computer language is a boring thing.In order to figure out a thing,programmer will type too many lines of codes without any mistake.But today's problem is to make the static character move on the screen.It's a really funny thing to do!

Text

Use any means to make your English name or anything like it move.
The following is the code

  1. import os
  2. import time
  3. def benpaobalanxiang():
  4. for i in range(150):
  5. print('wu fanfan\n')
  6. print(' '*i+' ___')
  7. print(' '*i+' /======/')
  8. print(' '*i+' ____ // \___ ,/')
  9. print(' '*i+' | \\ // :, ./')
  10. print(' '*i+' |_______|__|_// ;:; /')
  11. print(' '*i+' |_______|__|_// ;:; /')
  12. print(' '*i+' _L_____________\o ;;;/')
  13. print(' '*i+'____(CCCCCCCCCCCCCC)____________-/_____________________')
  14. time.sleep( 0.2 )
  15. os.system("cls")
  16. time.sleep( 10 )
  17. benpaobalanxiang()

we can make the printed praph move from left to right
[如图所示][ 9702843619.gif]

Conclusion

We can let the figure move from left to right.Using the same way we can move anything like this.

Now we tend to the rotation of the figure(The effect is not well)

The code is the following
First,make a initial.txt
[文件样式][txt.jpg]
the next is the code

  1. import os
  2. import math
  3. import time
  4. def read_initial(initial_file): ######## read the initial.txt and transfer it into a 2d list
  5. itxt= open(initial_file)
  6. initial_screen=[]
  7. for lines in itxt.readlines():
  8. lines=lines.replace("\n","").split(",")
  9. initial_screen.append(lines)
  10. itxt.close()
  11. for i in range(len(initial_screen)):
  12. initial_screen[i].extend(initial_screen[i][0])
  13. del initial_screen[i][0]
  14. return initial_screen
  15. def rotation(angle,xscreen,l2,s,x_):
  16. if (angle != 0):
  17. for j in range(l2):
  18. if (xscreen[x_][j] == s):
  19. radius = j - 60
  20. x = int(radius*math.cos(angle*math.pi/180))+60
  21. y = int(radius*math.sin(angle*math.pi/180))+x_
  22. xscreen[y][x] = xscreen[x_][j]
  23. xscreen[x_][j] = ' '
  24. else:
  25. pass
  26. return xscreen
  27. else:
  28. return xscreen
  29. def output_screen(l,w,oscreen):
  30. final_screen = [' ']*(w-1)
  31. for i in range( w-1):
  32. for j in range(l-1):
  33. final_screen[i] = final_screen[i]+oscreen[i][j]
  34. print (final_screen[i])
  35. print ('\n')
  36. def main():
  37. while True:
  38. hours = time.localtime(time.time())[3]
  39. if (hours >=12):
  40. hours = hours - 12
  41. minutes = time.localtime(time.time())[4]
  42. seconds = time.localtime(time.time())[5]
  43. iscreen = read_initial('initial.txt')
  44. length = len(iscreen[0])
  45. width = len(iscreen)
  46. fscreen = rotation(((hours+minutes/60)*30)-90,rotation((minutes*6)-90,rotation((seconds*6)-90,iscreen,length,'^',23),length,'*',22),length,'$',21)
  47. output_screen(length,width,fscreen)
  48. time.sleep(0.5)
  49. os.system('cls')
  50. main()

the result is
[动图节选][282172204.gif]

Thank

Thank Caihao for his hint.
1、Use the "import os i=os.systerm('cls')" to clean the screen
2、add space before each line to make the character move
Thank Chengfen liu yutao for their reminder.

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注