Skip to content

Instantly share code, notes, and snippets.

@onitonitonito
Last active July 25, 2020 02:07
Show Gist options
  • Save onitonitonito/18b31818e1171b6f2193fe1f4db20f52 to your computer and use it in GitHub Desktop.
Save onitonitonito/18b31818e1171b6f2193fe1f4db20f52 to your computer and use it in GitHub Desktop.
Multiple Move of Turtle Using QtPy UI - Use pre inputed coordination data
# ------ root path 를 sys.path.insert 시키는 코드 ... 최소 4줄 필요------
import os, sys # 1
top = "k_mooc_reboot" # 2 = name of Top folder
root = "".join(os.path.dirname(__file__).partition(top)[:2])+"\\" # 3 = root_dir for top folder
sys.path.insert(0, root) # 4
# ---------------------------------------------------------------------
if __name__ == '__main__':
print(root) # check root_dir whole path
import sys
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import (
QApplication,
QWidget,
QInputDialog,
QLineEdit,
)
class MyApp(QWidget):
def __init__(self):
super().__init__()
self.title = 'QInputDialog'
self.posxy = (50, 50)
self.windowSize = (400, 500)
def initUI(self):
# self.getChoice()
# self.getText()
self.showBasic()
def getChoice(self, winTitle, captionHead, items):
"""
# items = ("Red","Blue","Green")
"""
item, okPressed = QInputDialog.getItem(
self,
"Get item",
"Color:",
items,
0,
False,
)
if okPressed and item:
return item
def getInteger(
self, winTitle, captionHead, initValue=28,
start=0, end=100, step=1
):
integer, okPressed = QInputDialog.getInt(
self,
winTitle,
captionHead,
initValue,
start,
end,
step,
)
if okPressed:
return integer
def getText(self, winTitle, captionHead, initValue=""):
text, okPressed = QInputDialog.getText(
self,
winTitle,
captionHead,
QLineEdit.Normal,
initValue,
)
if okPressed and text != '':
return text
def getDouble(self):
d, okPressed = QInputDialog.getDouble(
self,
"Get double",
"Value:",
10.05,
0,
100,
10,
)
if okPressed:
print(d)
def showBasic(self):
self.setWindowTitle(self.title)
self.setGeometry(*self.posxy, *self.windowSize)
self.show()
if __name__ == '__main__':
posxy = (400, 700)
windowsize = (500, 500)
# winTitle = "Integer"
# captionHead = "INPUT VALUE :"
winTitle="TEXT!"
captionHead="ANGLE, MOVE :"
def main():
while True:
ma.setGeometry(*posxy, *windowsize)
_value = get_text(winTitle, captionHead)
print(f"value ... {_value}")
def get_int(winTitle, captionHead):
return ma.getInteger(winTitle, captionHead)
def get_text(winTitle, captionHead):
return ma.getText(winTitle, captionHead)
app = QApplication(sys.argv)
ma = MyApp()
main()
sys.exit(app.exec_())
"""
# Test Turtle : multiple turtles movement
"""
# - Enable = blank enter
# - Enable = set number of turtles
# - Enable = echo dict drawing (record drawing)
print(__doc__)
import sys
import turtle
from _assets._add_syspath_root import root
from _assets.modules_qt import QApplication, MyApp
FILE_DIR = root + 'module_turtle\\_statics\\'
FILENAME_ECHO = FILE_DIR + 'turtle_echo.txt'
POSXY = (1300, 900)
WINDOWSIZE = (400,300)
def main():
global MA, NUM_TURTLES, TURTLES
MA = MyApp()
# w = a = s = d = turtle.Turtle()
NUM_TURTLES = int(qt_input('NUMBERS OF TURTLES? : '))
TURTLES = [turtle.Turtle() for i in range(NUM_TURTLES)]
mode_select = qt_input('MODE - [Enter]=DRAW / [1]=DICT INPUT : ')
turtle.speed('fastest')
if mode_select.startswith('1'):
echoes = get_read_echo(filename_echo=FILENAME_ECHO)
dict_moves = get_moves_from_echoes(echoes)
draw_from_dict(TURTLES, dict_moves)
else:
draw_from_manual(TURTLES)
def qt_input(captionHead="INPUT :", winTitle="TITLE!"):
MA.setGeometry(*POSXY, *WINDOWSIZE)
qtInput = MA.getText(winTitle, captionHead)
if qtInput: return qtInput
return ""
def get_read_echo(filename_echo):
""" """
with open(filename_echo, mode='r', encoding='utf8') as f:
echoes_string = f.read()
return echoes_string
def get_moves_from_echoes(echoes):
""" get string-moves array from string-echoes"""
echoes_array = echoes.split('\n')
moves_array = [echo.split(':')[1]
for echo in echoes_array
if echo.startswith('*** ENTER ANGLE')]
dict_moves = {}
for i, move in enumerate(moves_array, 1):
if move != '':
angle, move = move.split(',')
angle, move = int(angle.strip()), int(move.strip())
temp_move = (angle, move)
dict_moves[i] = temp_move
return dict_moves
def draw_from_dict(turtles, dict):
""" # read movements from dict, draw multi-turtles"""
start=True
for vals in dict.values():
moves = "{}, {}".format(vals[0], vals[1])
print(f"*** ENTER ANGLE, FORWARD MOVE : {moves}")
if start:
move_turtles(turtles, moves, start=True)
start = False
else:
move_turtles(turtles, moves, start=False)
qt_input('QUIT : [Enter]')
quit()
def draw_from_manual(turtles):
""" # manual draw """
start = True
while True:
# CASE-01: correct moves = Move, Angle
# CASE_02: Incorrect moves = Enter (blank), ?
moves = qt_input('ENTER ANGLE, FORWARD MOVE : ')
print(f"*** ENTER ANGLE, FORWARD MOVE : {moves}")
if moves.startswith('?'):
quit()
if len(moves):
move_turtles(turtles, moves, start=start)
temp_moves = moves
start = False
else:
move_turtles(turtles, temp_moves, start=False)
def move_turtles(turtles, moves, start=True):
""" to move muti-turtles along w/ (angle,move)
# move_turtles(turtles, moves, start=True):
# - turtles = list of turtle objects
# - moves = input str 'angle,move' will be --> int (angle, move)
# - start = if True, when First movements set
# radiation directions of each turtles by 360/n
"""
angle, move = get_angle_move_from(moves)
heads = [n for n in range(0, 361, int(360/NUM_TURTLES))]
for i, tur in enumerate(turtles):
if start:
tur.setheading(heads[i])
tur.left(angle) if angle > 0 else tur.right(abs(angle))
tur.forward(move)
def get_angle_move_from(moves):
""" to make str-moves into int(angle, move)
# get_angle_move_from(moves):
# - return int (angle, move)
"""
angle, move = moves.split(",")
angle, move = angle.strip(), move.strip()
return int(angle), int(move)
if __name__ == '__main__':
app = QApplication(sys.argv)
main()
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment