0

だから私は基本的なボールバウンスゲームを作っています。10秒ごとにアイテムが画面から落ちるようにしようとしています。これはtime_score、1 秒に 1 回上昇することによって行います。34 行目で、10 秒ごとに 1 回アクティブにする必要があります。しかし、代わりにそれを行うのは最初だけです。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関数で、関数を追加してみてください。Itemround

だから、代わりに

if (time_score) % 10 == 0:

使用する

if round(time_score) % 10 == 0:

time_scoreそうしないと、浮動小数点演算によって、が と等しくならないことが保証される場合があり10ます。

于 2013-10-03T10:04:14.330 に答える