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
#1
Hello I would like some help on my game. My Score and Time limit do not work at all so I would like help fixing that. The digits underneath the Time limit and score spots don't even show up and I can't figure out why. Also tons of blue numbers show up on the shell side, is this normal? It looks like its processing the Ids. This is the block that gives the points when touched: (yblock) & at times (ybloc). Thank you.

My Code:

import os

from tkinter import *
from random import randint
from time import sleep, time
from math import sqrt

HEIGHT = 1000
WIDTH = 900

window = Tk()
window.title('Strange Game')
c = Canvas(window, width=WIDTH, height=HEIGHT, bg='black')
c.pack()

block_id = c.create_rectangle(0, 0, 40, 40, outline='red', fill='red')
BLOCK_R = 15
MID_X = WIDTH / 2
MID_Y = HEIGHT / 2
c.move(block_id, MID_X, MID_Y)

BLOCK_SPD = 15

yblock_id = list()
yblock_r = list()
yblock_speed = list()
MIN_YBLOCK_R = 5
MAX_YBLOCK_R = 10
MAX_YBLOCK_SPD = 2
GAP = 100

YBLOCK_CHANCE = 100
TIME_LIMIT = 30
BONUS_SCORE = 1000

score = 0
bonus = 0
end = time() + TIME_LIMIT

def move_block(event):
    if event.keysym == 'Up':
        c.move(block_id, 0, -BLOCK_SPD)
    elif event.keysym == 'Down':
        c.move(block_id, 0, BLOCK_SPD)
    elif event.keysym == 'Left':
        c.move(block_id, -BLOCK_SPD, 0)
    elif event.keysym == 'Right':
        c.move(block_id, BLOCK_SPD, 0)

def create_ybloc():
    x = randint(0, WIDTH)
    y = randint(0, 0)
    r = randint(MIN_YBLOCK_R, MAX_YBLOCK_R)
    id1 = c.create_rectangle(x - r, y - r, x + r, y + r, outline='yellow')
    yblock_id.append(id1)
    yblock_r.append(r)
    yblock_speed.append(randint(1, MAX_YBLOCK_SPD))

def move_yblocs():
    for i in range(len(yblock_id)):
        c.move(yblock_id[i], 0, yblock_speed[i])


def get_coords(id_num):
    pos = c.coords(id_num)
    x = (pos[0] + pos[2])/2
    y = (pos[1] + pos[3])/2
    return x, y

def del_ybloc(i):
    del yblock_r[i]
    del yblock_speed[i]
    c.delete(yblock_id[i])
    del yblock_id[i]

def clean_up_yblocks():
   for i in range(len(yblock_id)-1, -1, -1):
       x, y = get_coords(yblock_id[i])
       if x < -GAP:
          del_ybloc(i)

def distance(id1, id2):
    x1, y1 = get_coords(id1)
    x2, y2 = get_coords(id2)
    return sqrt((x2 - x1)**2 + (y2 - y1)**2)

def show_score(score):
    c.itemconfig(score_text, text=str(score))

def show_time(time_left):
    c.itemconfig(time_text, text=str(time_left))

def collision():
    points = 0
    for yblock in range(len(yblock_id)-1, -1, -1):
        if distance(block_id, yblock_id[yblock]) < (BLOCK_R + yblock_r[yblock]):
            points += (yblock_r[yblock] + yblock_speed[yblock])
            del_ybloc(yblock)
    return points

c.bind_all('<Key>', move_block)

c.create_text(50, 30, text='TIME', fill='white' )
c.create_text(150, 30, text='SCORE', fill='white' )
time_text = c.create_text(50, 50, fill='white' )
score_text = c.create_text(150, 50, fill='white' )

#MAIN GAME LOOP
while time() < end:
    if randint(1, YBLOCK_CHANCE) == 1:
        create_ybloc()
    move_yblocs()
    clean_up_yblocks
    score += collision()
    if (int(score / BONUS_SCORE)) > bonus:
        bonus += 1
        end += TIME_LIMIT
        show_score(score)
        show_time(int(end - time()))
    print(score)
    window.update()
    sleep(0.01)

c.create_text(MID_X, MID_Y, \
    text='GAME OVER', fill='white', font=('Helvetica',30))
c.create_text(MID_X, MID_Y + 30, \
    text='Score: '+ str(score), fill='white')
c.create_text(MID_X, MID_Y + 45, \
    text='Bonus time: '+ str(bonus*TIME_LIMIT), fill='white')
Reply


Messages In This Thread
Need help fixing Time and Score issues - by Kingrocket10 - Nov-16-2017, 09:11 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Time limit and Score not working Coding help Kingrocket10 1 2,933 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