I have a game - catching coins into a bowl. However, one of the criteria is to make it limited by entering time in seconds - how long will the game last. The time user enters is from tkinter.Entry, and I have no idea how to execute a timer and a function at the same time. Code progress:
import tkinter, random, time
from threading import Thread
window = tkinter.Tk()
window.title("Dukaty")
canvas = tkinter.Canvas(window, height = 400, width = 800)
canvas.pack()
timeLabel = tkinter.Label(text = "Time:", font = "Helvetica 15")
timeLabel.pack()
timeEntry = tkinter.Entry(bd = 1)
timeEntry.pack()
mistakesLabel = tkinter.Label(text = "Allowed mistakes:", font = "Helvetica 15")
mistakesLabel.pack()
mistakesEntry = tkinter.Entry(bd = 1)
mistakesEntry.pack()
timeEntry.insert(0, "0")
mistakesEntry.insert(0, "0")
speed = 250
score = 0
mistakes = 0
allowedTime = ""
def countdown(t):
while t:
mins, secs = divmod(t, 60)
timer = '{:02d}:{:02d}'.format(mins, secs)
allowedTime = timer
print(allowedTime)
time.sleep(1)
t -= 1
print("Time's up!")
def timedGame():
time = int(timeEntry.get())
canvas.after(time * 1000)
end()
def movingCoin():
global speed, score, mistakes, allowedTime
time = int(timeEntry.get())
coin = canvas.create_image(random.randint(15, 585), 18, image=coinImg, tags="coin")
canvas.create_text(700, 50, text = str(score), font = "Helvetica 20", fill = "green", tags = "score")
canvas.create_text(700, 80, text = str(mistakes), font = "Helvetica 20", fill = "red", tags = "mistakes")
# ENDLESS
if timeEntry.get() == "0" and mistakesEntry.get() == "0":
while True:
if canvas.coords(coin)[1] < 360:
canvas.move(coin, 0, 5)
canvas.after(speed)
canvas.update()
elif canvas.coords(coin)[1] >= 360 and (canvas.coords(coin)[0] > (canvas.coords(bowl)[0] - 40) and canvas.coords(coin)[0] < (canvas.coords(bowl)[0] + 40)):
canvas.delete("score")
score += 1
canvas.create_text(700, 50, text=str(score), font="Helvetica 20", fill="green", tags="score")
canvas.delete(coin)
coin = canvas.create_image(random.randint(15, 585), 18, image=coinImg, tags="coin")
speed -= 10
else:
canvas.delete("mistakes")
mistakes += 1
canvas.create_text(700, 80, text=str(mistakes), font="Helvetica 20", fill="red", tags="mistakes")
canvas.delete(coin)
coin = canvas.create_image(random.randint(15, 585), 18, image=coinImg, tags="coin")
speed -= 10
# TIME - not done yet
elif timeEntry.get() != "0" and mistakesEntry.get() == "0":
while str(allowedTime) != "00:00":
if canvas.coords(coin)[1] < 360:
canvas.move(coin, 0, 5)
canvas.after(speed)
canvas.update()
elif canvas.coords(coin)[1] >= 360 and (canvas.coords(coin)[0] > (canvas.coords(bowl)[0] - 40) and canvas.coords(coin)[0] < (canvas.coords(bowl)[0] + 40)):
score += 1
print(score)
canvas.delete(coin)
coin = canvas.create_image(random.randint(15, 585), 18, image=coinImg, tags="coin")
speed -= 10
else:
mistakes += 1
print(mistakes)
canvas.delete(coin)
coin = canvas.create_image(random.randint(15, 585), 18, image=coinImg, tags="coin")
speed -= 10
# MISTAKES
elif timeEntry.get() == "0" and mistakesEntry != "0":
allowedMistakes = int(mistakesEntry.get())
print("Allowed mistakes: " + str(allowedMistakes))
while True:
if canvas.coords(coin)[1] < 360:
canvas.move(coin, 0, 5)
canvas.after(speed)
canvas.update()
elif canvas.coords(coin)[1] >= 360 and (canvas.coords(coin)[0] > (canvas.coords(bowl)[0] - 40) and canvas.coords(coin)[0] < (canvas.coords(bowl)[0] + 40)):
canvas.delete("score")
score += 1
canvas.create_text(700, 50, text=str(score), font="Helvetica 20", fill="green", tags="score")
canvas.delete(coin)
coin = canvas.create_image(random.randint(15, 585), 18, image=coinImg, tags="coin")
speed -= 10
else:
if allowedMistakes > mistakes:
canvas.delete("mistakes")
mistakes += 1
canvas.create_text(700, 80, text=str(mistakes), font="Helvetica 20", fill="red", tags="mistakes")
canvas.delete(coin)
coin = canvas.create_image(random.randint(15, 585), 18, image=coinImg, tags="coin")
speed -= 10
elif allowedMistakes == mistakes:
print("ending the program")
break
def end():
window.destroy()
def tap():
Thread(target=countdown(int(timeEntry.get()))).start()
Thread(target=movingCoin).start()
endButton = tkinter.Button(text = "End", command = end)
endButton.pack(side = "right")
startButton = tkinter.Button(text = "Start", command = tap)
startButton.pack(side = "left")
window.mainloop()
What this does, is that it starts the timer, and when it comes to end, THEN it executes the movingCoin function.
What I want to achieve is to make the timer start (when in Entry is value greater than 0) and at the same time execute the movingCoin - which then drops to the time section and the while breaks when the time is up.
You can use this method
window.after(1000,<function_name>)
It means the function call after 1 sec. If you are use recursion with this method then you can create timer.
I hope it works for you, cheer.
import time
# time.time() return in second
game_duration = float(input('duration: ')) + time.time()
game_is_running = True
while game_is_running:
if game_duration == time.time(): # this means: if now equal to game_duration
game_is_running = False
# note: it is quite critical where you place time.time()
One more example:
# I used the following technique to build an explosion animation for 30 seconds, then it will do something else.
class Explosion(pygame.sprite.Sprite):
def __init__(self, center):
pygame.sprite.Sprite.__init__(self)
self.image = explosion_animation[0]
self.rect = self.image.get_rect()
self.rect.center = center
self.frame = 0
self.last_update = pygame.time.get_ticks()
self.duration = 30
def update(self):
now = pygame.time.get_ticks()
if now - self.last_update > self.duration:
self.last_update = now
self.frame += 1
if self.frame == len(explosion_animation):
self.kill()
else:
center = self.rect.center
self.image = explosion_animation[self.frame]
self.rect = self.image.get_rect()
self.rect.center = center
Related
I have this game that I created, it is from one of the coding books I have: Coding Games In Python. However, I have modified this game to have an intro screen, where it says: Press SPACE to start. But after users press space (as shown in the draw() function), they have to hold it down for the actual game to run. I have tried to use the return function as shown below. Please help.
import pgzrun
from random import randint
WIDTH = 400
HEIGHT = 400
score = 0
game_over = False
fox = Actor("fox")
fox.pos = 100, 100
coin = Actor("coin")
coin.pos = 200, 200
def draw():
screen.fill("green")
screen.draw.text("Press SPACE to start", color='black',
center=(200, 200), fontsize=45)
if keyboard.SPACE:
begin()
return
def begin():
screen.fill("green")
fox.draw()
coin.draw()
screen.draw.text("Score: " + str(score), color='black',
topleft=(10, 10))
if game_over:
screen.fill('green')
screen.draw.text("Final Score: " + str(score),
center=(200, 200), fontsize=60)
def place_coin():
coin.x = randint(75, (WIDTH - 75))
coin.y = randint(75, (HEIGHT - 75))
def time_up():
global game_over
game_over = True
def update():
global score
if keyboard.left:
fox.x = fox.x - 10
elif keyboard.right:
fox.x = fox.x + 10
elif keyboard.up:
fox.y = fox.y - 10
elif keyboard.down:
fox.y = fox.y + 10
coin_collected = fox.colliderect(coin)
if coin_collected:
score = score + 10
place_coin()
if fox.x <= 64:
fox.x = 64
if fox.x >= 336:
fox.x = 336
if fox.y <= 64:
fox.y = 64
if fox.y >= 336:
fox.y = 336
clock.schedule(time_up, 60.0)
place_coin()
pgzrun.go()
So, I have been working on this game called Space Invader. Everything is doing great except for one thing which is the sound. As in the below code, I've imported winsound module and it is not doing well. When I fire the bullet it makes the fire noise which I wanted but the problem is that I've also added background music, and whenever I hit fire it stops the background music and starts making fire noises until it hits the target. I want background music to run continuously. I've also seen many Youtube videos but couldn't get any help. Can anyone tell me the mistake which I've made in the below code?
import turtle
import random
import math
import winsound
score = 0
highscore = 0
targets = []
live_remaining = 3
screen = turtle.Screen()
screen.title("Space War")
screen.bgcolor("Black")
screen.setup(width=800, height=770)
screen.bgpic("Space.gif")
screen.addshape("Spaceship.gif")
screen.addshape("Missile.gif")
screen.addshape("ufo.gif")
screen.addshape("gameover.gif")
screen.addshape("youwin.gif")
screen.tracer(0)
winsound.PlaySound("bgmusic.wav", winsound.SND_ASYNC)
spaceship = turtle.Turtle()
spaceship.speed(0)
spaceship.shape("Spaceship.gif")
spaceship.penup()
spaceship.ht()
spaceship.goto(0, -320)
spaceship.st()
spaceship.direction = "stop"
spaceshipspeed = 15
num_of_targets = 50
for i in range(num_of_targets):
targets.append(turtle.Turtle())
target_start_x = -250
target_start_y = 275
target_number = 0
for target in targets:
target.shape("ufo.gif")
target.penup()
target.speed(0)
x=target_start_x + (50 * target_number)
y=target_start_y
target.goto(x, y)
target_number += 1
if target_number == 10:
target_start_y -= 20
target_number = 0
targetspeed = 0.5
fires=turtle.Turtle()
fires.shape("Missile.gif")
fires.penup()
fires.ht()
fires.speed(0)
fires.goto(0, -340)
fires.direction = "stop"
firesspeed=3
fire = turtle.Turtle()
fire.shape("Missile.gif")
fire.penup()
fire.ht()
fire.speed(0)
fire.goto(0, -340)
fire.direction = "stop"
firespeed = 4
firestate = "ready"
sb=turtle.Turtle()
sb.shape("square")
sb.color("white")
sb.ht()
sb.penup()
sb.goto(0,320)
sb.write("Score: 0 | High Score: 0", align="center", font=("courier", 30, "bold"))
life = turtle.Turtle()
life.shape("square")
life.ht()
life.penup()
life.goto(-350,-350)
life.color("white")
life.write("Life remaining: 3", font=("courier",10,"bold"))
def go_left():
x = spaceship.xcor()
x -= spaceshipspeed
if x < -340:
x = -340
spaceship.setx(x)
def go_right():
x = spaceship.xcor()
x += spaceshipspeed
if x > 340:
x = 340
spaceship.setx(x)
def go_forward():
global firestate
if firestate == "ready":
firestate = "fire"
x = spaceship.xcor()
y = spaceship.ycor()
fire.setposition(x, y)
fire.st()
winsound.PlaySound("fire.wav", winsound.SND_ASYNC)
def move():
pass
screen.listen()
screen.onkeypress(go_left, "a")
screen.onkeypress(go_right, "d")
screen.onkeypress(go_forward, "space")
while True:
screen.update()
for target in targets:
x = target.xcor()
x += targetspeed
target.setx(x)
if target.xcor() > 340:
targetspeed *= -1
for t in targets:
y = t.ycor()
y -= 20
t.sety(y)
if target.xcor() < -340:
targetspeed *= -1
for t in targets:
y = t.ycor()
y -= 20
t.sety(y)
if fire.distance(target) < 20:
fire.direction = "stop"
fire.ht()
fire.clear()
firestate = "ready"
fire.goto(10000000, 1000000)
target.goto(0, 10000000)
score += 10
winsound.PlaySound("explosion.wav", winsound.SND_ASYNC)
if score > highscore:
highscore = score
sb.clear()
sb.write("Score: {} | High Score: {}".format(score,highscore), align="center",
font=("courier", 30, "bold"))
if score == 500:
screen.clear()
screen.bgcolor("black")
gf = turtle.Turtle()
gf.shape("youwin.gif")
gf.goto(0, 0)
winsound.PlaySound("gamewin.wav",winsound.SND_ASYNC)
if spaceship.distance(target) < 60:
spaceship.goto(0, -320)
target.goto(-320, 300)
live_remaining -= 1
winsound.PlaySound("explosion.wav",winsound.SND_ASYNC)
life.clear()
life.write("Life remaining: {}".format(live_remaining), font=("courier",10,"bold"))
if live_remaining == 0:
screen.clear()
screen.bgcolor("black")
gover=turtle.Turtle()
gover.shape("gameover.gif")
gover.goto(0, 0)
winsound.PlaySound("gamelose.wav", winsound.SND_ASYNC)
replay = input("Press r to retry",)
y = fire.ycor()
y += firespeed
fire.sety(y)
if fire.ycor() > 300:
fire.ht()
firestate = "ready"
move()
screen.mainloop()
I'm working on a basic shooter game in Pygame (I'm following a tutorial.) I decided to add background music to keep things nice.
Code (quite long!):
import pygame
from pygame import image as sprite
pygame.init()
dow = pygame.display.set_mode((1000, 700))
pygame.display.set_caption("PY.Touhou")
clock = pygame.time.Clock()
rightSprites = [sprite.load('ness-right1.png'),
pygame.image.load('ness-right2.png'),
pygame.image.load('ness-right3.png'),
pygame.image.load('ness-right4.png'),
pygame.image.load('ness-right5.png'),
pygame.image.load('ness-right6.png'),
pygame.image.load('ness-right7.png'),
pygame.image.load('ness-right8.png')
]
leftSprites = [
sprite.load('ness-left1.png'),
sprite.load('ness-left2.png'),
sprite.load('ness-left3.png'),
sprite.load('ness-left4.png'),
sprite.load('ness-left5.png'),
sprite.load('ness-left6.png'),
sprite.load('ness-left7.png'),
sprite.load('ness-left8.png')
]
scene = pygame.image.load('bg.png')
idle = pygame.image.load('ness-idle.png')
class Player(object):
def __init__(self, x_pos, y_pos, w, h):
self.x_pos = x_pos
self.y_pos = y_pos
self.w = w
self.h = h
self.velocity = 15
self.JUMPbool = False
self.atleft = False
self.atright = False
self.steps = 0
self.jumpheight = 12
self.hitbox = (self.x_pos + 50, self.y_pos, 73, 227)
self.still = True
def move(self):
if self.steps + 1 >= 24:
self.steps = 0
if not self.still:
if self.atleft:
dow.blit(leftSprites[self.steps // 4], (self.x_pos, self.y_pos))
self.steps += 1
elif self.atright:
dow.blit(rightSprites[self.steps // 4], (self.x_pos, self.y_pos))
self.steps += 1
else:
if self.atright:
dow.blit(rightSprites[1], (self.x_pos, self.y_pos))
else:
dow.blit(leftSprites[1], (self.x_pos, self.y_pos))
self.hitbox = (self.x_pos + 50, self.y_pos, 73, 227)
# pygame.draw.rect(dow, (0, 0, 255), self.hitbox, 2)
class IceBullet(object):
def __init__(self, x_pos, y_pos, radius, color, direction):
self.x_pos = x_pos
self.y_pos = y_pos
self.radius = radius
self.color = color
self.direction = direction
self.velocity = 8 * direction
def summon(self):
pygame.draw.circle(dow, self.color, (self.x_pos, self.y_pos), self.radius)
class EnemyCirno(object):
erightSprites = [sprite.load('e-right%s.png' % pic) for pic in range(1, 9)]
eleftSprites = [sprite.load('e-left%s.png' % pic) for pic in range(1, 9)]
def __init__(self, x_pos, y_pos, w, h, ending):
self.x_pos = x_pos
self.y_pos = y_pos
self.w = w
self.h = h
self.ending = ending
self.path = [self.x_pos, self.ending]
self.hitbox = (self.x_pos + 50, self.y_pos, 73, 227)
self.steps = 0
self.health = 20
self.isAlive = True
self.velocity = 4
def summon(self):
self.move()
if self.isAlive:
if self.steps + 1 >= 24:
self.steps = 0
if self.velocity > 0:
dow.blit(self.erightSprites[self.steps // 4], (self.x_pos, self.y_pos))
self.steps += 1
else:
dow.blit(self.eleftSprites[self.steps // 4], (self.x_pos, self.y_pos))
self.steps += 1
pygame.draw.rect(dow, (255, 10, 0), (100, 110, 400, 20))
pygame.draw.rect(dow, (5, 255, 10), (100, 110, 200 - (20 * (10 - self.health)), 20))
enemy_health = italicStyle.render('Enemy Health', 1, (255, 0, 0))
dow.blit(enemy_health, (100, 150))
self.hitbox = (self.x_pos + 50, self.y_pos, 73, 227)
# pygame.draw.rect(dow, (0, 0, 0), self.hitbox, 2)
def move(self):
if self.velocity > 0:
if self.x_pos + self.velocity < self.path[1]:
self.x_pos += self.velocity
else:
self.velocity = self.velocity * -1
self.steps = 0
else:
if self.x_pos - self.velocity > self.path[0]:
self.x_pos += self.velocity
else:
self.velocity = self.velocity * -1
self.steps = 0
def hit(self):
if self.health > 1:
self.health -= 0.50
hit = pygame.mixer.Sound('hit.wav')
hit.play()
else:
self.isAlive = False
dead = pygame.mixer.Sound('death_sound.wav')
dead.play()
# Main starting loop.
letterStyle = pygame.font.SysFont('Bookman Old Style', 50)
italicStyle = pygame.font.SysFont('Bookman Old Style', 30, False, True)
cirno = Player(300, 470, 160, 233)
score = 0
evilCirno = EnemyCirno(50, 470, 160, 233, 800)
maxShots = 0
bullets = []
running = True
while running is True:
clock.tick(24)
pygame.time.delay(10)
music = pygame.mixer.music.load('main_theme.wav') # And here is where the bug begins!
pygame.mixer.music.play()
for events in pygame.event.get():
if events.type == pygame.QUIT:
running = False
key = pygame.key.get_pressed()
def update_screen():
dow.blit(scene, (0, 0))
textdisplay = letterStyle.render('Score: ' + str(score), 1, (0, 255, 255))
won = italicStyle.render("Enemy defeated!", 1, (10, 255, 19))
dow.blit(textdisplay, (50, 30))
if not evilCirno.isAlive:
dow.blit(won, (100, 150))
cirno.move()
evilCirno.summon()
for shot in bullets:
shot.summon()
pygame.display.update()
if maxShots > 0:
maxShots += 1
if maxShots > 5:
maxShots = 0
for shot in bullets:
if evilCirno.isAlive:
if shot.y_pos - shot.radius < evilCirno.hitbox[1] + evilCirno.hitbox[3] and shot.y_pos + shot.radius > \
evilCirno.hitbox[1]:
if shot.x_pos + shot.radius > evilCirno.hitbox[0] and shot.x_pos - shot.radius < evilCirno.hitbox[0] + \
evilCirno.hitbox[2]:
evilCirno.hit()
score += 50
bullets.pop(bullets.index(shot))
if 1000 > shot.x_pos > 0:
shot.x_pos += shot.velocity
else:
bullets.pop(bullets.index(shot))
if key[pygame.K_z] and maxShots == 0:
shoot = pygame.mixer.Sound('bullet.wav')
shoot.play()
if cirno.atleft:
facing = -1
else:
facing = 1
if len(bullets) < 5:
bullets.append(
IceBullet(round(cirno.x_pos + cirno.w // 2), round(cirno.y_pos + cirno.h // 1.2), 20, (0, 0, 255),
facing))
maxShots = 1
if key[pygame.K_LEFT] and cirno.x_pos > cirno.velocity: # Will stop ASAP when reached
cirno.x_pos -= cirno.velocity
cirno.atleft = True
cirno.atright = False
cirno.still = False
elif key[
pygame.K_RIGHT] and cirno.x_pos < 1000 - cirno.velocity - cirno.w: # If it goes past this, it will stop ASAP, as well
cirno.x_pos += cirno.velocity
cirno.atright = True
cirno.atleft = False
cirno.still = False
else:
cirno.steps = 0
cirno.still = True
if not cirno.JUMPbool:
if key[pygame.K_UP]:
cirno.JUMPbool = True
cirno.steps = 0
else:
if cirno.jumpheight >= -12: # What to do if pygame.K_SPACE is pressed down.
cirno.y_pos -= (cirno.jumpheight * abs(cirno.jumpheight)) * 0.5
cirno.jumpheight -= 1
else:
cirno.jumpheight = 12
cirno.JUMPbool = False
update_screen()
pygame.quit()
So far, the music actually does play, as well as the sounds. Though the problem is that it just loops the beginning part of the music over and over (which is only the first few seconds), so what you get is just something that sounds like a broken MP3 player.
(for reference the music is 1:45. Even weirder is that I've seen people use it with no issues.)
Is there any way to fix this bug? Thanks for your help :)
Do you see the bug here?
while running is True:
[...]
music = pygame.mixer.music.load('main_theme.wav')
pygame.mixer.music.play()
It's re-loading and re-playing replaying the music inside your code main loop. That is every single updated frame of game-play,
Load and play music outside your main loop:
music = pygame.mixer.music.load('main_theme.wav')
pygame.mixer.music.play()
while running is True:
[...]
Your code can then call the mixer.get_busy() function too see if the music has stopped, and do something about it.
You probably also want to investigate PyGame Sound mixer channels, so your sound effects mix in with the background music properly.
I'm trying to write a collision detection, but I'm getting some weird sticking behaviour, when I run into the object, it goes through, then when I go back, it sticks. what is going on here? the
module setup
import tkinter as tk
import random
setting up the variables
groundheight = 700
playersize = 50
PVx = 5
PVy = 0
playermass = 25
groundmass = 1000
playerx = 0
playery = -5
isgrounded = False
enemysize = 25
the player behavior
def forever():
global PVx
global PVy
global playerx
global playery
global isgrounded
enemyarray = canv.find_withtag("enemy")
playerx = canv.coords("player")[2]-playersize/2
playery = canv.coords("player")[3]-playersize/2
canv.move("player", PVx, PVy)
distoground = math.sqrt(math.pow(playery - groundheight, 2))
if distoground <= playersize:
PVy = 0
else:
isgrounded = False
PVy += 0.1
root.after(16, forever)
the enemy behavior
def enemyupdate():
global PVx
global PVy
enemyarray = canv.find_withtag("enemy")
for i in enemyarray:
enemymass = 25
Evx = 0
EVy = 0
x = canv.coords("enemy")[2]-playersize
y = canv.coords("enemy")[3]-playersize
distoground = math.sqrt(math.pow(y - groundheight, 2))
distoplayer = math.sqrt(((x-playerx)**2)+((y-playery)**2))
if distoground <= playersize:
EVy = 0
else:
EVy += 5
here's where the collisions happen
if distoplayer <= playersize+enemysize:
tempx = Evx
Evx = (playermass*PVx + enemymass*Evx)/(playermass+enemymass)
PVx = (playermass*PVx + enemymass*Evx)/(playermass+enemymass)
canv.move(i, Evx, EVy)
root.after(16, enemyupdate)
receiving input
def getaxis(X):
global PVx
PVx = X
jump code for later
def jump(event):
global PVy
if isgrounded:
PVy -= 5
tkinter main loop
root = tk.Tk()
root.geometry("1920x1080")
root.state("zoomed")
canv = tk.Canvas(width=1920, height=1080, bg='light blue')
canv.create_rectangle(0, groundheight, 1920, 1080, fill="White")
player = canv.create_oval(0, 700, playersize, 700-playersize, fill="Red", tag="player")
canv.pack()
root.after(1, forever)
root.after(1, enemyupdate)
root.bind("<space>", jump)
root.bind("<Left>", lambda *args: getaxis(-5))
root.bind("<Right>", lambda *args: getaxis(5))
for i in range(1):
enemx = 500
player = canv.create_oval(enemx+enemysize, 700, enemx - enemysize, 700-enemysize*2, fill="Red", tag="enemy")
root.mainloop()
I want the score to show on the canvas but so far to no avail. I thought I could just use:
self.canvas.create_text(200, 60, fill="darkblue", font="Times 15 italic bold", text="Your score is:",self.score)
Didn't work unfortunately, so how do I get the score to show on the canvas? Also wondering how to get a message box to pop up saying "You won!" when the score = 10. And lastly, how to get a message box to pop up saying "You lost if the score < 10 AND there is no even numbers on the canvas.
Code so far using python 3.3:
def click(self, event):
if self.canvas.find_withtag(CURRENT):
item_uid = event.widget.find_closest(event.x, event.y)[0]
is_even = False
try: # clicked oval
self.bubbles[item_uid]
except KeyError: # clicked the text
for key, value in self.bubbles.iteritems():
if item_uid == value[5]: # comparing to text_id
if value[4] % 2 == 0:
is_even = True
self.canvas.delete(key) # deleting oval
self.canvas.delete(item_uid) # deleting text
else:
if self.bubbles[item_uid][4] % 2 == 0:
is_even = True
self.canvas.delete(item_uid) # deleting oval
self.canvas.delete(self.bubbles[item_uid][5]) # deleting text
if is_even:
self.score += 1
else:
self.score -= 1
if self.score == 10:
print ("You won")
print (self.score)
That is a fun game! The script I gave below shows you how to do everything you want (display the current score and show messages). The lines with ## above them are what I added:
from tkinter import *
import random
##
from tkinter.messagebox import showinfo
class BubbleFrame:
def __init__(self, root):
root.title("Math Bubbles")
self.bubbles = {}
self.score = 0
Button(root, text="Start", width=8, command=self.initialize_bubbles).pack() # This button starts the game, making the bubbles move across the screen
Button(root, text="Quit", width=8, command=quit).pack()
self.canvas = Canvas(root, width=800, height=650, bg='#afeeee')
self.canvas.create_text(400, 30, fill="darkblue", font="Times 20 italic bold", text="Click the bubbles that are multiples of two.")
##
self.current_score = self.canvas.create_text(200, 60, fill="darkblue", font="Times 15 italic bold", text="Your score is:")
self.canvas.pack()
def initialize_bubbles(self):
for each_no in range(1, 21):
xval = random.randint(5, 765)
yval = random.randint(5, 615)
oval_id = self.canvas.create_oval(xval, yval, xval + 30, yval + 30,fill="#00ffff", outline="#00bfff", width=5, tags="bubble")
text_id = self.canvas.create_text(xval + 15, yval + 15, text=each_no, tags="bubble")
self.canvas.tag_bind("bubble", "<Button-1>", lambda x: self.click(x))
self.bubbles[oval_id] = (xval, yval, 0, 0, each_no, text_id)
def click(self, event):
if self.canvas.find_withtag(CURRENT):
item_uid = event.widget.find_closest(event.x, event.y)[0]
is_even = False
try:
self.bubbles[item_uid]
except KeyError:
for key, value in self.bubbles.iteritems():
if item_uid == value[5]:
if value[4] % 2 == 0:
is_even = True
self.canvas.delete(key)
self.canvas.delete(item_uid)
else:
if self.bubbles[item_uid][4] % 2 == 0:
is_even = True
self.canvas.delete(item_uid)
self.canvas.delete(self.bubbles[item_uid][5])
if is_even:
self.score += 1
else:
self.score -= 1
if self.score == 10:
##
showinfo("Winner", "You won!")
##
self.canvas.delete(self.current_score)
##
self.current_score = self.canvas.create_text(200, 60, fill="darkblue", font="Times 15 italic bold", text="Your score is: %s"%self.score)
def loop(self, root):
for oval_id, (x, y, dx, dy, each_no, text_id) in self.bubbles.items():
dx += random.randint(-1, 1)
dy += random.randint(-1, 1)
dx, dy = max(-5, min(dx, 5)), max(-5, min(dy, 5))
if not 0 < x < 770:
dx = -dx
if not 0 < y < 620:
dy = -dy
self.canvas.move(oval_id, dx, dy)
self.canvas.move(text_id, dx, dy)
self.bubbles[oval_id] = (x + dx, y + dy, dx, dy, each_no, text_id)
root.after(100, self.loop, root)
if __name__ == "__main__":
root = Tk()
frame = BubbleFrame(root)
frame.loop(root)
root.mainloop()
from http://www.tutorialspoint.com/python/tk_messagebox.htm
import tkMessageBox
tkMessageBox.showinfo("Winner", "You won!")
you can use place instead of pack to put elements above the canvas in the same Frame or Tk.
Label(root, text="Quit").place()
place()
relx, rely : Horizontal and vertical offset as a float between 0.0 and 1.0, as a fraction of the height and width of the parent widget.
x, y : Horizontal and vertical offset in pixels.
from http://www.tutorialspoint.com/python/tk_place.htm