ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • thkinter을 이용한 간단한 미니게임
    기타/파이썬 2021. 1. 14. 15:34

    말할 수 없이 허접하지만 만들고나니 대충 어떤 식으로 돌아가는지 알게 되었다.

    만든 것은 10초동안 버튼을 열심히 누르면 점수에 따라 평가가 나오는 미니게임!

     

     

     

    from tkinter import *
    from tkinter import messagebox
    import threading,time 
    
    root = Tk()
    root.title("10초 동안 버튼 때리기")
    root.geometry("200x200+500+100")
    
    count = 0
    Stime = 10
    
    def play():
        btn2.place(x=60, y=80)
        btn.place(x=-1000, y=-1000)
        global Stime
        global count
        if Stime > 0:
            root.after(1000, play)
            Stime -= 1     
            timebox.config(text=f"남은 시간 {Stime}초")
        else:
            Stime = 0   
            if count < 10:
                point = str(count) + "점 입니다. \n좀 더 분발해주세요"
            elif 11 < count < 20 :
                point = str(count) + "점 입니다. \n꽤 하시는데요"
            else:
                point = str(count) + "점 입니다. \n메크로를 사용하셨나요?"
            messagebox.showinfo("점수", point)
            btn3.place(x=60, y=80)
    
    def score():
        global count
        global Stime
        if Stime > 0:
            count += 1
            pointbox.config(text=count)
     
    def replay():
        global count
        global Stime
        Stime = 10
        count =0
        btn3.place(x=-1000, y=-1000)
        btn.place(x=60, y=80)
             
    #타이틀 
    title = Label(root, text="10초 동안 버튼 때리기")
    title.pack()
    
    #시간
    timebox = Label(root, text=f"남은 시간 {Stime}초")
    timebox.pack()
    
    #점수
    pointbox = Label(root, text=count)
    pointbox.pack()
    
    #버튼
    btn2 = Button(root, text="누르세요" , width=10, height=2, command=score)
    btn = Button(root, text="시작" , width=10, height=2, command=play)
    btn3 = Button(root, text="다시하기" , width=10, height=2, command=replay)
    btn.place(x=60, y=80)
    btn2.place(x=-1000, y=-1000)
    
    #mainloop()는 단순히 코드 마지막이 아니라 프로그램 흐름 마지막단에 위치
    root.mainloop()
    

    위에는 전체 코드이고 이제 하나하나 분석해보자

     

    우선 tkinter 창을 만들기 위해 아래 코드를 만든다.

    root = Tk()
    root.title("10초 동안 버튼 때리기")
    root.geometry("200x200+500+100")
    
    #mainloop()는 단순히 코드 마지막이 아니라 프로그램 흐름 마지막단에 위치
    root.mainloop()

    .title은 닫기버튼 줄의 타이틀을 나타낸다.

    .geometry("가로크기x세로크기+x좌표 위치+y좌표 위치")

    여기서 x, y는 모니터를 기준으로 한다.

     

     

    #타이틀 
    title = Label(root, text="10초 동안 버튼 때리기")
    title.pack()
    
    #시간
    timebox = Label(root, text=f"남은 시간 {Stime}초")
    timebox.pack()
    
    #점수
    pointbox = Label(root, text=count)
    pointbox.pack()
    
    #버튼
    btn2 = Button(root, text="누르세요" , width=10, height=2, command=score)
    btn = Button(root, text="시작" , width=10, height=2, command=play)
    btn3 = Button(root, text="다시하기" , width=10, height=2, command=replay)
    btn.place(x=60, y=80)
    btn2.place(x=-1000, y=-1000)

    다음은 버튼 만들기.

    필요한 건 게임 타이틀, 시간, 점수, 실행버튼이다.

    버튼은 command="함수이름" 으로 하면 클릭시 해당 함수를 실행할 수 있다.

     

    count = 0
    Stime = 10
    

    함수를 만들기 전 함수에서 쓸 전역 변수를 설정해준다.

     

     

    def play():
        btn2.place(x=60, y=80)
        btn.place(x=-1000, y=-1000)
        global Stime
        global count
        if Stime > 0:
            root.after(1000, play)
            Stime -= 1     
            timebox.config(text=f"남은 시간 {Stime}초")
        else:
            Stime = 0   
            if count < 10:
                point = str(count) + "점 입니다. \n좀 더 분발해주세요"
            elif 11 < count < 20 :
                point = str(count) + "점 입니다. \n꽤 하시는데요"
            else:
                point = str(count) + "점 입니다. \n메크로를 사용하셨나요?"
            messagebox.showinfo("점수", point)
            btn3.place(x=60, y=80)

    시작 버튼을 누르면 실행되는 함수.

    시작을 누르면 시작 버튼 위에 누르세요 버튼을 올려준다.

    global을 이용해 전역변수를 가져와 사용하고

     

    if 문으로 1초당 Stime이 1초씩 줄어들다 0이 되면 멈춘다.

    시간이 0초가 되면 count 점수에 따라 messagebox의 문구가 달라진다.

    마지막으로 다시하기 버튼이 위로 올라와 실수로라도 카운트가 올라가지 않게 해준다

     

    여기 부분에 시간을 좀 썼는데 after 함수를 제대로 쓸 줄 몰라 조금 해매었다 ㅠㅠ

    이리저리 조합하다가 방법을 알고는 넘나 허무 ㅋㅋㅋ

    after함수는 for문을 사용하지 않아도 자동으로 루프되서 편한 것 같다.

    처음 sleep이랑 for문을 쓰니 시간이 10초 뒤에 0으로 바뀌어서 화가났던 ㅋㅋㅋ

     

     

    def score():
        global count
        global Stime
        if Stime > 0:
            count += 1
            pointbox.config(text=count)

    Stime 이 0초가 되기 전까진 클릭 당 카운트가 1씩 증가하게 해준다

     

     

    def replay():
        global count
        global Stime
        Stime = 10
        count =0
        btn3.place(x=-1000, y=-1000)
        btn.place(x=60, y=80)

    다시 시작하기 위해 시간과 카운트를 초기로 설정 한 후 

    시작 버튼을 보여준다

     

    댓글

Designed by Tistory.