Python Forum
Need help fixing Time and Score issues
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help fixing Time and Score issues
#4
I perfer classes over function but here one without classes.
Here another example using function with some improvements and better tkinter code.
import tkinter as tk
from random import randint, choice

WIDTH = 1000
HEIGHT = 700
MID_X = WIDTH / 2
MID_Y = HEIGHT / 2
CHOICE = [1, 1, 1, 1] + [2 for n in range(150)] + [3, 4]

# globals
stop_game = 0
time_end = False
block_id = None
bonus_time = 0

# countdown
def time_loop(canvas, time_id, time):
    global time_end, bonus_time
    if bonus_time:
        time += bonus_time
        bonus_time = 0

    time -= 1
    if stop_game:
        loop_stop(canvas)
        return

    if time < 0:
        time_end = True
        loop_stop(canvas)
        return

    canvas.itemconfig(time_id, text=str(time))
    # 1000 milliseconds = 1 seconds
    canvas.after(1000, time_loop, canvas, time_id, time)

def collide(canvas, box):
    x, y, w, h = canvas.bbox(block_id)
    return ( (((x > box[0]) and (x < box[2])) or
             ((box[0] > x) and (box[0] < w))) and
             (((y > box[1]) and (y < box[3])) or
             ((box[1] > y) and (box[1] < h))))

def game_loop(canvas, score_id, score, yblocks):
    global bonus_time
    if time_end:
        canvas.create_text(MID_X, MID_Y, text='GAME OVER', fill='white', font=('Helvetica',30))
        canvas.create_text(MID_X, MID_Y + 30, text='Score: '+ str(score), fill='white')
        # stop game_loop timer
        loop_stop(canvas)
        return

    if stop_game:
        loop_stop(canvas)
        return

    add_block = choice(CHOICE)
    if add_block != 2 or len(yblocks) < 4:
        value = randint(5, 10)
        x = randint(value, WIDTH - value)
        # pick a color from list.
        color = [None, 'yellow', 'yellow', 'green', 'orange'][add_block]
        square_id = canvas.create_rectangle(x - value, -value, x + value, value, outline=color)
        if add_block == 4:
            yblocks[square_id] = (value * 4, 7, 4)
        elif add_block == 3:
            yblocks[square_id] = (value + 1, 4, 3)
        else:
            yblocks[square_id] = (value, randint(1, 2), 1) # value of block , speed, block_choice

    # remove block, collision , move block
    remove_block = []
    for item_id, values in yblocks.items():
        box = canvas.bbox(item_id)
        # move yellow block down
        canvas.move(item_id, 0, values[1])
        # yellow block off screen
        if box[1] > HEIGHT:
            remove_block.append(item_id)
        # collision
        else:
            if(collide(canvas, box)):
                remove_block.append(item_id)
                score += values[0]
                if values[2] == 3:
                    bonus_time += 2
                canvas.itemconfig(score_id, text=str(score))

    # remove yellow blocks
    for key in remove_block:
        canvas.delete(key)
        del yblocks[key]

    canvas.after(int(1000/30), game_loop, canvas, score_id, score, yblocks)

# stopping all loops
def loop_stop(canvas):
    global stop_game
    stop_game += 1

    if stop_game == 3:
        new_game(canvas)

def new_game(canvas):
    global block_id, time_end, stop_game
    stop_game = 0
    time_end = False
    # remove all items from canvas
    canvas.delete('all')

    canvas.create_text(50, 30, text='TIME', fill='white' )
    canvas.create_text(150, 30, text='SCORE', fill='white' )
    time_id = canvas.create_text(50, 50, fill='white' )
    score_id = canvas.create_text(150, 50, fill='white', text='0')
    block_id = canvas.create_rectangle(0, 0, 40, 40, fill='red')
    canvas.move(block_id, MID_X, MID_Y)

    # start timers
    game_loop(canvas, score_id, 0, {})
    time_loop(canvas, time_id, 31)

def main():
    window = tk.Tk()
    canvas = tk.Canvas(window, width=WIDTH, height=HEIGHT, bg='black')
    canvas.pack()
    new_game(canvas)

    def keys_press(event):
        speed = 15
        if event.keysym == 'Up':
            canvas.move(block_id, 0, -speed)
        elif event.keysym == 'Down':
            canvas.move(block_id, 0, speed)
        elif event.keysym == 'Left':
            canvas.move(block_id, -speed, 0)
        elif event.keysym == 'Right':
            canvas.move(block_id, speed, 0)
        elif event.keysym == 'Escape':
            window.destroy()
        elif event.keysym == 'n':
            loop_stop(canvas)

    window.bind_all('<Key>', keys_press)
    window.mainloop()

if __name__ == '__main__':
    main()
99 percent of computer problems exists between chair and keyboard.
Reply


Messages In This Thread
RE: Need help fixing Time and Score issues - by Windspar - Nov-18-2017, 12:35 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Time limit and Score not working Coding help Kingrocket10 1 3,038 Nov-09-2017, 03:20 PM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020