python拍3游戏_Python 3.6.3 球拍游戏源代码 调试通过

本文提供了一段使用Python 3.6.3实现的球拍游戏完整源代码,游戏涉及球与球拍的碰撞检测。通过导入`tkinter`库创建图形界面,利用随机数确定球的初始运动方向,并在窗口中实时更新球的位置。代码已调试通过,适合初学者学习和理解游戏编程。
摘要由CSDN通过智能技术生成

Python 3.6.3 球拍游戏源代码调试通过import tkinter

import time

import random

tk=http://www.doczj.com/doc/23bfef3c54270722192e453610661ed9ad5155b4.html()

tk.title('GAME')

tk.resizable(0,0)

tk.wm_attributes("-topmost",-1)

canvas=tkinter.Canvas(tk,width=500,height=400,bd=0,highlightthickness=0) canvas.pack()

tk.update()

class Ball:

def __init__(self,canvas,paddle,color):

self.canvas=canvas

self.paddle=paddle

self.id=canvas.create_oval(10,10,25,25,fill=color)

self.canvas.move(self.id,245,100)

starts=[-3,-2,-1,1,2,3]

random.shuffle(starts)

self.x=starts[0]

self.y=-3

self.canvas_height=self.canvas.winfo_height()

self.canvas_width=self.canvas.winfo_width()

self.hit_bottom=False

def hit_paddle(self,pos):

paddle_pos=self.canvas.coords(self.paddle.id)

if pos[2]>=paddle_pos[0] and pos[0]<=paddle_pos[2]:

if pos[3]>=paddle_pos[1] and pos[3]<=paddle_pos[3]:

return True

return False

def draw(self):

self.canvas.move(self.id,self.x,self.y)

pos=self.canvas.coords(self.id)

if pos[1]<=0:

self.y=1

if pos[3]>=self.canvas_height:

self.hit_bottom=True

if self.hit_paddle(pos)==True:

self.y=-2

if pos[0]<=0:

self.x=1

if pos[2]>=self.canvas_width:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值