ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 파이썬 tkinter GUI 디비를 이용한 간단한 게임
    기타/파이썬 2021. 1. 19. 16:51
    '''
    Created on 2021. 1. 19.
    
    @author: pc368
    '''
    import cx_Oracle 
    from tkinter import *
    import tkinter
    from tkinter import messagebox
    import threading,time 
    import tkinter.font as tkFont
    
    #오라클 디비 연결
    dsn = cx_Oracle.makedsn("localhost",1521,"xe")
    db = cx_Oracle.connect("testid","testid", dsn)
    
    cursor = db.cursor()
    
    ##### GUI 게임
    root = Tk()
    root.title("10초 동안 버튼 때리기")
    root.geometry("400x200+500+300")
    root.resizable(False, False)
    
    #폰트스타일
    fontStyle = tkFont.Font(family="나눔고딕", size=15)
    
    #초기값 설정
    count = 0
    Stime = 10
    myname =  StringVar()
    
    
    #게임에 참여할 이름 설정
    def inName():
        global name
        name_btn.place_forget()
        ninfo.place_forget()
        entry.place_forget()
        name = str(myname.get())
        
        
    #게임 플레이 함수    
    def play():
        btn2.place(x=120, y=90)
        btn.place_forget()
        timebox.pack()
        pointbox.pack()
        global Stime
        global count
        global name
        global point
        if Stime > 0:
            root.after(1000, play)
            Stime -= 1     
            timebox.config(text=f"남은 시간 {Stime}초")
        else:
            Stime = 0   
            
            #결과 표시를 위해 시간과 점수는 감추기
            timebox.pack_forget()
            pointbox.pack_forget()
            
            #랭킹조회 (현재 점수보다 높은 점수를 가진사람들을 조회한 후 +1)
            #조회 결과는 리스트 안의 튜플로 나온다
            cursor.execute( f''' SELECT COUNT(NAME) FROM rank WHERE point > {count} ''' )
            myrank = cursor.fetchall()
            ranking = myrank[0][0] + 1
            point = name+ "님의 점수는" + str(count) + "점 이며 \n 현제 랭킹은 " + str(ranking) + "등 입니다"
            result_score.configure(text=point)
            result_score.pack()
            
            #점수등록 버튼, 다시하기 버튼
            btn3.place(x=120, y=90)
            btn4.place(x=120, y=140)
    
    
    
    #점수와 이름을 INSERT
    def inscore():
        global count
        global name
        #본인 이름과 점수를 데이터베이스에 등록
        sql = f'''INSERT INTO rank (no, name, point) VALUES(NO_SEQ.NEXTVAL, '{name}', {count})'''
        cursor.execute(sql)
        db.commit() 
        
        
        
    #클릭한 횟수를 카운트 해줌    
    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
        result_score.pack_forget()
        btn3.place_forget()
        btn4.place_forget()
        timebox.pack()
        pointbox.pack()
        btn.place(x=120, y=90)
    
    
         
    #게임 타이틀 
    title = Label(root, text="10초 동안 버튼 때리기", anchor="center", font=fontStyle)
    title.pack()
    
    #이름입력하기
    ninfo = Label(root, text="이름: ")
    ninfo.place(x=90, y=50)
    entry = tkinter.Entry(root, textvariable=myname )
    entry.place(x=130, y=50)
    
    #시간
    timebox = Label(root, text=f"남은 시간 {Stime}초")
    
    #점수
    pointbox = Label(root, text=count)
    
    #카운트가 올라가는 버튼
    btn2 = Button(root, text="누르세요" , width=20, height=2, command=score)
    
    #게임 시작 버튼
    btn = Button(root, text="시작" , width=20, height=2, command=play)
    btn.place(x=120, y=90)
    
    #게임 다시하기 버튼
    btn3 = Button(root, text="다시하기" , width=20, height=2, command=replay)
    btn2.place_forget()
    
    #이름을 입력하는 버튼
    name_btn = Button(root, text="이름입력" , width=20, height=2 ,command = inName)
    name_btn.place(x=120, y=90)
    
    #점수를 데이터베이스에 등록
    btn4 = Button(root, text="점수등록" , width=20, height=2, command=inscore)
    result_score = Label(text="")
    
    #mainloop()는 단순히 코드 마지막이 아니라 프로그램 흐름 마지막단에 위치
    root.mainloop()
    
    
    #디비 작업종료
    cursor.close()
    db.close()

    오라클 데이터 베이스와 GUI를 연결한 결과물!

    게임 실행시 이름을 작성

    게임이 끝난뒤 본인의 점수와 랭킹이 나온다

    점수등록 버튼을 누르면 해당 정보가 데이터베이스에 쌓인다

    다시하기를 누르면 정보는 쌓이지 않고 새롭게 게임을 즐길 수 있다.

     

    원래 데이터베이스 수정을 이용하여 게임클리어시 기록이 바로 저장되고

    다시 하기의 경우 점수가 예전 점수보다 높을 경우에만 업데이트 해야하는데

    시간이 부족해서 그 기능까지는 구현하지 못했다 

     

    나중에 시간이 나면은 구현할 계획

     

     

    '기타 > 파이썬' 카테고리의 다른 글

    thkinter을 이용한 간단한 미니게임  (0) 2021.01.14
    파이썬 장고의 정의  (0) 2021.01.12
    파이썬 장고 정리내용  (0) 2021.01.08
    파이썬 장고 블로그 앱 만들기  (0) 2021.01.08
    파이썬 장고 - 북마크 만들기  (0) 2020.12.29

    댓글

Designed by Tistory.