I am trying to create my own game using python, but the score variable is not changing. I want the score to go up by one every second but it does not seem to be working.
I was hoping someone could help me fix this error. The other thing that does not work is that the square does not move. I would really appriciate it if someone helped me with this code.
from tkinter import * import random from random import randint import time from time import * root = Tk() root.title('Circle Vs Squares') c = Canvas(root, height=500, width=500, bg='lightgreen') score = 0 play = c.create_polygon(-250, 250, 250, 0, -250, -250, fill='red', state=NORMAL) play_text = c.create_text(250, 250, text='Click to play:', fill='black', state=NORMAL ) title = c.create_text(250, 225, text='Circle Vs Squares') c.move(play, 250, 250) c.pack() game_started = False free = True score_text = c.create_text(50, 50, text='Score: '+ str(score), fill='red', state=HIDDEN) def start_game(event): game_started = True c.itemconfig(title, state=HIDDEN) c.itemconfig(play_text, state=HIDDEN) c.itemconfig(play, state=HIDDEN) c.itemconfig(player, state=NORMAL) c.itemconfig(square1, state=NORMAL) c.itemconfig(score_text, state=NORMAL) c.bind('<Button-1>', start_game) #this is where the score comes in: def score(): while game_started == True and free == True: global score time.sleep(1) score = score + 1 c.itemconfig(score_text, text='Score: '+ str(score)) player = c.create_oval(250, 50, 300, 100, fill='blue', state=HIDDEN) c.pack() player_spd = 10 game_started = True square_spd = 5 square1 = c.create_rectangle(250, 400, 300, 450, fill='blue', state=HIDDEN) square_r = 20 square_spd = 5 #the square also does not move, if you could help with that. def move_square(): while free == True: c.move(square1, square_spd, randint(0, 359)) if game_started == True: def move_player(event): if event.keysym == 'Up': c.move(player, 0, -player_spd) elif event.keysym == 'Down': c.move(player, 0, player_spd) elif event.keysym == 'Left': c.move(player, -player_spd, 0) elif event.keysym == 'Right': c.move(player, player_spd, 0) c.bind_all('<Key>', move_player) #MAIN GAME LOOP #Anything wrong here? while free == True and game_started == True: move_player() move_square() score() root.update()
So that’s the code and I cannot figure out why score does not change and the square does not move.