Python Tkinter弹跳球类游戏res

2024-09-23 22:19:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在读一本书,名叫《孩子的Python》,书中一步步讲述了如何创造弹跳球游戏。 最后的代码如下:

from Tkinter import *
import random

tk = Tk()
tk.title("Paddleball Game")
tk.resizable(0,0) # tk window cannot be resized in x or y
tk.wm_attributes("-topmost", 1)

canvas = Canvas(tk, width=500, height=400, bd=0, highlightthickness=0) #no borders around the canvas
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):
        if self.hit_bottom == False:

            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.canvas_height:
                self.y = -3

            if pos[3] >= self.canvas_height:
                self.hit_bottom = True

            if self.hit_paddle(pos) == True:
                self.y = -3

            if pos[0] <= 0:
                self.x = 3
            if pos[2] >= self.canvas_width:
                self.x = -3

            if ball.hit_bottom == False:
                self.canvas.after(10, self.draw) # miliseconds, function

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_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):
        if ball.hit_bottom == False:

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

            pos = self.canvas.coords(self.id)
            if pos[0] <= 0:
                self.x = 0
            if pos[2] >= self.canvas_width:
                self.x = 0

            self.canvas.after(10, self.draw)

    def turn_left(self, event):
        self.pos = self.canvas.coords(self.id)

        if self.pos[0] >= 1:
            self.x = -3

    def turn_right(self, event):
        self.pos = self.canvas.coords(self.id)

        if self.pos[2] <= self.canvas_width-1:
            self.x = 3

paddle = Paddle(canvas, "blue")
ball = Ball(canvas, paddle, "red")


def start_game(event):
    ball.draw()

canvas.bind_all("<Button-1>", start_game)
paddle.draw()

tk.mainloop()

现在,在读完这本书并了解了大部分内容之后,我试着单独重新创建游戏,并添加一些额外的内容。1)当我输了时,显示游戏结束,2)计算分数并显示,3)延迟开始(1秒),4)在我输后停止球和桨的移动。 而且,我这样做是为了当我输了,如果我再次点击窗口,游戏中的文字就会消失,比分变为零,划桨和球又开始移动。

但是,我希望球从原来的位置开始,而不是从底部开始,如果可能的话,当我输了时,显示一个按钮,这样当我按下它时,游戏重新开始。

我的代码是:

from Tkinter import *
import random
import time

root = Tk()
root.title("Testing... testing...")
root.resizable(0,0)
root.wm_attributes("-topmost", -1)

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

root.update()

count = 0
lost = False

class Ball:
    def __init__(self, canvas, paddle, color):
        self.canvas = canvas
        self.paddle = paddle
        self.id = canvas.create_oval(0, 0, 15, 15, fill=color)
        self.canvas.move(self.id, 245, 200)

        starts_x = [-3, -2, -1, 1, 2, 3]
        random.shuffle(starts_x)

        self.x = starts_x[0]
        self.y = -3

        self.canvas_height = self.canvas.winfo_height()
        self.canvas_width = self.canvas.winfo_width()


    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.canvas_height:
            self.y = -3

        if pos[0] <= 0:
            self.x = 3
        if pos[2] >= self.canvas_width:
            self.x = -3

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


        if pos[2] >= self.paddle_pos[0] and pos[0] <= self.paddle_pos[2]:
            if pos[3] >= self.paddle_pos[1] and pos[3] <= self.paddle_pos[3]:
                self.y = -3
                global count
                count +=1
                score()


        if pos[3] <= self.canvas_height:
            self.canvas.after(10, self.draw)
        else:
            game_over()
            global lost
            lost = True


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_width = self.canvas.winfo_width()

        self.canvas.bind_all("<KeyPress-Left>", self.move_left)
        self.canvas.bind_all("<KeyPress-Right>", self.move_right)

    def draw(self):
        self.canvas.move(self.id, self.x, 0)

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

        if self.pos[0] <= 0:
            self.x = 0
        if self.pos[2] >= self.canvas_width:
            self.x = 0
        global lost
        if lost == False:
            self.canvas.after(10, self.draw)

    def move_left(self, event):
        if self.pos[0] >= 0:
            self.x = -3

    def move_right(self, event):
        if self.pos[2] <= self.canvas_width:
            self.x = 3


def start_game(event):
    global lost, count
    lost = False
    count = 0
    score()
    canvas.itemconfig(game, text=" ")

    time.sleep(1)
    paddle.draw()
    ball.draw()


def score():
    canvas.itemconfig(score_now, text="score: " + str(count))

def game_over():
    canvas.itemconfig(game, text="Game over!")


paddle = Paddle(canvas, "blue")
ball = Ball(canvas, paddle, "red")


score_now = canvas.create_text(430, 20, text="score: " + str(count), fill = "red", font=("Arial", 16))
game = canvas.create_text(250, 150, text=" ", fill="red", font=("Arial", 20))


canvas.bind_all("<Button-1>", start_game)

root.mainloop()

有人能帮我吗? 谢谢。

编辑:我想我需要一种方法,在我输球后取消抽签,在比赛开始时重新抽签!但我不知道怎么做。。。

我试着在开始游戏函数中插入ball = Ball(canvas, paddle, "red"),但这将绘制一个新球并将旧球保持在底部。。。


Tags: posselfidgamefalsemoveifdef
2条回答

另外,如果你需要“解开”这个球,用canvas.delete(ball.id)在我头顶上canvas.itemconfig(state=Tkinter.HIDDEN)应该可以

如果有其他人试图解决这个问题,但没有解决,我就找到了解决办法。

我编辑了两件事,如下:

在Ball类的draw()函数中,编辑了以下行:

if pos[3] <= self.canvas_height:
            self.canvas.after(10, self.draw)
        else:
            self.canvas.move(self.id, 245, 200) #added this line
            game_over()
            global lost
            lost = True

还编辑了start_game()函数,如下所示:

def start_game(event):
    global lost, count, ball #added ball here
    if lost == True: # added this if
        ball = Ball(canvas, paddle, "red")

    lost = False #and finally changed the lost var BEFORE drawing the paddle which has a check of lost var in order to move.
    paddle.draw()
    ball.draw()
    count = 0

    score()
    canvas.itemconfig(game, text=" ")

    time.sleep(1)

欢迎更多帮助/回答!

相关问题 更多 >