• 个人简介

    蒟蒻一个,请多指教 本人不玩游戏,python一般,c++不会。 蒟蒻一个,请多指教。 一个python小游戏

    from tkinter import*
    from tkinter import messagebox
    import random
    import time
    
    class Ball:
        def __init__(self,cv,paddle,color):
            self.canvas = cv
            self.paddle = paddle
            self.id = cv.create_oval(10,10,25,25,fill = color)
            self.canvas.move(self.id,200,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):
            global score
            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]:
                    score += 1
                    return True
            return False
    
        def draw(bsb):
            bsb.canvas.move(bsb.id,bsb.x,bsb.y)
            pos = bsb.canvas.coords(bsb.id)
            if pos[0] <= 0:
                bsb.x = 3
            if pos[2] >= bsb.canvas_width:
                bsb.x = -3
            if bsb.hit_paddle(pos) == True:
                bsb.y = -3
            if pos[1] <= 0:
                bsb.y = 3
            if pos[3] >= bsb.canvas_height:
                bsb.hit_bottom = True
    
    class Paddle:
        def __init__(self,cv,color):
            self.canvas = cv
            self.id = cv.create_rectangle(0,0,100,10,fill = color)
            self.canvas.move(self.id,200,300)
            self.x = 0
            self.canvas_width = self.canvas.winfo_width()
            self.canvas.bind_all('<KeyPress-Left>',self.turn_left)
            self.canvas.bind_all('<KeyPress-Right>',self.turn_right)
    
        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.canvas_width:
                self.x = 0
    
        def turn_left(self,evt):
            self.x = -3
    
        def turn_right(self,evt):
            self.x = 3
    
    root = Tk(className = '弹球游戏')
    root.resizable(0,0)
    score = 0
    cv = Canvas(root,width = 500,height = 400,bd = 0,highlightthickness = 0)
    cv.pack()
    root.update()
    
    paddle = Paddle(cv,"green")
    ball = Ball(cv,paddle,"red")
    
    while 1:
        if ball.hit_bottom == False:
            ball.draw()
            paddle.draw()
        else:
            messagebox.showinfo(title = '游戏结束',message = "你的分数为:" + str(score))
            break
        root.update_idletasks()
        root.update()
        time.sleep(0.01)
    

    小游戏,试一试(从书上抄下来的(用本地编辑器))

    image

    本人书法

  • 通过的题目

  • 最近活动

    This person is lazy and didn't join any contests or homework.
  • 最近编写的题解

    This person is lazy and didn't write any solutions.