Arc shape coming out strangely - python

I am trying to make a catch the egg game and it is going fine apart from the basket, which is supposed to look like an arc upside-down but it became a very strange shape, and I have searched as far as I could but no one has had such a problem like this.
I want it to look like this:
I'll have to post the whole program otherwise you wouldn't understand:
from itertools import cycle
from random import randrange
from tkinter import Canvas, Tk, messagebox, font
canvas_width = 800
canvas_height = 400
root = Tk()
c = Canvas(
root,
width=canvas_width,
height=canvas_height,
background="deep sky blue"
)
c.create_rectangle(
-5,
canvas_height - 100,
canvas_width + 5,
canvas_height + 5,
fill="sea green",
width=0
)
c.create_oval(-80, -80, 120, 120, fill="orange", width=0)
c.pack()
color_cycle = cycle([
"light blue",
"light green",
"light pink",
"light yellow",
"light cyan"
])
egg_width = 45
egg_height = 55
egg_score = 10
egg_speed = 500
egg_interval = 4000
difficulty_factor = 0.95
catcher_color = "blue"
catcher_width = 100
catcher_height = 100
catcher_start_x = canvas_width / 2 - catcher_width / 2
catcher_start_y = canvas_height - catcher_width - 20
catcher_start_x2 = catcher_start_x = catcher_width
catcher_start_y2 = catcher_start_y + catcher_height
catcher = c.create_arc(
catcher_start_x,
catcher_start_y,
catcher_start_x2,
catcher_start_y2,
start=200,
extent=140,
style="arc",
outline=catcher_color,
width=30
)
game_font = font.nametofont("TkFixedFont")
game_font.config(size=18)
score = 0
score_text = c.create_text(
10,
10,
anchor="nw" ,
font=game_font,
fill="darkblue",
text="Score: " + str(score)
)
lives_remaining = 3
lives_text = c.create_text(
canvas_width - 10,
10,
anchor="ne",
font=game_font,
fill="darkblue",
text="Lives " + str(lives_remaining)
)
eggs = []
def create_egg():
x = randrange(10, 740)
y = 40
new_egg = c.create_oval(
x,
y,
x + egg_width,
y + egg_height,
fill=next(color_cycle),
width=0
)
eggs.append(new_egg)
root.after(egg_interval, create_egg)
def move_eggs():
for egg in eggs:
(egg_x, egg_y, egg_x2, egg_y2) = c.coords(egg)
c.move(egg, 0, 10)
if egg_y2 > canvas_height:
egg_dropped(egg)
root.after(egg_speed, move_eggs)
def egg_dropped(egg):
eggs.remove(egg)
lose_a_life()
if lives_remaining == 0:
messagebox.showinfo("Game Over!", "Final Score: " + str(score))
root.destroy()
def lose_a_life():
global lives_remaining
lives_remaining -= 1
c.itemconfigure(lives_text, text = "Lives: " + str(lives_remaining))
def check_catch():
catcher_x, catcher_y, catcher_x2, catcher_y2 = c.coords(catcher)
for egg in eggs:
egg_x, egg_y, egg_x2, egg_y2 = c.coords(egg)
if egg_x<catcher_x<egg_x2 and (catcher_y2 - egg_y2) < 5:
eggs.remove(egg)
c.delete(egg)
increase_score(egg_score)
root.after(100, check_catch)
def increase_score(points):
global score, egg_speed, egg_interval
score += points
egg_speed = int(egg_speed * difficulty_factor)
egg_interval = int(egg_interval * difficulty_factor)
c.itemconfigure(score_text, text="Score: " + str(score))
def move_left(event):
(x1, y1, x2, y2) = c.coords(catcher)
if x1 > 0:
c.move(catcher, -20, 0)
def move_right(event):
(x1, y1, x2, y2) = c.coords(catcher)
if x2 < canvas_width:
c.move(catcher, 20, 0)
c.bind("<Left>" , move_left)
c.bind("<Right>", move_right)
c.focus_set()
root.after(1000, create_egg)
root.after(1000, move_eggs)
root.after(1000, check_catch)
root.mainloop()

