您好,登錄后才能下訂單哦!
這篇文章主要講解了“Python實現彈球小游戲代碼分享”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Python實現彈球小游戲代碼分享”吧!
整個游戲實現比較簡單,只需在安裝python的電腦上即可運行,玩游戲,通過鍵盤鍵控制彈球擋板的移動即可。原理不多說,且讓我們去看看吧。
1、代碼運行后,游戲界面如下所示:
2、游戲結束后,界面如下所示:
游戲實現部分源碼如下:
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繼續--回車鍵\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實現彈球小游戲代碼分享”的內容了,經過本文的學習后,相信大家對Python實現彈球小游戲代碼分享這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。