0

所以我正在制作一个基本的弹球游戏。我试图让它每 10 秒就有一个项目从屏幕上掉下来。我通过time_score每秒上升一次来做到这一点。在第 34 行,它应该每十秒激活一次。但只是在开始时才这样做。为什么它不是每 10 秒执行一次?

`from tkinter import *
import random
import time
import pickle

tk = Tk()
tk.title("Game")
tk.resizable(0,0)
tk.wm_attributes("-topmost", 1)
canvas = Canvas (tk, width=1500,height=700, bd=0, highlightthickness=0)
canvas.pack()
game_over = canvas.create_text(750,350, text='GAME OVER', font=('Helvetica', 100,), fill='red')
canvas.itemconfig(game_over, state='hidden')
time_score=0
tk.update()
speeder = 0
ClickTS = canvas.create_text(750,350, text='CLICK TO START', font=('Helvetica', 70,), fill='blue')
load_file = open('C:\\Users\\hatchfamily\\Desktop\\Python\\Saved Functions\\Saves\\save.dat','rb')
high_score = round(pickle.load(load_file))
load_file.close()
paddle_add=150

class Item:
    def __init__(self,canvas,paddle):
        self.canvas=canvas
        self.paddle = paddle
        self.id= canvas.create_polygon(10,0,20,20,0,20, outline='black', fill='cyan')
        self.canvas.move(self.id, random.randint(100,1300), -22)
        self.y = 2
        pos = self.canvas.coords(self.id)
    def draw(self):
        pos = self.canvas.coords(self.id)
        self.canvas.move(self.id, 0, self.y)
        if (time_score) % 10 == 0:
            print('pie')
            canvas.itemconfig(item.id, state='normal')
            self.canvas.move(self.id, random.randint(100,1300), -22)
    def hit_paddle(self, pos):
        paddle_pos = self.canvas.coords(self.paddle.id)
        pos = self.canvas.coords(self.id)
        if pos[4] >= paddle_pos[0] and pos[2]<= paddle_pos[2]:
            if pos[3] == ((paddle_pos[1]+1) or (paddle_pos[1]-1)) and pos[3] <= paddle_pos[3]:
                return True
            return False
    def re_create(self):
        if round(time_score) % 10 == 0:
            print('lobster')
            canvas.itemconfig(item.id, state='normal')
            self.canvas.move(self.id, random.randint(100,1300), -22)


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, outline = "")
        self.canvas.move(self.id, random.randint(0,1500), random.randint(0,150))
        starts = [-3, 3]
        startsb = [-3, 3]
        random.shuffle(starts)
        random.shuffle(startsb)
        self.x = starts[0]
        self.y = startsb[0]
        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+speeder
        if self.hit_paddle(pos) == True:
            self.y = -3-speeder
        if pos[3] >= self.canvas_height:
            self.hit_bottom = True
        if pos[0] <= 0:
            self.x = 3+speeder
        if pos[2] >= self.canvas_width:
            self.x = -3-speeder

    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,130,15, fill=color)
        self.canvas.move(self.id, 750, 625)
        self.x = 0
        self.started = False
        self.canvas_width = self.canvas.winfo_width()
        self.canvas.bind_all('<KeyPress-a>', self.turn_left)
        self.canvas.bind_all('<KeyPress-Left>', self.turn_left)
        self.canvas.bind_all('<KeyPress-Right>', self.turn_right)
        self.canvas.bind_all('<KeyPress-d>', self.turn_right)
        self.canvas.bind_all('<Button-1>', self.start_game)
    def turn_left(self, evt):
        pos = self.canvas.coords(self.id)
        if pos[0] <= 0:
            self.x = 0
        else: self.x = -4-speeder

    def turn_right(self, evt):
        pos = self.canvas.coords(self.id)
        if pos [2] >= self.canvas_width:
            self.x= 0
        else: self.x = 4+speeder

    def start_game(self, evt):
        self.started= True

    def grow(self):
        pos = self.canvas.coords(self.id)
        global paddle_add
        paddle_add = paddle_add + 10
        self.canvas.coords(self.id, pos[0]-paddle_add,pos[1],pos[2]+paddle_add,pos[3])

    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
        if item.hit_paddle(pos) == True:
            canvas.itemconfig(item.id, state='hidden')
            self.grow()

paddle = Paddle(canvas, 'black')
ball = Ball(canvas, paddle, 'red')
item = Item(canvas, paddle)

while 1:
    if ball.hit_bottom == False and paddle.started == True:
        canvas.itemconfig(ClickTS, state='hidden')
        ball.draw()
        paddle.draw()
        item.draw()
        speeder = speeder + .001
        time_score = time_score + .01

    if ball.hit_bottom == True:
        time.sleep(1)
        canvas.itemconfig(game_over, state='normal')
        canvas.itemconfig(ball.id, state='hidden')
        canvas.itemconfig(paddle.id, state='hidden')
        canvas.create_text(750,500, text='Time: %s' % round(time_score), font=('Helvetica', 60,), fill='black')
        canvas.create_text(750,600, text='High Score: %s' % high_score, font=('Helvetica', 50,), fill='black')
        if time_score > high_score:
            high_score = time_score
            canvas.create_text(750,100, text='NEW HIGH SCORE', font=('Ariel', 60), fill='cyan')
            save_file = open('C:\\Users\\hatchfamily\\Desktop\\Python\\Saved Functions\\Saves\\save.dat','wb')
            pickle.dump(high_score, save_file)
            save_file.close()
        break
    tk.update_idletasks()
    tk.update()
    time.sleep(0.01)
`
4

2 回答 2

0

在类的draw函数中Item,尝试添加round函数:

所以,而不是

if (time_score) % 10 == 0:

利用

if round(time_score) % 10 == 0:

否则,浮点运算可以保证time_score永远不等于10

于 2013-10-03T10:04:14.330 回答
0

我不是 tkinter 的专家,但在我看来,好像你是在将 0.01 添加到一个整数值上。

time_score = time_score + .01

如果 time_score 每秒更新一次,则可以通过以下方式对其进行更改:

time_score = time_score + 1

如果您这样做,我认为您还必须修改以下行:

round(time_score) % 10 == 0

round(time_score) % 1000 == 0.

为了更灵活,我建议使用一个名为 sleeptime 的变量:

sleeptime=0.01
round(time_score) % (10*1/sleeptime) == 0
...
time.sleep(sleeptime)
于 2013-10-02T23:41:56.647 回答