簡體   English   中英

簡單的槳球游戲,球不彈跳

[英]Simple paddleball game, ball not bouncing

我正在關注 Python 4 個孩子,我們在其中創建了一個簡單的板球游戲。 我以前能夠讓球從牆上反彈,但現在在本章結束時它不再起作用了。 運行文件時,我也沒有在終端中看到任何錯誤。

`

from tkinter import *
import random
import time

class Ball:
    def __init__(self, canvas, paddle, color):
        self.canvas = canvas
        self.paddle = paddle
        self.id = canvas.create_oval(10, 10, 25, 25, fill=color)
        self.canvas.move(self.id, 245, 100)
        starts = [-3, -2, -1, 1, 2, 3]
        self.x = random.choice(starts)
        self.y = -1
        self.canvas_height = self.canvas.winfo_height()
        self.canvas_width = self.canvas.winfo_width()
        self.hit_bottom = False
    
    def hit_paddle(self, pos):
        paddle_pos = self.canvas.coords(self.paddle.id)
        if pos[2] >= paddle_pos[0] and pos[0] <= paddle_pos[2]:
            if pos[3] >= paddle_pos[1] and pos[3] <= paddle_pos[3]:
                return True
        return False
    
    def draw(self):
        self.canvas.move(self.id, self.x, self.y)
        pos = self.canvas.coords(self.id)
        if pos[1] <= 0:
            self.y = self.y * -1
        if pos[3] >= self.canvas_height:
            self.hit_bottom = True
        if self.hit_paddle(pos) == True:
            self.y = self.y * -1
        if pos[0] <= 0 or pos[2] >= self.canvas_width:
            self.y = self.y * -1
        
        
class Paddle:
    def __init__(self, canvas, color):
        self.canvas = canvas
        self.id = canvas.create_rectangle(0, 0, 100, 10, fill=color)
        self.canvas.move(self.id, 200, 300)
        self.x = 0
        self.canvas_width = self.canvas.winfo_width()
        self.canvas.bind_all('<KeyPress-Left>', self.turn_left)
        self.canvas.bind_all('<KeyPress-Right>', self.turn_right)
    
    def draw(self):
        self.canvas.move(self.id, self.x, 0)
        pos = self.canvas.coords(self.id)
        if pos[0] <= 0 or pos[2] >= self.canvas_width:
            self.x = 0
    
    def turn_left(self, evt):
        self.x = -2
    
    def turn_right(self, evt):
        self.y = 2

tk = Tk()
tk.title('Bounce Game')
tk.resizable(0, 0)
tk.wm_attributes('-topmost', 1)
canvas = Canvas(tk, width=500, height=400, bd=0, highlightthickness=0)
canvas.pack()
tk.update()

paddle = Paddle(canvas, 'blue')
ball = Ball(canvas, paddle, 'red' )

while True:
    if ball.hit_bottom == False:
        ball.draw()
        paddle.draw()
    tk.update_idletasks()
    tk.update()
    time.sleep(0.01)

tk.mainloop()

`

球應該從牆壁和球拍上彈起。

這只是您需要反轉坐標的疏忽。

問題出在球class中抽取function

def draw(self):
    self.canvas.move(self.id, self.x, self.y)
    pos = self.canvas.coords(self.id)
    if pos[1] <= 0:
        self.y = self.y * -1
    if pos[3] >= self.canvas_height:
        self.hit_bottom = True
    if self.hit_paddle(pos) == True:
        self.y = self.y * -1
    if pos[0] <= 0 or pos[2] >= self.canvas_width:
        self.y = self.y * -1

在最后一個 if 語句中,你說如果球已經到達屏幕的左邊界或右邊界,那么我們要反轉 y 坐標。 你真正想要做的是反轉 x 坐標!

修復只是改變...

if pos[0] <= 0 or pos[2] >= self.canvas_width:
    self.y = self.y * -1

到...

if pos[0] <= 0 or pos[2] >= self.canvas_width:
    self.x = self.x * -1

為了使您的代碼更具可讀性,可能需要進行一些額外的更改

def draw(self):
    self.canvas.move(self.id, self.x, self.y)
    pos = self.canvas.coords(self.id)

    if pos[1] <= 0 or self.hit_paddle(pos):
        self.y = -self.y

    if pos[3] >= self.canvas_height:
        self.hit_bottom = True

    if pos[0] <= 0 or pos[2] >= self.canvas_width:
        self.x = -self.x

也許 turn_right 方法應該是 self。 x = 2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM