简体   繁体   English

开始/游戏结束屏幕

[英]Start/game over screen

I am currently making a simple arcade game as I'm beginning to learn how to code.我目前正在制作一个简单的街机游戏,因为我开始学习如何编码。 The "gameplay" in itself is done, but I want to add a start/game over screen. “游戏玩法”本身已经完成,但我想添加一个开始/游戏结束屏幕。 I'm also wondering if I should use create_text, state or itemconfig?我还想知道是否应该使用 create_text、state 或 itemconfig? I am new to programming, so my program might not be the best.我是编程新手,所以我的程序可能不是最好的。 The code that I made is around 90lines long.我编写的代码长约 90 行。 So does anyone have any suggestions on how I should do that (I don't have a clue) PS: For some reasons I used tkinter那么有人对我应该如何做有任何建议(我不知道) PS:出于某些原因,我使用了 tkinter

from tkinter import *
import random
import time


tk = Tk()
tk.title("Game")
tk.resizable(0, 0)
tk.wm_attributes("-topmost", 1)
canvas = 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 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 self.hit_paddle(pos) == True:
            self.y = -3
        if pos[3] >= self.canvas_height:
            self.hit_bottom = True
        if pos[0] <= 0:
            self.x = 3
        if pos[2] >= self.canvas_width:
            self.x = -3

    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




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 turn_left(self, evt):
        self.x = self.rect =-2

    def turn_right(self, evt):
        self.x = 2

    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


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

while 1:
    if ball.hit_bottom == False:
        ball.draw()
        paddle.draw()
    tk.update_idletasks()
    tk.update()
    time.sleep(0.01)

You are on the right track.你在正确的轨道上。 Here you can see I added a game_over function.在这里你可以看到我添加了一个game_over function。 I also changed the way your program works.我还改变了你的程序的工作方式。 Instead of using a while loop and sleep.而不是使用while循环和睡眠。 I use the tkinter method mainloop().我使用 tkinter 方法 mainloop()。 Then to run code after tk is added to the mainloop I used the.after() method.然后在将 tk 添加到主循环后运行代码,我使用了 .after() 方法。 After 10 milliseconds the program continues. 10 毫秒后,程序继续。 It then has to continue to call itself in the if statement.然后它必须继续在 if 语句中调用自己。 Whenever the ball hits the bottom it calls the game_over() function instead of the loop() function.每当球击中底部时,它都会调用 game_over() function 而不是 loop() function。 You could add a reset button or something in the the game_over() function and have it call itself until it is pressed.您可以在 game_over() function 中添加一个重置按钮或其他东西,并让它自己调用,直到它被按下。 Using these callback function you change the flow of your program without having and checking a lot of bool values in your while loop to see what your program is supposed to be doing.使用这些回调 function 您可以更改程序的流程,而无需检查您的 while 循环中的大量 bool 值以查看您的程序应该做什么。

from tkinter import *
import random


tk = Tk()
tk.title("Game")
tk.resizable(0, 0)
tk.wm_attributes("-topmost", 1)
canvas = 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 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 self.hit_paddle(pos) == True:
            self.y = -3
        if pos[3] >= self.canvas_height:
            self.hit_bottom = True
        if pos[0] <= 0:
            self.x = 3
        if pos[2] >= self.canvas_width:
            self.x = -3

    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




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 turn_left(self, evt):
        self.x = self.rect =-2

    def turn_right(self, evt):
        self.x = 2

    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


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


def game_over():
    the_text = canvas.create_text(100, 100, anchor="nw")
    canvas.itemconfig(the_text, text="Game Over", width=500)
    canvas.itemconfig(the_text, font=("courier", 50))


def loop():
    if not ball.hit_bottom:
        ball.draw()
        paddle.draw()
        tk.after(10, loop)
    else:
        game_over()


loop()
tk.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM