您好,登錄后才能下訂單哦!
這篇文章主要講解了Python如何實現(xiàn)彈球小游戲,內容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。
整個游戲實現(xiàn)比較簡單,只需在安裝python的電腦上即可運行,玩游戲,通過鍵盤鍵控制彈球擋板的移動即可。原理不多說,且讓我們去看看吧。
1、代碼運行后,游戲界面如下所示:
2、游戲過程中,界面如下所示:
3、游戲結束后,界面如下所示:
游戲實現(xiàn)部分源碼如下:
def main(): tk = tkinter.Tk() # call back for Quit def callback(): if mb.askokcancel("Quit", "Do you really wish to quit?"): Ball.flag = False tk.destroy() tk.protocol("WM_DELETE_WINDOW", callback) # Init parms in Canvas canvas_width = 600 canvas_hight = 500 tk.title("小彈球游戲V1版") tk.resizable(0, 0) tk.wm_attributes("-topmost", 1) canvas = tkinter.Canvas(tk, width=canvas_width, height=canvas_hight, bd=0, highlightthickness=0, bg='#00ffff') canvas.pack() tk.update() score = Score(canvas, 'red') paddle = Paddle(canvas, "magenta") ball = Ball(canvas, paddle, score, "grey") game_over_text = canvas.create_text(canvas_width / 2, canvas_hight / 2, text='Game over', state='hidden', fill='red', font=(None, 18, "bold")) introduce = '歡迎來到小彈球游戲 V1版:\n點擊任意鍵--開始\n停止--回車鍵\n繼續(xù)--回車鍵\n' game_start_text = canvas.create_text(canvas_width / 2, canvas_hight / 2, text=introduce, state='normal', fill='magenta', font=(None, 18, "bold")) while True: if (ball.hit_bottom == False) and ball.paddle.started: canvas.itemconfigure(game_start_text, state='hidden') ball.draw() paddle.draw() if ball.hit_bottom == True: time.sleep(0.1) canvas.itemconfigure(game_over_text, state='normal') tk.update_idletasks() tk.update() time.sleep(0.01) if __name__ == '__main__': main()
看完上述內容,是不是對Python如何實現(xiàn)彈球小游戲有進一步的了解,如果還想學習更多內容,歡迎關注億速云行業(yè)資訊頻道。
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。