Guest User

Untitled

a guest
Mar 20th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.12 KB | None | 0 0
  1. #encoding: UFT-8
  2. #David Arnaiz
  3. #Realizamos varios dibujos
  4.  
  5. import pygame
  6.  
  7. import math
  8.  
  9. import random
  10.  
  11.  
  12. Ancho = 800
  13. Alto = 800 #dimensiones de la pantalla
  14.  
  15. Blanco = (255,255,255)
  16. Negro = (0,0,0) #colores R,G,B
  17.  
  18. def Dibujarespiral():
  19.  
  20. pygame.init()
  21. ventana = pygame.display.set_mode((Ancho, Alto)) #ventana de dibujo
  22. reloj = pygame.time.Clock() #limita los fps
  23. termina = False
  24.  
  25. while not termina
  26. for evento in pygame.event.get():
  27. if evento.type == pygame.QUIT:
  28. termina = True
  29.  
  30. ventana.fill(Blanco) #borrar
  31.  
  32. for a in range(0,400,10):
  33.  
  34. pygame.draw.line(ventana,Negro,(Ancho-a,Alto-a),(0+a,Alto-a),1) #lineas horizontales abajo
  35.  
  36. pygame.draw.line(ventana,Negro,(0+a,Alto-a),(0+a,10+a)) #verticales
  37.  
  38. pygame.draw.line(ventana,Negro,(0+a,10+a),(Ancho -(a+10), 10 + a),1) # horizontales arriba
  39.  
  40. pygame.draw.line(ventana,Negro,(Ancho- a,Alto-a), (Ancho-a, 10 + a-10),1) # verticales
  41.  
  42. pygame.display.flip() #actualiza los trazos
  43. reloj.tick(30) # 40fps
  44.  
  45. pygame.quit() #termina pygame
  46.  
  47. def DibujarParabola():
  48. pygame.init() #inicia
  49. ventana = pygame.display.set_mode((Ancho,Alto))
  50. reloj = pygame.time.Clock()
  51. termina = False
  52.  
  53. while not termina:
  54. for evento in pygame.event.get():
  55. if evento.type == pygame.QUIT:
  56. termina = True
  57.  
  58.  
  59. ventana.fill(Blanco)
  60.  
  61. RANDOM = (random.randint(0,255),random.randint(0,255),random.randint(0,255)) #cambio de color
  62.  
  63. for p in range(0,400,10):
  64. RANDOM = (random.randint(0,255),random.randint(0,255),random.randint(0,255)
  65.  
  66. pygame.draw.line(ventana,RANDOM,(Ancho // 2 + p , Alto // 2), (Alto // 2, p), 1)# cuadrante 1
  67.  
  68. pygame.draw.line(ventana,RANDOM,(Ancho // 2 - p, Alto - p),(Alto // 2), 1) # 2
  69.  
  70. pygame.draw.line(ventana,RANDOM,(Ancho // 2, Alto -p), (Ancho // 2 - p, Alto//2 ), 1) # 3
  71.  
  72. pygame.draw.line(ventana,RANDOM,(Ancho // 2, Alto - p), (Ancho // 2+p, Alto //2), 1) #4
  73.  
  74. pygame.display.flip()
  75. reloj.tick(30)
  76.  
  77. pygame.quit()
  78.  
  79. def Dibujarcirculosentrelazados():
  80. pygame.init() # Inicia
  81. ventana = pygame.display.set_mode((ANCHO, ALTO))
  82. reloj = pygame.time.Clock()
  83. termina = False
  84.  
  85. while not termina:
  86. for evento in pygame.event.get():
  87. if evento.type == pygame.QUIT: # El usuario hizo click en el botón de salir
  88. termina = True
  89.  
  90.  
  91. ventana.fill(BLANCO)
  92.  
  93. # Dibujar patrón de círculos.
  94. for r in range(1, 13,
  95. 1): # El ángulo máximo es 330. 360 ya está contado en el (0,0). Queremos 12 círculos: 360/11= 32.72
  96. pygame.draw.circle(ventana, NEGRO, (
  97. int((math.cos(r * math.pi / 6) * 150) + ANCHO // 2), int((math.sin(r * math.pi / 6) * 150) + ALTO // 2)),
  98. (150), 1)
  99. # Se multiplica por pi con el fin de pasar los radianes a grados
  100.  
  101. pygame.display.flip() # Actualiza trazos
  102. reloj.tick(60) # 40 fps
  103.  
  104. pygame.quit() # termina pygame
  105.  
  106.  
  107. def dibujarCirculoCuadrado():
  108. pygame.init() # Inicializa pygame
  109. ventana = pygame.display.set_mode((Ancho, Alto))
  110. reloj = pygame.time.Clock()
  111. termina = False # Bandera para saber si termina la ejecución
  112.  
  113. while not termina:
  114. # Procesa los eventos que recibe
  115. for evento in pygame.event.get():
  116. if evento.type == pygame.QUIT: # El usuario hizo click en el botón de salir
  117. termina = True
  118.  
  119. # Borrar pantalla
  120. ventana.fill(Blanco)
  121.  
  122. for y in range(1, 400, 10):
  123. # DibujarCírculo
  124. pygame.draw.circle(ventana, Negro, (Ancho // 2, Alto // 2), y, 1)
  125. # DibujarCuadrado
  126. pygame.draw.rect(ventana, Negro, (y, y, Ancho - y * 2, Alto - y * 2), 1)
  127.  
  128. pygame.display.flip() # Actualiza trazos
  129. reloj.tick(60) # 40 fps
  130.  
  131. pygame.quit() # termina pygame
  132.  
  133.  
  134. def imprimirPiramide():
  135. # Para columna del 8:
  136. incremento = 1
  137. for x in range(1, 10, 1):
  138. resultado = incremento * 8 + x # Primer resultado.
  139. print(incremento, "* 8 +", x, "=", resultado) # Imprime el primer resultado.
  140.  
  141. incremento = (incremento * 10) + (x + 1) # El valor del incremento cambia. Multiplicando la variable inicial X10 y sumando el facto más x para agregar el valor pasado.
  142. print("---------------------------------")
  143.  
  144. # Para columna del 8:
  145. incremento2 = 1
  146.  
  147. for y in range(1, 10, 1):
  148. resultado2 = incremento2 * incremento2 # Primer resultado.
  149. print(incremento2, "*", incremento2, "=", resultado2) # Imprime el primer resultado.
  150.  
  151. incremento2 = (incremento2 * 10) + (1)
  152.  
  153. def calcularNumerosDivisibles():
  154. d = 0
  155. for x in range(1000, 10000, 1):
  156.  
  157. if x % 29 == 0:
  158. d += 1
  159.  
  160. return d
  161.  
  162.  
  163. def aproximarPI(valor):
  164. incremento = 0 # Contador
  165.  
  166. for x in range(1, valor + 1):
  167. incremento = incremento + (1 / x ** 2)
  168.  
  169. pi = (incremento * 6) ** (1 / 2)
  170.  
  171. return pi
  172.  
  173.  
  174. def main():
  175. print("Tarea 5. Seleccione qué quiere hacer:")
  176.  
  177. print("""
  178. 1. Dibujar cuadros y círculos
  179. 2. Dibujar espiral
  180. 3. Dibujar círculos
  181. 4. Dibujar parábolas
  182. 5. Aproximar PI
  183. 6. Contar divisibles entre 29
  184. 7. Imprimir pirámides de números
  185. 0. Salir""")
  186.  
  187. print("")
  188. seleccion = int(input("¿Qué desea hacer?:"))
  189. print("---------------------------------")
  190.  
  191. while seleccion >= 0:
  192. if seleccion == 1:
  193. dibujarCirculoCuadrado()
  194. seleccion = int(input("¿Qué desea hacer?:"))
  195. print("---------------------------------")
  196.  
  197. elif seleccion == 2:
  198. dibujarEspiral()
  199. seleccion = int(input("¿Qué desea hacer?:"))
  200. print("---------------------------------")
  201.  
  202. elif seleccion == 3:
  203. dibujarCirculosEntrelazados()
  204. seleccion = int(input("¿Qué desea hacer?:"))
  205. print("---------------------------------")
  206.  
  207. elif seleccion == 4:
  208. dibujarParabola()
  209. seleccion = int(input("¿Qué desea hacer?:"))
  210. print("---------------------------------")
  211.  
  212. elif seleccion == 7:
  213. imprimirPiramide()
  214. seleccion = int(input("¿Qué desea hacer?:"))
  215. print("---------------------------------")
  216.  
  217. elif seleccion == 6:
  218. print("Número de divisibles entre 29 es:", calcularNumerosDivisibles())
  219. seleccion = int(input("¿Qué desea hacer?:"))
  220. print("---------------------------------")
  221.  
  222. elif seleccion == 5:
  223. pi = int(input("Ingresa el rango de pi:"))
  224. print("PI vale:", aproximarPI(pi))
  225. seleccion = int(input("¿Qué desea hacer?:"))
  226. print("---------------------------------")
  227.  
  228. elif seleccion >= 8:
  229. print("Selecciones un número del 1 al 7.")
  230. print("")
  231. seleccion = int(input("¿Qué desea hacer?:"))
  232. print("---------------------------------")
  233.  
  234. elif seleccion == 0:
  235. print("¡Hasta luego!")
  236. break
  237.  
  238.  
  239. main()
Add Comment
Please, Sign In to add comment