You have a typo on line 27 - you have written catcher_start_x2 = catcher_start_x = catcher_width where you should have catcher_start_x2 = catcher_start_x - catcher_width.
Also, the backslash on line 29 is unnecessary as things contained in brackets automatically continue on multiple lines.
One more thing - once the typo is fixed, the eggs are only caught in the left area of the catcher because line 79 (if egg_x<catcher_x<egg_x2 and (catcher_y2 - egg_y2) < 5:) is checking if the left x coord of the catcher is inside the x coords of the egg not the other way around. To fix this, replace it with
if catcher_x - 10 < egg_x and egg_x2 < catcher_x2 + 10 and (catcher_y2 - egg_y2) < 5: (the -10 and +10 are to give the player a bit of leeway so the egg doesn't have to be exactly in the centre).

Related

PHYTON: _tkinter.TclError: invalid command name ".!canvas"

My 9 year-old son is studying Python and he faced below problem in his code (bubble breaker game):
Traceback (most recent call last):
File "M:\Yandex disk\YandexDisk\ДЕТКИ\python modules\Bubble Blaster.py", line 55, in <module>
move_bubbles()
File "M:\Yandex disk\YandexDisk\ДЕТКИ\python modules\Bubble Blaster.py", line 48, in
move_bubbles
c.move(bub_id[i], -bub_speed[i], 0)
File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line
2949, in move
self.tk.call((self._w, 'move') + args)
_tkinter.TclError: invalid command name ".!canvas"*
I've rechecked the whole code twice but can't find any mistake.
Here is the whole code:
from tkinter import *
HEIGHT = 1080
WIDTH = 1920
window = Tk()
window.title('Bubble Blaster')
c = Canvas(window, width=WIDTH, height=HEIGHT, bg='darkblue')
c.pack()
ship_id = c.create_polygon(5, 5, 5, 25, 30, 15, fill='red')
ship_id2 = c.create_oval(0, 0, 30, 30, outline='red')
SHIP_R = 15
MID_X = WIDTH / 2
MID_Y = HEIGHT / 2
c.move(ship_id, MID_X, MID_Y)
c.move(ship_id2, MID_X, MID_Y)
SHIP_SPD = 10
def move_ship(event):
if event.keysym == 'Up':
c.move(ship_id, 0, -SHIP_SPD)
c.move(ship_id2, 0, -SHIP_SPD)
elif event.keysym == 'Down':
c.move(ship_id, 0, SHIP_SPD)
c.move(ship_id2, 0, SHIP_SPD)
elif event.keysym == 'Left':
c.move(ship_id, -SHIP_SPD, 0)
c.move(ship_id2, -SHIP_SPD, 0)
elif event.keysym == 'Right':
c.move(ship_id, SHIP_SPD, 0)
c.move(ship_id2, SHIP_SPD, 0)
c.bind_all('<Key>', move_ship)
from random import randint
bub_id = list()
bub_r = list()
bub_speed = list()
MIN_BUB_R = 10
MAX_BUB_R = 30
MAX_BUB_SPD = 10
GAP = 100
def create_bubble():
x = WIDTH + GAP
y = randint(0, HEIGHT)
r = randint(MIN_BUB_R, MAX_BUB_R)
id1 = c.create_oval(x - r, y - r, x + r, y + r, outline='white')
bub_id.append(id1)
bub_r.append(r)
bub_speed.append(randint(1, MAX_BUB_SPD))
def move_bubbles():
for i in range(len(bub_id)):
c.move(bub_id[i], -bub_speed[i], 0)
from time import sleep, time
BUB_CHANCE = 10
#MAIN GAME LOOP
while True:
if randint(1, BUB_CHANCE) == 1:
create_bubble()
move_bubbles()
window.update()
sleep(0.01)
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_bubble(I):
del bub_r[I]
del bub_speed[I]
c.delete(bub_id[i])
del bub_id[I]
def clean_up_bubs():
for i in range(len(bub_id)-1, -1, -1):
x, y = get_coords(bub_id[I])
if x < -GAP:
del_bubble(I)
#MAIN GAME LOOP
while True:
if randint(1, BUB_CHANCE) == 1:
create_bubble()
move_bubbles()
clean_up_bubs()
window.update()
sleep(0.01)
from math import sqrt
def distance(id1, id2):
x1, y1 = get_coords(id1)
x2, y2 = get_coords(id2)
return sqrt((x2 - x1)**2 + (y2 - y1)**2)
def collision():
points = 0
for bub in range(len(bub_id)-1, -1, -1):
if distance(ship_id2, bub_id[bub]) < (SHIP_R + bub_r[bub]):
points += (bub_r[bub] + bub_speed[bub])
del_bubble(bub)
return points
score = 0
#MAIN GAME LOOP
while True:
if randint(1, BUB_CHANCE) == 1:
create_bubble()
move_bubbles()
clean_up_bubs()
score += collision()
print(score)
window.update()
sleep(0.01)
What is causing this error?
Major mistakes:
no main loop
from tkinter import *
root = Tk()
root.mainloop()
https://tkdocs.com/tutorial/eventloop.html
all loops inside the main loop are created with the .after method
https://tkdocs.com/shipman/universal.html
when the window is closed, while cycles continue to work, so an error pops up _tkinter.TclError: invalid command name ".!canvas"*
and little things
c.delete(bub_id[i]) The variable "i" is not defined in the function.
x, y = get_coords(bub_id[I])
if x < -GAP:
del_bubble(I)
in this function does not define a large "I".

error : _tkinter.TclError: invalid command name ".!canvas"

I was trying to make a catch the egg game but it didn't go to plan and it brought up an error saying
_tkinter.TclError: invalid command name ".!canvas".
I have searched the whole of the internet about the problem but none of the solutions that people the person helped me at all.
anyways this is what I have written so far it would be appreciated if someone helped especailly because I'm new.
from itertools import cycle
from random import randrange
from tkinter import Canvas, Tk, messagebox, font
canvas_width = 800
canvas_height = 400
root = Tk()
c = Canvas(root, width = canvas_width, height = canvas_height, background="deep sky blue")
c.create_rectangle(-5, canvas_height - 100, canvas_width + 5, canvas_height + 5, fill = "sea green", width=0)
c.create_oval(-80, -80, 120, 120, fill="orange", width=0)
c.pack()
color_cycle = cycle(["light blue", "light green", "light pink", "light yellow", "light cyan"])
egg_width = 45
egg_height = 55
egg_score = 10
egg_speed = 500
egg_interval = 4000
difficulty_factor = 0.95
catcher_color = "magenta"
catcher_width = 100
catcher_height = 100
catcher_start_x = canvas_width / 2 - catcher_width / 2
catcher_start_y = canvas_height - catcher_width - 20
catcher_start_x2 = catcher_start_x = catcher_width
catcher_start_y2 = catcher_start_y + catcher_height
catcher = c.create_arc(catcher_start_x, catcher_start_y, catcher_start_x2, catcher_start_y2, start = 200, extent = 140, \
style = "arc", outline = catcher_color, width = 3)
game_font = font.nametofont("TkFixedFont")
game_font.config(size = 18)
score = 0
score_text = c.create_text(10, 10, anchor="nw" , font=game_font, fill="darkblue", text="Score: " + str(score))
lives_remaining = 1
lives_text = c.create_text(canvas_width - 10, 10, anchor="ne", font = game_font, fill="darkblue", text = "Lives " + str(lives_remaining))
eggs = []
def create_egg():
x = randrange(10, 740)
y = 40
new_egg = c.create_oval(x, y, x + egg_width, y + egg_height, fill = next(color_cycle), width=0)
eggs.append(new_egg)
root.after(egg_interval, create_egg)
def move_eggs():
for egg in eggs:
(egg_x, egg_y, egg_x2, egg_y2) = c.coords(egg)
c.move(egg, 0, 10)
if egg_y2 > canvas_height:
egg_dropped(egg)
root.after(egg_speed, move_eggs)
def egg_dropped(egg):
eggs.remove(egg)
lose_a_life()
if lives_remaining == 0:
messagebox.showinfo("Game Over!", "Final Score: " + str(score))
root.destroy()
def lose_a_life():
global lives_remaining
lives_remaining -= 1
c.itemconfigure(lives_text, text = "Lives: " + str(lives_remaining))
def check_catch():
(catcher_x, catcher_y, catcher_x2, catcher_y2) = c.coords(catcher)
for egg in eggs:
(egg_x, egg_y, egg_x2, egg_y2) = c.coords(egg)
if catcher_x < egg_x and egg_x2 < catcher_x2 and catcher_y2 - egg_y2 - egg_y2 < 40:
eggs.remove(egg)
c.delete(egg)
increase_score(egg_score)
root.after(100, check_catch)
def increase_score(points):
global score, egg_speed, egg_interval
score += points
egg_speed = int(egg_speed * difficulty_factor)
egg_interval = int(egg_interval * difficulty_factor)
c.itemconfigure(score_text, text="Score: " + str(score))
def move_left(event):
(x1, y1, x2, y2) = c.coords(catcher)
if x1 > 0:
c.move(catcher, -20, 0)
def move_right(event):
(x1, y1, x2, y2) = c.coords(catcher)
if x2 < canvas_width:
c.move(catcher, 20, 0)
c.bind("<Left>" , move_left)
c.bind("<Right>" , move_right)
c.focus_set()
root.after(1000, create_egg)
root.after(1000, move_eggs)
root.after(1000, check_catch)
root.mainloop()
I changed the way it checks for hits and it works now.
def check_catch():
catcher_x, catcher_y, catcher_x2, catcher_y2 = c.coords(catcher)
for egg in eggs:
egg_x, egg_y, egg_x2, egg_y2 = c.coords(egg)
print(c.coords(egg))
if egg_x<catcher_x<egg_x2 and (catcher_y2 - egg_y2) < 5:
eggs.remove(egg)
c.delete(egg)
increase_score(egg_score)
root.after(100, check_catch)

PyCharm can't find 'keysm' in 'event' (Python 3.9 PyCharm 2020.3.3)

I recently tried making a submarine game and made about 132 lines of code. I run the code and pressed the specified keys to move the submarine. Errors came up in the Shell that event doesn't have attribute keysm. Could anyone tell what I am supposed to replace event with or correct keysm?
Full Code:
from tkinter import *
from random import randint
from time import sleep, time
from math import sqrt
HEIGHT = 500
WIDTH = 800
window = Tk()
bub_id = list()
bub_r = list()
bub_speed = list()
MIN_BUB_R = 10
MAX_BUB_R = 30
MAX_BUB_SPD = 10
GAP = 100
window.title('Bubble Blaster')
c = Canvas(window, width=WIDTH, height=HEIGHT, bg='darkblue')
c.pack()
ship_id = c.create_polygon(5, 5, 5, 25, 30, 15, fill='red')
ship_id2 = c.create_oval(0, 0, 30, 30, outline='red')
SHIP_R = 15
MID_X = WIDTH / 2
MID_Y = HEIGHT / 2
SHIP_SPD = 10
BUB_CHANCE = 10
time_text = c.create_text(50, 50, fill='white')
score_text = c.create_text(150, 50, fill='white')
TIME_LIMIT = 30
BONUS_SCORE = 1000
score = 0
bonus = 0
end = time() + TIME_LIMIT
c.move(ship_id, MID_X, MID_Y)
c.move(ship_id2, MID_X, MID_Y)
def move_ship(event):
if event.keysm == 'Up':
c.move(ship_id, 0, -SHIP_SPD)
c.move(ship_id2, 0, -SHIP_SPD)
elif event.keysm == 'Down':
c.move(ship_id, 0, SHIP_SPD)
c.move(ship_id2, 0, SHIP_SPD)
elif event.keysm == 'Left':
c.move(ship_id2, -SHIP_SPD, 0)
c.move(ship_id2, -SHIP_SPD, 0)
elif event.keysm == 'Right':
c.move(ship_id, SHIP_SPD, 0)
c.move(ship_id2, SHIP_SPD, 0)
c.bind_all('<Key>', move_ship)
def create_bubble():
x = WIDTH + GAP
y = randint(0, HEIGHT)
r = randint(MIN_BUB_R, MAX_BUB_R)
id1 = c.create_oval(x - r, y - r, x + r, y + r, outline='white')
bub_id.append(id1)
bub_r.append(r)
bub_speed.append(randint(1, MAX_BUB_SPD))
def move_bubbles():
for i in range(len(bub_id)):
c.move(bub_id[i], -bub_speed[i], 0)
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 distance(id1, id2):
x1, y1 = get_coords(id1)
x2, y2 = get_coords(id2)
return sqrt((x2 - x1)**2 + (y2 - y1)**2)
def del_bubble(i):
del bub_r[i]
del bub_speed[i]
c.delete(bub_id[i])
del bub_id[i]
def collision():
points = 0
for bub in range(len(bub_id)-1, -1, -1):
if distance(ship_id2, bub_id[bub]) < (SHIP_R + bub_r[bub]):
points += (bub_r[bub] + bub_speed[bub])
del_bubble(bub)
return points
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 clean_up_bubs():
for i in range(len(bub_id)-1, -1, -1):
x, y = get_coords(bub_id[i])
if x < -GAP:
del_bubble(i)
c.create_text(50, 30, text='TIME', fill='white')
c.create_text(150, 30, text='SCORE', fill='white')
#MAIN GAME LOOP
while time() < end:
if randint(1, BUB_CHANCE) == 1:
create_bubble()
move_bubbles()
clean_up_bubs()
score += collision()
if (int(score / BONUS_SCORE)) > bonus:
bonus += 1
end += TIME_LIMIT
show_score(score)
show_time(int(end - time()))
window.update()
sleep(0.01)

Why is nothing moving in this python script?

So I copied this code from a python book, and the code is meant to be like an egg catcher game, however if you run the code, you can see, tkinter creates everything and the game sets up, however, the game itself does not start. Does anyone know how to make it start? I tried pressing buttons and making the whole thing a function, however, they both do not work. Here is my code below...
from itertools import cycle
from random import randrange
from tkinter import Canvas, Tk, messagebox, font
canvas_width = 800
canvas_height = 400
root = Tk()
c = Canvas(root, width=canvas_width, height=canvas_height, background='deep sky blue')
c.create_rectangle(-5, canvas_height - 100, canvas_width + 5, canvas_height + 5, \
fill='sea green', width=0)
c.create_oval(-80, -80, 120, 120, fill='orange', width=0)
c.pack()
color_cycle = cycle(['light blue', 'light green', 'light pink', 'light yellow', 'light cyan'])
egg_width = 45
egg_height = 55
egg_score = 10
egg_speed = 500
egg_interval = 4000
difficulty_factor = 0.95
catcher_color = 'blue'
catcher_width = 100
catcher_height = 100
catcher_start_x = canvas_width / 2 - catcher_width / 2
catcher_start_y = canvas_height - catcher_height - 20
catcher_start_x2 = catcher_start_x + catcher_width
catcher_start_y2 = catcher_start_y + catcher_height
catcher = c.create_arc(catcher_start_x, catcher_start_y, \
catcher_start_x2, catcher_start_y2, start=200, extent=140, \
style='arc', outline=catcher_color, width=3)
game_font = font.nametofont('TkFixedFont')
game_font.config(size=18)
score = 0
score_text = c.create_text(10, 10, anchor='nw', font=game_font, fill='darkblue', \
text='Score: ' + str(score))
lives_remaining = 3
lives_text = c.create_text(canvas_width - 10, 10, anchor='ne', font=game_font, fill='darkblue', \
text='Lives: ' + str(lives_remaining))
eggs = []
def create_egg():
x = randrange(10, 740)
y = 40
new_egg = c.create_oval(x, y, x + egg_width, y + egg_height, fill=next(color_cycle), width=0)
eggs.append(new_egg)
root.after(egg_interval, create_egg)
def move_eggs():
for egg in eggs:
(egg_x, egg_y, egg_x2, egg_y2) = c.coords(egg)
c.move(egg, 0, 10)
if egg_y2 > canvas_height:
egg_dropped(egg)
root.after(egg_speed, move_eggs)
def egg_dropped(egg):
eggs.remove(egg)
c.delete(egg)
lose_a_life()
if lives_remaining == 0:
messagebox.showinfo('Game Over!', 'Final Score: ' + str(score))
root.destroy()
def lose_a_life():
global lives_remaining
lives_remaining -= 1
c.itemconfigure(lives_text, text='Lives: ' + str(lives_remaining))
def check_catch():
(catcher_x, catcher_y, catcher_x2, catcher_y2) = c.coords(catcher)
for egg in eggs:
(egg_x, egg_y, egg_x2, egg_y2) = c.coords(egg)
if catcher_x < egg_x and egg_x2 < catcher_x2 and catcher_y2 - egg_y2 < 40:
eggs.remove(egg)
c.delete(egg)
increase_score(egg_score)
root.after(100, check_catch)
def increase_score(points):
global score, egg_speed, egg_interval
score += points
egg_speed = int(egg_speed * difficulty_factor)
egg_interval = int(egg_interval * difficulty_factor)
c.itemconfigure(score_text, text='Score: ' + str(score))
def move_left(event):
(x1, y1, x2, y2) = c.coords(catcher)
if x1 > 0:
c.move(catcher, -20, 0)
def move_right(event):
(x1, y1, x2, y2) = c.coords(catcher)
if x2 < canvas_width:
c.move(catcher, 20, 0)
c.bind('<Left>', move_left)
c.bind('<Right>', move_right)
c.focus_set()
root.after(1000, create_egg)
root.after(1000, move_eggs)
root.after(1000, check_catch)
root.mainloop()
I see lots of functions but no calls to them, especially the function that runs the Tkinter main loop.
Given the similarity you would expect between the move_left() and move_right() functions, it's a near certainty that the final lines of the latter should not be indented to where they are.
With your current code, the lines doing the binding and timer call-back setups are only executed when move_right() is called. Which it isn't, because the key binding that would call it is only set up when it's called (which it isn't because ...
WARNING W-42: thought process terminated due to likely stack overflow in wet-ware :-)
In other words, your code should be something like:
def move_left(event):
(x1, y1, x2, y2) = c.coords(catcher)
if x1 > 0:
c.move(catcher, -20, 0)
def move_right(event):
(x1, y1, x2, y2) = c.coords(catcher)
if x2 < canvas_width:
c.move(catcher, 20, 0)
# Run this at top level, NOT as part of move_right().
c.bind('<Left>', move_left)
c.bind('<Right>', move_right)
c.focus_set()
root.after(1000, create_egg)
root.after(1000, move_eggs)
root.after(1000, check_catch)
root.mainloop()
As an aside, it may just be the way you pasted the code in, but you should try to follow the PEP8 style guide when writing code, specifically the four-space-indent guideline. They generally make your code much easier to read and/or analyse.
And, just so you're aware, those move_xxx() functions will actually allow your catcher to go off the edges of the screen (up to twenty pixels, I think). If that's not what you want, you may want to make a small change:
move_sz = 20
def move_left(event):
(x1, y1, x2, y2) = c.coords(catcher)
c.move(catcher, -min(move_sz, x1), 0)
def move_right(event):
(x1, y1, x2, y2) = c.coords(catcher)
c.move(catcher, min(move_sz, canvas_width - x2 - 1), 0)
I haven't tested that (particularly the possibility of being out by one pixel - I'll leave that up to you) but the idea is just to limit how much you move if that would send your catcher outside the canvas bounds.
You'll see I've also replaced the magic constant 20 with a more appropriate variable - that's so you only need to change it in one place should you want to adjust the movement speed.

