Pong game in python

AnneONyme -  
 AnneONyme -
Hello.
With the help of the book "I Learn to Program with Python" from Usborne editions, I'm trying to program a pong game (with a single paddle that is controlled with the arrow keys). This game is the simplest version as there is no score, and the ball doesn't speed up as the game progresses. Despite this, I am encountering a problem: when the ball drops, the book explains how to display a message "Do you want to play again?" with two buttons "yes" and "no". If you click on no, the tkinter window closes, and if you click on yes, the game restarts. That's what the program is supposed to do... When the player clicks on yes, the paddle is positioned to the left, the ball just above, but pressing the left and right arrow keys has no effect. > (At first, I thought it was me who had typed the code wrong from my book, so I directly downloaded the code from the website www.usborne.com. Same problem. I therefore deduce that they made a mistake in writing the book. No one seems to have had the same problem as me... I am using python 3.7.1 and here is my code:

**Modified by the moderation for easier reading of the code, in the future use the tags, SEE THIS PAGE

import tkinter from tkinter import messagebox import time canvasWidth = 750 canvasHeight = 500 window = tkinter.Tk() canvas = tkinter.Canvas(window, width=canvasWidth, height=canvasHeight, bg="dodgerblue4") canvas.pack() paddle = canvas.create_rectangle(0, 0, 40, 10, fill="dark turquoise") ball = canvas.create_oval(0, 0, 10, 10, fill="deep pink") window_open = True def play_pong(): while window_open == True: move_paddle() move_ball() window.update() time.sleep(0.02) if window_open == True: check_game_over() left_pressed = 0 right_pressed = 0 def when_key_pressed(event): global left_pressed, right_pressed if event.keysym == "Left": left_pressed = 1 elif event.keysym == "Right": right_pressed = 1 def when_key_released(event): global left_pressed, right_pressed if event.keysym == "Left": left_pressed = 0 elif event.keysym == "Right": right_pressed = 0 paddle_speed = 6 def move_paddle(): paddle_movement = paddle_speed * right_pressed - paddle_speed * left_pressed (left_paddle, top_paddle, right_paddle, bottom_paddle) = canvas.coords(paddle) if (left_paddle > 0 or paddle_movement > 0) and (right_paddle < canvasWidth or paddle_movement < 0): canvas.move(paddle, paddle_movement, 0) ball_movement_X = 4 ball_movement_Y = -4 top_paddle_limit = canvasHeight-40 bottom_paddle_limit = canvasHeight-30 def move_ball(): global ball_movement_X, ball_movement_Y (left_ball, top_ball, right_ball, bottom_ball) = canvas.coords(ball) if ball_movement_X > 0 and right_ball > canvasWidth: ball_movement_X = -ball_movement_X if ball_movement_X < 0 and left_ball < 0: ball_movement_X = -ball_movement_X if ball_movement_Y < 0 and top_ball < 0: ball_movement_Y = -ball_movement_Y if ball_movement_Y > 0 and bottom_ball > top_paddle_limit and bottom_ball < bottom_paddle_limit: (left_paddle, top_paddle, right_paddle, bottom_paddle) = canvas.coords(paddle) if right_ball > left_paddle and left_ball < right_paddle: ball_movement_Y = -ball_movement_Y canvas.move(ball, ball_movement_X, ball_movement_Y) def check_game_over(): (left_ball, top_ball, right_ball, bottom_ball) = canvas.coords(ball) if top_ball > canvasHeight: replay = tkinter.messagebox.askyesno(message = "Do you want to play again?") if replay == True: reset() else: close() def close(): global window_open window_open = False window.destroy() def reset(): global left_pressed, right_pressed global ball_movement_X, ball_movement_Y left_pressed = 0 right_pressed = 0 ball_movement_X = 4 ball_movement_Y = -4 canvas.coords(paddle, 10, top_paddle_limit, 50, bottom_paddle_limit) canvas.coords(ball, 20, top_paddle_limit-10, 30, top_paddle_limit) window.protocol("WM_DELETE_WINDOW", close) window.bind("<keypress>", when_key_pressed) window.bind("<keyrelease>", when_key_released) reset() play_pong()</keyrelease></keypress>

5 answers

ghobi
 
Hello,

It's simply a matter of focus.

When the toplevel appears asking the player if they want to replay, the canvas loses focus to the toplevel, and when this is closed by clicking the yes button, the focus is given to another widget at the discretion of the operating system's graphical manager; in your case, it seems to be on the main Tk window, while for me and others, it goes to the previous widget that had it.

It is therefore necessary to force the focus back on the canvas.
In the reset function, simply adding the instruction
canvas.focus_set()
will ensure that focus is returned to the canvas before starting a game.
0
AnneONyme
 
ARRRGH !!! It's not working!
0
AnneONyme
 
On the other hand, the first time we play, there's a sort of black frame that wasn't there before... the focus?
0
AnneONyme
 
A last thing, "I'm learning to program with Python" is a popularization book and I'm twelve years old, so toplevel, focus, widget, can you explain them to me? :-/
0
AnneONyme
 
Ah, yes! But the focus only arrives when you click on the window. Is there a way to fix that? In any case, thank you for helping me!
0