網(wǎng)站首頁 編程語言 正文
一、彈球游戲代碼?
下文是tkinter的應(yīng)用實(shí)例,實(shí)現(xiàn)彈球游戲,通過<--和-->件移動(dòng)平板接球。
from tkinter import * import random import time # Creating the window: window = Tk() window.title("Bounce") window.geometry('600x600') window.resizable(False, False) # Creating the canvas containing the game: canvas = Canvas(window, width = 450, height = 450, bg = "black") canvas.pack(padx = 50, pady= 50) score = canvas.create_text(10, 20, fill = "white") window.update() class Ball: def __init__(self, canvas1, paddle1, color): self.canvas = canvas1 self.paddle = paddle1 self.id = canvas1.create_oval(10, 10, 25, 25, fill = color) # The starting point of the ball self.canvas.move(self.id, 190, 160) starting_direction = [-3, -2, -1, 0, 1, 2, 3] random.shuffle(starting_direction) self.x = starting_direction[0] self.y = -3 self.canvas_height = self.canvas.winfo_height() self.canvas_width = self.canvas.winfo_width() # Detecting the collision between the ball and the paddle: def hit_paddle(self, ballcoords): paddle_pos = self.canvas.coords(self.paddle.id) if ballcoords[0] <= paddle_pos[2] and ballcoords[2] >= paddle_pos[0]: if paddle_pos[3] >= ballcoords[3] >= paddle_pos[1]: return True return False # Detecting the collision between the the ball and the canvas sides: def draw(self): self.canvas.move(self.id, self.x, self.y) ballcoords = self.canvas.coords(self.id) if ballcoords[1] <= 0: self.y = 3 if ballcoords[3] >= self.canvas_height: self.y = 0 self.x = 0 self.canvas.create_text(225, 150, text = "Game Over!", font = ("Arial", 16), fill = "white") if ballcoords[0] <= 0: self.x = 3 if ballcoords[2] >= self.canvas_width: self.x = -3 if self.hit_paddle(ballcoords): self.y = -3 class Paddle: def __init__(self, canvas1, color): self.canvas1 = canvas self.id = canvas.create_rectangle(0, 0, 100, 10, fill = color) self.canvas1.move(self.id, 180, 350) self.x = 0 self.y = 0 self.canvas1_width = canvas1.winfo_width() self.canvas1.bind_all("<Left>", self.left) self.canvas1.bind_all("<Right>", self.right) def draw(self): self.canvas1.move(self.id, self.x, 0) paddlecoords = self.canvas1.coords(self.id) if paddlecoords[0] <= 0: self.x = 0 if paddlecoords[2] >= self.canvas1_width: self.x = 0 def right(self, event): self.x = 3 def left(self, event): self.x = -3 paddle = Paddle(canvas, color = "white") ball = Ball(canvas, paddle, color = "red") # New code after here def handler(): global run run = False window.protocol("WM_DELETE_WINDOW", handler) run = True while run: # New code before here ball.draw() paddle.draw() window.update_idletasks() window.update() time.sleep(0.01) window.destroy() # should always destroy window before exit
二、程序結(jié)果?
總結(jié)
原文鏈接:https://blog.csdn.net/gongdiwudu/article/details/122530718
相關(guān)推薦
- 2022-05-07 MVC中Action方法的返回類型介紹_基礎(chǔ)應(yīng)用
- 2022-05-24 Asp.net?core?使用SignalR推送消息過程詳解_實(shí)用技巧
- 2022-03-23 C語言?scanf的工作原理詳解_C 語言
- 2022-05-19 C++的內(nèi)聯(lián)函數(shù)你了解嗎_C 語言
- 2022-04-25 Pytorch技法之繼承Subset類完成自定義數(shù)據(jù)拆分_python
- 2023-05-03 python?re.match函數(shù)的具體使用_python
- 2022-05-22 Nginx圖片服務(wù)器配置之后圖片訪問404的問題解決_nginx
- 2023-03-22 幾分鐘教你掌握Redis簡單動(dòng)態(tài)字符串SDS_Redis
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支