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
#3
Here my example of your code. Still don't like how keypress works.
import tkinter as tk
from random import randint

WIDTH = 1000
HEIGHT = 700
MID_X = WIDTH / 2
MID_Y = HEIGHT / 2

class YellowBlock:
    MAX_SPEED = 2

    def __init__(self, canvas):
        self.value = randint(5, 10)
        x = randint(self.value + 1, WIDTH - self.value)
        self.id = canvas.create_rectangle(x - self.value, -self.value, x + self.value, self.value, outline='yellow')
        self.speed = randint(1, YellowBlock.MAX_SPEED)

    def move(self, canvas):
        canvas.move(self.id, 0, self.speed)

class Game:
    def __init__(self):
        self.score = 0
        self.bonus = 0
        self.time = 31
        self.yblocks = {}

        # tk stuff
        self.window = tk.Tk()
        self.canvas = tk.Canvas(self.window, width=WIDTH, height=HEIGHT, bg='black')
        self.canvas.pack()

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

        self.window.bind_all('<Key>', self.keys_press)
        # game over
        self.game_over_id = None
        self.game_score_id = None

        # start time loops
        self.loop_id = None
        self.time_loop_id = None
        self.loop()
        self.time_loop()

    def new_game(self):
        self.score = 0
        self.bonus = 0
        self.time = 31

        self.canvas.itemconfig(self.score_id, text='0')
        # recenter player
        x, y , w, h = self.canvas.coords(self.block_id)
        self.canvas.move(self.block_id, MID_X - (x + w) / 2, MID_Y - (y + h) / 2)

        # remove left over blocks
        for key in self.yblocks.keys():
            self.canvas.delete(key)
        self.yblocks = {}

        # reset game over
        if self.game_over_id:
            self.canvas.delete(self.game_over_id)

        if self.game_score_id:
            self.canvas.delete(self.game_score_id)

        self.game_over_id = None
        self.game_score_id = None

        # stop timers
        if self.time_loop_id:
            self.window.after_cancel(self.time_loop_id)
        if self.loop_id:
            self.window.after_cancel(self.loop_id)

        # start time loops
        self.loop_id = None
        self.time_loop_id = None
        self.loop()
        self.time_loop()

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

    def collide(self, box):
        x, y, w, h = self.canvas.bbox(self.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 loop(self):
        if self.time < 0:
            if self.time_loop_id:
                self.window.after_cancel(self.time_loop_id)
            if self.loop_id:
                self.window.after_cancel(self.loop_id)

            self.game_over_id = self.canvas.create_text(MID_X, MID_Y,
                text='GAME OVER', fill='white', font=('Helvetica',30))
            self.game_score_id = self.canvas.create_text(MID_X, MID_Y + 30,
                text='Score: '+ str(self.score), fill='white')

            # return for it can't assign another cycle
            return

        if randint(1, 50) == 1 or len(self.yblocks) < 2:
            yellow_block = YellowBlock(self.canvas)
            self.yblocks[yellow_block.id] = yellow_block

        blocks_remove = []
        for item_id, block in self.yblocks.items():
            x, y, w, h = self.canvas.bbox(item_id)
            # move block
            block.move(self.canvas)
            # object fell offscreen
            if y > HEIGHT + 1:
                blocks_remove.append(item_id)
            # collision
            else:
                if self.collide((x,y,w,h)):
                    blocks_remove.append(item_id)
                    self.score += block.value
                    self.canvas.itemconfig(self.score_id, text=str(self.score))

        for key in blocks_remove:
            self.canvas.delete(key)
            del self.yblocks[key]

        # roughly 30 frames per second
        self.loop_id = self.window.after(int(1000/30), self.loop)

    def time_loop(self):
        self.time -= 1
        self.canvas.itemconfig(self.time_id, text=str(self.time))
        self.time_loop_id = self.window.after(1000, self.time_loop)

def main():
    game = Game()
    game.window.mainloop()

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-17-2017, 02:03 AM

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