How to display and update a score on screen with Tkinter? [duplicate]

My question is about displaying and updating text, in order to display the score on screen.
I would like to create a score like the real game that would appear on the screen. But after researching Google, I have not found anyone wishing to increase a score on the screen ...
Indeed, I would like the score to increase each time the bird passes between the pipes and therefore whenever the pipes have an X of 67 pixels. So does anyone know how to do this?
from tkinter import *
import random
from random import randint
def sauter(event):
canvas.move(image_oiseau, 0, -10*DY)
def deplacement():
global tuyx,tuyx2,h,H,oisx,oisy,solx,sol2x
x0, y0, x1, y1 = canvas.bbox(image_oiseau)
if y1 < 416:
canvas.move(image_oiseau, 0, DY)
canvas.coords(image_sol,solx,512)
if solx >= -144:
solx=solx-5
else:
solx=144
canvas.coords(image_sol2,sol2x,512)
if sol2x >= 144:
sol2x=sol2x-5
else:
sol2x=432
canvas.coords(image_tuyau_haut,tuyx,h)
canvas.coords(image_tuyau_bas,tuyx,h-241)
if tuyx>=-28:
tuyx=tuyx-5
else:
tuyx=316
h=randint(256,505)
canvas.coords(image_tuyau_haut2,tuyx2,H)
canvas.coords(image_tuyau_bas2,tuyx2,H-241)
if tuyx2>=-28:
tuyx2=tuyx2-5
else:
tuyx2=316
H=randint(256,505)
canvas.after(40,deplacement)
LARGEUR = 286
HAUTEUR = 510
DY = 5
tuyx=316
tuyx2=488
h=randint(256,505)
H=randint(256,505)
oisx=67
oisy=244
solx=144
sol2x=432
fenetre = Tk()
canvas = Canvas(fenetre, width=LARGEUR, height=HAUTEUR)
fond = PhotoImage(file="background-day.png")
fond2 = PhotoImage(file="background-night.png")
fond=[fond,fond2]
F= random.choice(fond)
canvas.create_image(144,256, anchor=CENTER,image=F)
tuyau_haut = PhotoImage(file="tuyau_vers_le_haut.png")
image_tuyau_haut = canvas.create_image(tuyx,h,anchor=CENTER,image=tuyau_haut)
image_tuyau_haut2 = canvas.create_image(tuyx2,H,anchor=CENTER,image=tuyau_haut)
tuyau_bas = PhotoImage(file="tuyau_vers_le_bas.png")
image_tuyau_bas = canvas.create_image(tuyx,h,anchor=CENTER,image=tuyau_bas)
image_tuyau_bas2 = canvas.create_image(tuyx2,H,anchor=CENTER,image=tuyau_bas)
sol = PhotoImage(file="sol-day.png")
image_sol = canvas.create_image(144,512, anchor=S,image=sol)
image_sol2 = canvas.create_image(432,512, anchor=S,image=sol)
oiseau = PhotoImage(file="yellowbird-midflap.png")
oiseau2 = PhotoImage(file="bluebird-midflap.png")
oiseau3 = PhotoImage(file="redbird-midflap.png")
oiseau=[oiseau,oiseau2,oiseau3]
O=random.choice(oiseau)
image_oiseau=canvas.create_image(oisx,oisy, anchor=W,image=O)
deplacement()
canvas.pack()
canvas.focus_set()
canvas.bind("<space>",sauter)
fenetre.mainloop()
Could someone explain the problem to me because I thought it would be easy :(
Here are the pictures of the game :)
Here are the pictures of the game
Here is one approach to display the scores: It uses a tk.Label, that is updated at the same time the score increases.
The trigger that increases the score is currently a random call to on_change; you can modify this to be a test if a pipe x coordinates becomes lower than the bird x coordinates (the bird successfully crossed the obstacle)
You can, if you want relocate the score label on the canvas.
import random
import tkinter as tk
WIDTH, HEIGHT = 500, 500
def create_pipes():
pipes = []
for x in range(0, WIDTH, 40):
y1 = random.randrange(50, HEIGHT - 50)
y0 = y1 + 50
pipes.append(canvas.create_line(x, 0, x, y1))
pipes.append(canvas.create_line(x, y0, x, HEIGHT))
return pipes
def move_pipes():
for pipe in pipes:
canvas.move(pipe, -2, 0)
x, y0, _, y1 = canvas.coords(pipe)
if x < 0:
canvas.coords(pipe, WIDTH+20, y0, WIDTH+20, y1)
if random.randrange(0, 20) == 10:
on_change()
root.after(40, move_pipes)
def on_change():
global score
score += 1
score_variable.set(f'score: {score}')
root = tk.Tk()
tk.Button(root, text='start', command=move_pipes).pack()
score = 0
score_variable = tk.StringVar(root, f'score: {score}')
score_lbl = tk.Label(root, textvariable=score_variable)
score_lbl.pack()
canvas = tk.Canvas(root, width=WIDTH, height=HEIGHT, bg="cyan")
canvas.pack()
pipes = create_pipes()
root.mainloop()

Categories

Resources