2017-12-11 9 views
0

프로젝트 용으로 Zelle graphics 모듈을 사용하여 파이썬으로 게임을 만들려고합니다. (그것은 Zelle 그래픽이어야합니다). 이 프로그램은 임의의 수의 얼굴을 그리고, 그 위에 텍스트가있는 단추를 그립니다. 나는 완전한 초심자이고, 이것이 정말로 명백한 지 사과드립니다.zelle 그래픽 버튼으로 포인트 시스템 만들기

사용자가 버튼을 선택하고 버튼을 답으로 사용할 수있게하려면 버튼을 숫자와 연결하는 방법을 알아낼 수 없으며 그 번호를 올바른 답으로 사용하십시오. 점을 지정합니다. 오류는 없지만 단추가 의도 한대로 작동하지 않습니다. 단추를 번호 응답과 연관 시키려면 어떻게합니까?

def isBetween(x, end1, end2): 
    return end1 <= x <= end2 or end2 <= x <= end1 

def isInside(point, rect): 
    pt1 = rect.getP1() 
    pt2 = rect.getP2() 
    return isBetween(point.getX(), pt1.getX(), pt2.getX()) and \ 
     isBetween(point.getY(), pt1.getY(), pt2.getY()) 

def makeColoredRect(corner, width, height, color, win): 
    corner2 = corner.clone() 
    corner2.move(width, -height) 
    rect = Rectangle(corner, corner2) 
    rect.setFill(color) 
    rect.draw(win) 
    return rect 

def getChoice(choicePairs, default, win): 
    point = win.getMouse() 
    for (rectangle, choice) in choicePairs: 
     if isInside(point, rectangle): 
      return choice 
    return default 

def makeButtonSetup(colors): 
    buttons = list() 
    x = 810 
    y = 350 
    for color in colors: 
     buttons.append((x, y, color)) 
     y = y - 40 
    return buttons 

choicePairs = list() 
buttonSetup = makeButtonSetup(['red', 'green', 'blue', 'purple', 'orange', 'yellow']) 
for (x, y, color) in buttonSetup: 
    button = makeColoredRect(Point(x, y), 80, 30, color, win) 
    choicePairs.append((button, color)) 

answerPairs = [(1, 'red'), (2, 'green'), (3, 'blue'), (4, 'purple'), (5, 'orange'), (6, 'yellow')] 

redText = Text(Point(850, 335), "One Face") 
redText.setSize(10) 
redText.draw(win) 
greenText = Text(Point(850, 295), "Two Faces") 
greenText.setSize(10) 
greenText.draw(win) 
blueText = Text(Point(850, 255), "Three Faces") 
blueText.setSize(10) 
blueText.draw(win) 
purpleText = Text(Point(850, 215), "Four Faces") 
purpleText.setSize(10) 
purpleText.draw(win) 
orangeText = Text(Point(850, 175), "Five Faces") 
orangeText.setSize(10) 
orangeText.draw(win) 
yellowText = Text(Point(850, 135), "Six Faces") 
yellowText.setSize(10) 
yellowText.draw(win) 

numFaces = randint(1, 6) 
print(numFaces) 

points = 0 
correctAnswer = True 
def correct(points):  
    if correctAnswer == True: 
     points = points+10 
    print(points) 
    if numFaces == 1: 
     1 == True 
    if numFaces == 2: 
     2 == True  
    if numFaces == 3: 
     3 == True 
    if numFaces == 4: 
     4 == True 
    if numFaces == 5: 
     5 == True 
    if numFaces == 6: 
     6 == True 
    else: 
     correctAnswer == False 

def incorrectAnswer(points): 
    correctAnswer = False 
    if correctAnswer == False: 
     points = 0 
    print(points) 

correct(points) 

robots = [] 
for n in range(numFaces): 
    robots.append(makeRobotFace()) 

i = 0 
for robotList in robots: 
    for robotPart in robotList: 
     robotPart.draw(win) 
     robotPart.move(i * 100, 0) 
    i = i + 1 

time.sleep(5) 
i = 0 

for robotList in robots: 
    for robotPart in robotList: 
     robotPart.undraw() 
    i = i + 1 

shapePairs = [(1, 'redText'), (2, 'greenText'), (3, 'blueText'), (4, 'purpleText'), (5, 'orangeText'), (6, 'yellowText')] 
msg = Text(Point(700, 400),'') 
msg.draw(win) 
for (shape, description) in shapePairs: 
    time.sleep(.2) 
    prompt = 'Click to choose an answer' 
    msg.setText(prompt) 
    answer = getChoice(choicePairs, shapePairs, win) 
+0

