python ткинтер прыгающий мяч - PullRequest
0 голосов
/ 06 февраля 2020

Я делаю основную игру c. Я хочу, чтобы мяч отскакивал ТОЛЬКО, когда он ударяет по платформе. Пока что я написал код, который заставит мяч отскочить от верхнего и нижнего экрана, но у меня возникли проблемы с отскоком мяча от платформы.

from tkinter import *
import time
import tkinter
tk = Tk()

canvas = Canvas(tk, bg="white",width=(900),height=(500))
canvas.pack()

platform = canvas.create_rectangle(400,400,500,410)

def ball():
    xspeed = 2
    yspeed = 2
    ball = canvas.create_oval(430,10,470,50)
    while True:
        canvas.move(ball, xspeed, yspeed)
        pos = canvas.coords(ball)
        if pos[2] >=900 or pos[0] <0:
            xspeed = -xspeed
        tk.update()
        time.sleep(0.01)


def board():
    board_right()
    board_left()


def board_right(event):
    xspeed = 5
    yspeed = 0
    canvas.move(platform,xspeed,yspeed)
    tk.update
    time.sleep(0.01)

def board_left(event):
    xspeed = 5
    yspeed = 0
    canvas.move(platform,-xspeed,yspeed)
    tk.update()
    time.sleep(0.01)


canvas.bind_all("<Right>",board_right)
canvas.bind_all("<Left>",board_left)
ball()
tk.mainloop()

, пожалуйста, помогите мне

1 Ответ

0 голосов
/ 07 февраля 2020

Не используйте time.sleep(), поскольку он заблокирует tkinter mainloop, вместо этого используйте after().

Чтобы проверить, попадает ли мяч на платформу, вам нужно получить центр х шара и нижний у мяча. Если центр x находится слева и справа от платформы, а нижний шар y - верхний край платформы y, измените скорость вращения мяча y. В противном случае игра окончена!

Ниже приведен пример кода, основанного на вашем:

import tkinter as tk

root = tk.Tk()

width = 900
height = 500

canvas = tk.Canvas(root, bg='white', width=width, height=height)
canvas.pack()

ball = canvas.create_oval(430, 10, 470, 50, fill='green')

platform_y = height - 20
platform = canvas.create_rectangle(width//2-50, platform_y, width//2+50, platform_y+10, fill='black')

# ball moving speed
xspeed = yspeed = 2

def move_ball():
  global xspeed, yspeed
  x1, y1, x2, y2 = canvas.coords(ball)
  if x1 <= 0 or x2 >= width:
    # hit wall, reverse x speed
    xspeed = -xspeed
  if y1 <= 0:
    # hit top wall
    yspeed = 2
  elif y2 >= platform_y:
    # calculate center x of the ball
    cx = (x1 + x2) // 2
    # check whether platform is hit
    px1, _, px2, _ = canvas.coords(platform)
    if px1 <= cx <= px2:
      yspeed = -2
    else:
      canvas.create_text(width//2, height//2, text='Game Over', font=('Arial Bold', 32), fill='red')
      return
  canvas.move(ball, xspeed, yspeed)
  canvas.after(20, move_ball)

def board_right(event):
  x1, y1, x2, y2 = canvas.coords(platform)
  # make sure the platform is not moved beyond right wall
  if x2 < width:
    dx = min(width-x2, 10)
    canvas.move(platform, dx, 0)

def board_left(event):
  x1, y1, x2, y2 = canvas.coords(platform)
  # make sure the platform is not moved beyond left wall
  if x1 > 0:
    dx = min(x1, 10)
    canvas.move(platform, -dx, 0)

canvas.bind_all('<Right>', board_right)
canvas.bind_all('<Left>', board_left)

move_ball()

root.mainloop()
...