Carlos Azuaje
Carlos Azuaje

Reputation: 119

Pygame Ellipse change color to collide

I have a class that ball bounces when it hits the limits of the screen

I am Attempted programming ballchange my color when the collision with the limits, but this does not change color, stays with color_default values and not change, I'm missing?

Here is the code:

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)

With this code, I tried to follow the advice given to me gives me TypeError:      draw.ellipse (self.display, self.current_color, self.rect) TypeError: Color invalid argument

    # 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)"""

Upvotes: 0

Views: 1014

Answers (1)

Adam Barnes
Adam Barnes

Reputation: 3203

Your .draw() function is only drawing the ball with a different colour while it's outside of the bounds of the display, which is never. What you instead want, is within your .check_collisions(), if a collision is detected, set a property on self determining which colour to use.

Then, in .draw(), instead of draw.ellipse(self.display, self.color_default, self.rect), you'd have draw.ellipse(self.display, self.current_colour, self.rect), or something similar.

Upvotes: 1

Related Questions