我有一个类,当球击中屏幕的极限时球反弹
我尝试编程ballchange我的颜色与限制碰撞时,但这不会改变颜色,保持color_default值而不改变,我不知道?
以下是代码:
from pygame.sprite import Sprite
from pygame import Surface, draw, mouse
from GeneralInformation import R, G, B
class Ball(Sprite):
def __init__(self, display)#sorry for bad indent in this example
super(Ball, self).__init__()
self.elasticity = .8
self.radius = 30
self.friction = 1
self.image = Surface([self.radius*2, self.radius*2])
self.rect = self.image.get_rect()
self.rect.x = 100
self.rect.y = 100
self.dx = 0
self.dy = 0
self.color_default = (R+130, G-40, B-60)
self.color_change1 = (R-20, G+20, B+50)
self.color_change2 = (R-40, G+70, B+100)
self.display = display
self.delta_time = 0
self.pos_x_aux = 0
self.pos_y_aux = 0
self.controlled = False
def calculate_gravity(self):
"""Function for calcule the gravity"""
self.dy += .20
def impulse(self, dx, dy):
self.dx = dx
self.dy = dy
def update(self, dt):
self.calculate_gravity()
self.rect.x = self.rect.x + self.dx
self.rect.y = self.rect.y + self.dy
self.delta_time = self.delta_time + dt
if self.controlled:
self.rect.x = mouse.get_pos()[0] - self.radius
self.rect.y = mouse.get_pos()[1] - self.radius
if self.delta_time > 50:
self.pos_x_aux = mouse.get_pos()[0]
self.pos_y_aux = mouse.get_pos()[1]
self.delta_time = 0
if not self.controlled and self.pos_x_aux != 0 and self.pos_y_aux != 0:
self.dx = mouse.get_pos()[0] - self.pos_x_aux
self.dy = mouse.get_pos()[1] - self.pos_y_aux
self.pos_y_aux = 0
self.pos_x_aux = 0
self.check_collisions()
self.draw()
def check_collisions(self):
if self.rect.y + self.radius*2 > self.display.get_size()[1]:
self.rect.y = self.display.get_size()[1]-self.radius*2
self.impulse(self.dx, -self.dy*self.elasticity)
if self.rect.x + self.radius * 2 > self.display.get_size()[0]:
self.rect.x = self.display.get_size()[0] - self.radius * 2
self.impulse(-self.dx*self.elasticity, self.dy)
if self.rect.x < 0:
self.rect.x = 0
self.impulse(-self.dx*self.elasticity, self.dy)
if self.rect.y < 0:
self.rect.y = 0
self.impulse(-self.dy*self.elasticity, self.dx)
def draw(self):
draw.ellipse(self.display, self.color_default, self.rect)
if self.rect.y + self.radius * 2 > self.display.get_size()[1]:
draw.ellipse(self.display, self.color_change1, self.rect)
if self.rect.x + self.radius * 2 > self.display.get_size()[0]:
draw.ellipse(self.display, self.color_change2, self.rect)
if self.rect.x < 0:
draw.ellipse(self.display, self.color_change1, self.rect)
if self.rect.y < 0:
draw.ellipse(self.display, self.color_change2, self.rect)
使用此代码,我试着按照给我的建议给我TypeError: draw.ellipse(self.display,self.current_color,self.rect) TypeError:颜色无效参数
# coding=utf-8
from pygame.sprite import Sprite
from pygame import Surface, draw, mouse
from GeneralInformation import R, G, B
class Ball(Sprite):
def __init__(self, display):
super(Ball, self).__init__()
self.elasticity = .8
self.radius = 30
self.friction = 1
self.image = Surface([self.radius*2, self.radius*2])
self.rect = self.image.get_rect()
self.rect.x = 100
self.rect.y = 100
self.current_color = []
self.dx = 0
self.dy = 0
self.color_default = [R+130, G-40, B-60]
self.color_change1 = [R-20, G+20, B+50]
self.color_change2 = [R-40, G+70, B+100]
self.display = display
self.delta_time = 0
self.pos_x_aux = 0
self.pos_y_aux = 0
self.controlled = False
def calculate_gravity(self):
"""Function for calcule the gravity"""
self.dy += .20
def impulse(self, dx, dy):
self.dx = dx
self.dy = dy
def update(self, dt):
self.calculate_gravity()
self.rect.x = self.rect.x + self.dx
self.rect.y = self.rect.y + self.dy
self.delta_time = self.delta_time + dt
if self.controlled:
self.rect.x = mouse.get_pos()[0] - self.radius
self.rect.y = mouse.get_pos()[1] - self.radius
if self.delta_time > 50:
self.pos_x_aux = mouse.get_pos()[0]
self.pos_y_aux = mouse.get_pos()[1]
self.delta_time = 0
if not self.controlled and self.pos_x_aux != 0 and self.pos_y_aux != 0:
self.dx = mouse.get_pos()[0] - self.pos_x_aux
self.dy = mouse.get_pos()[1] - self.pos_y_aux
self.pos_y_aux = 0
self.pos_x_aux = 0
self.check_collisions()
self.draw()
def check_collisions(self):
if self.rect.y + self.radius*2 > self.display.get_size()[1]:
self.rect.y = self.display.get_size()[1]-self.radius*2
self.impulse(self.dx, -self.dy*self.elasticity)
self.current_color.append(self.color_change1)
if self.rect.x + self.radius * 2 > self.display.get_size()[0]:
self.rect.x = self.display.get_size()[0] - self.radius * 2
self.impulse(-self.dx*self.elasticity, self.dy)
self.current_color.append(self.color_change2)
if self.rect.x < 0:
self.rect.x = 0
self.impulse(-self.dx*self.elasticity, self.dy)
self.current_color.append(self.color_change1)
if self.rect.y < 0:
self.rect.y = 0
self.impulse(-self.dy*self.elasticity, self.dx)
self.current_color.append(self.color_change2)
def draw(self):
"""Draw"""
draw.ellipse(self.display, self.color_default, self.rect)
if not self.check_collisions():
draw.ellipse(self.display, self.current_color, self.rect)
""""if self.rect.y + self.radius * 2 > self.display.get_size()[1]:
draw.ellipse(self.display, self.color_change1, self.rect)
if self.rect.x + self.radius * 2 > self.display.get_size()[0]:
draw.ellipse(self.display, self.color_change2, self.rect)
if self.rect.x < 0:
draw.ellipse(self.display, self.color_change1, self.rect)
if self.rect.y < 0:
draw.ellipse(self.display, self.color_change2, self.rect)"""
答案 0 :(得分:1)
你的.draw()
函数只是在不在显示范围之外时用不同的颜色绘制球。您想要的是在.check_collisions()
内,如果检测到碰撞,请在self
上设置属性,以确定要使用的颜色。
然后,在.draw()
,而不是draw.ellipse(self.display, self.color_default, self.rect)
,您将拥有draw.ellipse(self.display, self.current_colour, self.rect)
或类似内容。