admin 管理员组

文章数量: 1086019

I am trying to write a pong game in python tkinter how can I keep the paddle Inbounds? so that will NOT go off of the screen? I have done some research and haven't found a working answer.

the code goes like this :

import pygame
import tkinter as tk
 
class MoveCanvas(tk.Canvas):
 
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
 
        self.dx = 0
        self.dy = 0
        self.x = 5
        self.y = 5
       
        #These lines define where the paddles start and end

        self.padL_top = 45
        self.padL_bottom = 115
       
        self.padR_top = 45
        self.padR_bottom = 115
       
        self.dy_padL = 0
        self.dy_padR = 0
        self.label_win = tk.Label(text = "")
        self.label_win.pack()
         
        #The numbers inside the parentheses define the size of the ball
        self.ball = self.create_oval(0, 0, 20, 20, fill="red", outline = "red")
       
        self.left_paddle = self.create_rectangle(5, self.padL_top, 20, self.padL_bottom, fill="green", outline = "green")
        self.right_paddle = self.create_rectangle(480, self.padR_top, 495, self.padR_bottom, fill="blue", outline = "blue")

        pygame.mixer.init()
        #self.boop = pygame.mixer.music.load("strike.mp3")
         
        self.dt = 25
        self.tick()
       

    def play_boop(self):
        pygame.mixer.music.play(loops=0)
       
     
    def tick(self):
        self.move(self.ball, self.dx, self.dy)
        self.move(self.left_paddle, 0, self.dy_padL)
        self.move(self.right_paddle, 0, self.dy_padR)
        #keeps track of ball location
        self.x = self.x + self.dx
        self.y = self.y + self.dy
        #checks for winner
        if self.x > 499:
            self.label_win.config(text = "Blue Wins!",font = ("Helvetica", 20))
           
        if self.x < 1:
            self.label_win.config(text = "Green Wins!",font = ("Helvetica", 20))
           
           
       
        #keeps track of left paddle location
        self.padL_top = self.padL_top + self.dy_padL
        self.padL_bottom = self.padL_bottom + self.dy_padL
       
        #keeps track of right paddle location
        self.padR_top = self.padR_top + self.dy_padR
        self.padR_bottom = self.padR_bottom + self.dy_padR
       
        #checks to see if ball hits left paddle
        if self.y in range(self.padL_top, self.padL_bottom) and self.x < 20:
            pygame.mixer.music.load("strike.mp3")
            pygame.mixer.music.play(loops=0)
           
            #self.play_boop()
            self.dx = -1 * self.dx
           
        #checks to see if ball hits right paddle
        if self.y in range(self.padR_top, self.padR_bottom) and self.x > 470:
            pygame.mixer.music.load("strike.mp3")
            pygame.mixer.music.play(loops=0)
            #self.play_boop()
            self.dx = -1 * self.dx

        #checks to see if ball hits top or bottom of panel
        if self.y not in range(7, 266):
            self.dy = -1 * self.dy
        self.after(self.dt, self.tick)

        if self.x > 495:
            pygame.mixer.music.load("strike.mp3")
            pygame.mixer.music.play(loops=0)
           
 
    def change_heading_ball(self, dx, dy):
        self.dx = dx
        self.dy = dy

    def change_heading_padL(self, dy_padL):
        self.dy_padL = dy_padL

    def change_heading_padR(self, dy_padR):
        self.dy_padR = dy_padR
 
 
if __name__ == "__main__":
 
    root = tk.Tk()
    root.title("Simple Pong")
    root.geometry("500x300")
 
    cvs = MoveCanvas(root, bg = "yellow")
    cvs.pack(fill="both", expand=True)
 
    #This number controls the ball speed
    ds_ball = 3

    #This number controls the paddle speed
    ds_paddle = 10
 
    root.bind("<KeyPress-Left>", lambda _: cvs.change_heading_ball(-ds_ball, ds_ball))
    root.bind("<KeyPress-Right>", lambda _: cvs.change_heading_ball(ds_ball, ds_ball))
    root.bind("<KeyPress-w>", lambda _: cvs.change_heading_padL(-ds_paddle))
    root.bind("<KeyPress-s>", lambda _: cvs.change_heading_padL(ds_paddle))
    root.bind("<KeyRelease-w>", lambda _: cvs.change_heading_padL(0 * ds_paddle))
    root.bind("<KeyRelease-s>", lambda _: cvs.change_heading_padL(0 * ds_paddle))
    root.bind("<KeyPress-Up>", lambda _: cvs.change_heading_padR(-ds_paddle))
    root.bind("<KeyPress-Down>", lambda _: cvs.change_heading_padR(ds_paddle))
    root.bind("<KeyRelease-Up>", lambda _: cvs.change_heading_padR(0 * ds_paddle))
    root.bind("<KeyRelease-Down>", lambda _: cvs.change_heading_padR(0 * ds_paddle))

     
    root.mainloop()

I need this done for a school assignment, and i am trying to keep the PADDLE inbounds NOT the ball

本文标签: is there a way to Limit Movement in a python tkinter canvas gameStack Overflow