Python反弹球游戏代码

精华 发表在    Python教程部落 11-25 17:26:01

1 3164 6

代码:

from tkinter import *
import tkinter.messagebox
import random
import time
import pickle



class Ball:
    def __init__(self,canvas,paddle,color):
        self.canvas = canvas
        self.id = canvas.create_oval(25,25,45,45,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.endgame = False
        self.height = 500
        self.width = 500
        self.paddle = paddle
    def hit(self,pos):
        pa_pos = self.canvas.coords(self.paddle.id)
        if pos[2] >= pa_pos[0] and pos[0] <= pa_pos[2]:
            if pos[3] >= pa_pos[1] and pos[3] <= pa_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 = 3
        if pos[3] >= self.height:
            self.endgame = True
        if self.hit(pos) == True:
            self.y = -3
            text.score += 1
        if pos[0] <= 0:
            self.x = 3
        if pos[2] >= self.width:
            self.x = -3

class Paddle:
    def __init__(self,canvas,color):
        self.canvas = canvas
        self.id = canvas.create_rectangle(0,0,100,10,fill=color)
        self.canvas.move(self.id,200,300)
        self.x = 0
        self.canvas.bind_all("<KeyPress-Left>",self.left)
        self.canvas.bind_all("<KeyPress-Right>",self.right)
        self.width = 500

    def left(self,evt):
        self.x = -5
        
    def right(self,evt):
        self.x = 5
        
    def draw(self):
        self.canvas.move(self.id,self.x,0)
        pos = self.canvas.coords(self.id)
        if pos[0] <= 0:
            self.x = 0
        elif pos[2] >= self.width:
            self.x = 0
            
class Score_Text:
    def __init__(self,canvas,color,text):
        self.canvas = canvas
        self.text = text
        self.id = canvas.create_text(17,10,text=self.text,fill=color)
        self.score = 0
    def draw(self):
        self.canvas.itemconfig(self.id,state='normal',text='分数:'+str(self.score))
        
tk = Tk()
tk.title("反弹球游戏")
tk.resizable(0,0)
tk.wm_attributes("-topmost",1)
canvas = Canvas(tk,width=500,height=500,bd=0,highlightthickness=0)
canvas.pack()
image = PhotoImage(file="im.png")
h = image.height()
w = image.width()
for x in range(0,5):
    for y in range(0,5):
        canvas.create_image(x*w,y*h,image=image,anchor='nw')
                                  
tk.update()

paddle = Paddle(canvas,'blue')
ball = Ball(canvas,paddle,'red')
text = Score_Text(canvas,'black','分数:0')

while True:
    if ball.endgame == False:
        ball.draw()
        paddle.draw()
        text.draw()
    else:
        tkinter.messagebox.showinfo('提示','失败!')
        try:
            exit()
        except:
            exit()
    tk.update()
    tk.update_idletasks()
    time.sleep(0.01)

注意,还要再加上这个图片,程序需要它,不然会报错

壁纸


谢谢!

登录或注册后发布评论