작업 생성 - 예. '(버튼, 색상, 숫자, 대답)'을 선택하면 답을 검색 할 필요가 없습니다. 또는 적어도'(button, color, number) '를 사용하고 사전'answerPairs = {1 :'red ', 2 :'green ', ..}'을 만들거나 answerPairs = ['red ','green ' ...]', 그래서 당신은'button'에서 번호를 얻을 수 있고 대답을 얻으려고 사용할 수 있습니다.'answerPairs [number]' – furas

+0

BTW : 변수에 값을 표시하고 코드의 일부분을 표시하기 위해 항상'print()'를 사용할 수 있습니다. print ("getChoice")','print (isInside (point, rectangle))')를 실행하면 프로그램이 올바른 값을 가지고 있는지 그리고 올바른 순서로 코드를 실행하는지 알 수 있습니다. 이것을''print debugging ''이라고 부른다. – furas

+0

그래서 분명히하기 위해 다음과 같이 수정해야한다고 말하고 있습니다 : 'choicePairs = list (button, color, number, answer)''buttonSetup = makeButtonSetup ([ 'red', 'green', 'blue', (x, y), 80, 30, color, win)' '(x, y, color) button 세트에서 : 버튼 세트에서 : 'button = makeColoredRect {blue :}, {4 : '보라색}}, {5 : 빨강}}, {2 :'녹색 '}, {3 : 'orange'}, {6 : 'yellow'}' – hoshizuku

답변

0

나는 코드의 일부를 잘라 다른 정보를 유지해야 choicePairs``예를

from graphics import * 
from random import randint 
import graphics 

# --- functions --- 

def makeColoredRect(corner, width, height, color, win): 
    corner2 = corner.clone() 
    corner2.move(width, -height) 

    rect = Rectangle(corner, corner2) 
    rect.setFill(color) 
    rect.draw(win) 

    return rect 

def makeText(text_position, text_string, win): 
    text = Text(text_position, text_string) 
    text.setSize(10) 
    text.draw(win) 

    return text 

def makeButtons(buttonsSetup, win): 
    buttons = list() 

    x = 810 
    y = 350 

    for answer, color, text_string in buttonsSetup: 
     # create button - rect & text 
     rect = makeColoredRect(Point(x, y), 80, 30, color, win) 

     text = makeText(Point(x+40, y-15), text_string, win) 

     buttons.append((answer, rect, text)) 

     y = y - 40 

    return buttons 

def isBetween(x, end1, end2): 
    return end1 <= x <= end2 or end2 <= x <= end1 

def isInside(point, rect): 
    pt1 = rect.getP1() 
    pt2 = rect.getP2() 
    return isBetween(point.getX(), pt1.getX(), pt2.getX()) and \ 
     isBetween(point.getY(), pt1.getY(), pt2.getY()) 

def getChoice(buttons, default, win): 
    point = win.getMouse() 

    for answer, rect, text in buttons: 
     if isInside(point, rect): 
      print('selected:', answer, text.getText()) 
      return answer 

    return default 

def checkAnswer(numFaces, answer, points):  
    print('points before:', points) 

    if numFaces == answer: 
     correctAnswer = True 
     points = points + 10 
    else: 
     correctAnswer = False 
     points = points - 10 

    print('correct answer:', correctAnswer) 
    print('points after:', points) 

    return points, correctAnswer 

# --- main ------------------------------------------------ 

points = 0 
correctAnswer = None 

buttonsSetup = [ 
# (answer, color, text_string) 
    (1,  'red', "One Face"), 
    (2,  'green', "Two Faces"), 
    (3,  'blue', "Three Faces"), 
    (4,  'purple', "Four Faces"), 
    (5,  'orange', "Five Faces"), 
    (6,  'yellow', "Six Faces"), 
    ('exit', 'white', "EXIT"), 
] 

# - start - 

win = graphics.GraphWin(width=1000, height=600) 

# - objects - 

# it create list with elements `(answer, rect, text)` 
buttons = makeButtons(buttonsSetup, win) 

# select new number of faces 

numFaces = randint(1, 6) 
print('----') 
print('numFaces:', numFaces) 

# - mainloop - 

while True: 

    print('----') 

    # check which button was clicked 
    answer = getChoice(buttons, '', win) 

    # exit `while` loop 
    if answer == 'exit': 
     break 

    if answer != '': 
     # compare selected answer with correct answer and change points 
     points, correctAnswer = checkAnswer(numFaces, answer, points) 

     if correctAnswer: 
      # select new number of faces 
      numFaces = randint(1, 6) 
      print('----') 
      print('numFaces:', numFaces) 

# - exit - 

win.close()