gpt4 book ai didi

python - 尝试使球对象从桨上弹起

转载 作者:太空宇宙 更新时间:2023-11-03 15:14:58 24 4
gpt4 key购买 nike

我是 python 新手,正在为我的 python 入门类(class)做一个最终项目。我已经完成了大部分桨球游戏,但无法弄清楚如何使球对象从我的桨对象上弹开。

我在 Stackoverflow 上研究了一段时间,并花了几个小时试图自己解决这个问题,但没有成功。如果有人有任何想法,我真的可以使用帮助。

如果有什么我需要更好地解释以便您更好地理解,请发表评论。

图形用户界面文件:

Import tkinter, random, particle, and helpers
from tkinter import *
from ball import *
from paddle import *
from time import *

class PaddleBall:
def __init__(self, window):
''' Construct the paddle ball GUI '''
self.window = window
self.window.protocol('WM_DELETE_WINDOW', self.safe_exit)
self.width = 700
self.height = 900
self.canvas = Canvas(self.window, bg='black', width=self.width, height=self.height, highlightthickness=0)
self.canvas.bind_all("<KeyPress-Left>", self.move_left)
self.canvas.bind_all("<KeyPress-Right>", self.move_right)

self.canvas.pack()

# Create a label to indicate instructions
instructions = Label(window, text="Controls: Left & Right Arrow Keys")
instructions.pack(side=BOTTOM, expand=YES)

# Create a button to clear Ball
restart_button = Button(window, text="Play", command=self.reset)
restart_button.pack(side=BOTTOM, expand=YES)

self.ball = Ball(350, 350)
self.paddle = Paddle(300, 850, 400, 860, 0, 0)
self.terminated = False
self.render()

def ballobject(self):
self.ball = Ball(350, 350)
self.paddle = Paddle(300, 850, 400, 860, 0, 0)
self.render()

def reset(self):
self.terminated = True

def safe_exit(self):
''' Turn off the event loop before closing the GUI '''
self.terminated = True
self.window.destroy()

# Render everything
def render(self):
# While program is not terminated
if not self.terminated:
# Erase Canvas
self.canvas.delete(ALL)

# Move ball
self.ball.move(self.canvas, self.paddle)

# Render ball
self.ball.render(self.canvas)

# Render paddle
self.paddle.render(self.canvas)

# use distance() to detect collision between ball and paddle.
'''Ball.bounce(self)'''

# Animate the particles movement
self.canvas.after(10, self.render)

else:
# Erase Canvas
self.canvas.delete(ALL)

self.terminated = False

self.canvas.after(50, self.ballobject)


def move_left(self, event):
self.paddle.move_left(event)

def move_right(self, event):
self.paddle.move_right(event)


if __name__ == '__main__':
root = Tk()
root.option_add('*font', ('Verdana', 12, 'bold italic')) # Found at http://effbot.org/tkinterbook/tkinter-widget-styling.htm
root.resizable(0,0) # Found at https://mail.python.org/pipermail/tutor/2001-September/008504.html
root.title('Paddle Ball')
root.wm_attributes("-topmost", -1)
app = PaddleBall(root)
root.mainloop()

球类文件:

class Ball:
'''
Ball models a single ball that may be rendered to a canvas
'''

def __init__(self, x, y, radius = 15,):
'''
Constructor
'''
self._x = x
self._y = y
self._velX = randint(-10,10)
self._velY = randint(-10,-5)
self._radius = radius
self._color = 'white'
self._tx = 350
self._ty = 400
self._t = ""
self._tfill = "red"
self._tfont = ("Arial", 35, "bold italic")

# This method renders the ball
def render(self, canvas):
canvas.create_oval(self._x - self._radius, self._y - self._radius, self._x + self._radius, self._y + self._radius, fill = self._color)
canvas.create_text(self._tx, self._ty, text = self._t, fill = self._tfill, font = self._tfont)


# This method moves the ball
def move(self, canvas, Paddle):
# Update Position

self._x += self._velX
self._y += self._velY

# If the ball hits any of the wall negate the velocity
if (self._x + self._radius > canvas.winfo_reqwidth() and self._velX > 0) or (self._x - self._radius < 0 and self._velX < 0):
self._velX = -self._velX
if (self._y + self._radius < 0 and self._velY < 0):
self._velY = -self._velY
if (self._y + self._radius > canvas.winfo_reqheight() and self._velY > 0):
self._velY = 0
self._velX = 0
self._t = " GAME OVER! \n Click the play button to play again."


#*****THIS IS WHAT I'M HAVING TROUBLE WITH******
# Determine if the ball hits the paddle
if ((self._x + self._radius > Paddle._x(self) and self._velX > 0) or (self._x + self._radius < Paddle._x2(self))) and (self._y < Paddle._y(self)):
self._velX = -self._velX

桨类文件:

# Import math and helpers
from tkinter import *
import math
from gui import *


class Paddle:

def __init__(self, x, y, x2, y2, velX, velY):
'''
Constructor
'''
self._x = x
self._y = y
self._x2 = x2
self._y2 = y2
self._velX = velX
self._velY = velY
self._color = 'white'

def getpadx(self):
return self._x

def getpady(self):
return self._y

def getpadx1(self):
return self._x2

def getpady2(self):
return self._y2

# This method renders the paddle
def render(self, canvas):
canvas.create_rectangle(self._x, self._y, self._x2, self._y2, fill = self._color)

# This method moves the paddle
def move(self, canvas):
# Update Position

# If the paddle hits any of the wall negate the velocity
if (self._x + self._radius > canvas.winfo_reqwidth() and self._velX > 0) or (self._x - self._radius < 0 and self._velX < 0):
self._velX = -self._velX

def move_left(self, event):
self._x -= 35
self._x2 -= 35

def move_right(self, event):
self._x += 35
self._x2 += 35

最佳答案

在 friend 的帮助下我解决了这个问题。我所要做的就是更改此代码:

从此:

if ((self._x + self._radius > Paddle._x(self) and self._velX > 0) or (self._x + self._radius < Paddle._x2(self))) and (self._y < Paddle._y(self)):
self._velX = -self._velX

对此:

`if (self._x > Paddle._x) and (self._x < Paddle._x2):
if (self._y + self._radius > Paddle._y):
self._velY = -self._velY
self._velX = self._velX + randint(-2,2)`

关于python - 尝试使球对象从桨上弹起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43943547/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com