Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

These two files allow me to draw butterflies and caterpillars on a simple background scene by pressing b and c. I am trying to implement

These two files allow me to draw butterflies and caterpillars on a simple background scene by pressing "b" and "c". I am trying to implement a method(change_color(self)) in both Caterpillar class and Butterfly class that will change the color of every already existed butterflies and caterpillars by pressing "s". 
I am trying to call change_color(self) from critters.py and now I am stuck. How should I do this? ===================================== #This file is called critters.py import pygame import mycritters # Define some colors white = ( 255, 255, 255) green = ( 0, 255, 0) lightblue = ( 0, 100, 255) brown = ( 125, 100, 100) pink = ( 100, 0, 0) # Initialize pygame pygame.init() # Set the height and width of the screen size=[1000,1000] screen=pygame.display.set_mode(size) # Set title of screen pygame.display.set_caption("Critters") # Function to draw background scene def draw_background(): pygame.draw.rect(screen,brown,[0, 700, 1000, 300]) pygame.draw.rect(screen,green,[0, 600, 1000, 100]) pygame.draw.rect(screen,lightblue,[0, 0, 1000, 600]) pygame.draw.ellipse(screen,white,[50, 80, 100, 60]) pygame.draw.ellipse(screen,white,[120, 60, 180, 80]) pygame.draw.ellipse(screen,white,[700, 80, 150, 60]) # Loop until the user clicks the close button. done=False  # Used to manage how fast the screen updates clock=pygame.time.Clock() critterlist = [] temp = [] ###################################### # -------- Main Program Loop ----------- while done==False: for event in pygame.event.get(): # User did something  if event.type == pygame.QUIT: # If user clicked close  done=True # Flag that we are done so we exit this loop  if event.type == pygame.KEYDOWN: # If user wants to perform an action  if event.key == pygame.K_c: newcaterpillar = mycritters.Caterpillar() critterlist.append(newcaterpillar) if event.key == pygame.K_b: newbutter = mycritters.Butterfly() critterlist.append(newbutter) if event.key == pygame.K_s: mycritters.Butterfly.change_color() for critter in critterlist: temp.append(critter) critterlist = temp # Draw the background scene  draw_background() # Draw the critters   for critter in critterlist: critter.draw_critter(screen) # Limit to 20 frames per second  clock.tick(50) # Go ahead and update the screen with what we've drawn.  pygame.display.flip() # If you forget this line, the program will 'hang' on exit. pygame.quit () 
 ==================================== 
#This file is called mycritters.py
import pygame import random # Define some colors black = ( 0, 0, 0) white = ( 255, 255, 255) green = ( 0, 255, 0) blue = ( 0, 0, 255) red = ( 255, 0, 0) yellow = ( 255, 255, 0) purple = ( 255, 0, 255) brown = ( 125, 100, 100) pink = ( 255, 100, 100) class Caterpillar: def __init__(self): x = random.randrange(50,950) y = random.randrange(600, 950) z = random.randrange(0,4) c = 0 self.xcoord = x self.ycoord = y self.zcoord = z self.colorIndex = c # color for Caterpillar : [0 head, 1 antenna, ,2 eyes, 3 body, 4 legs]  self.colorC = [[black,red,yellow,green,purple],[pink,yellow,red,purple,brown],[white,blue,red,yellow,purple] ] def draw_critter(self, screen): x = self.xcoord y = self.ycoord z = self.zcoord c = self.colorIndex colorCate = self.colorC #head  pygame.draw.ellipse(screen,colorCate[c][0], [x, y, 40, 45]) #eyes  pygame.draw.ellipse(screen,colorCate[c][2], [x+6, y+10, 10, 15]) pygame.draw.ellipse(screen,colorCate[c][2], [x+24, y+10, 10, 15]) for g in range (z+1): s = (g+1)*40 # body  pygame.draw.ellipse(screen,colorCate[c][3],[x,y+s,40,45]) # legs  pygame.draw.line(screen,colorCate[c][4], (x+10, y+s+10), (x-1, y+s+6), 3) pygame.draw.line(screen,colorCate[c][4], (x+26, y+s+10), (x+36, y+s+6), 3) pygame.draw.line(screen,colorCate[c][4], (x+10, y+s+35), (x-1, y+s+39), 3) pygame.draw.line(screen,colorCate[c][4], (x+26, y+s+35), (x+36, y+s+39), 3) # antennas  pygame.draw.line(screen,colorCate[c][1], (x+11, y+1), (x+9, y-10), 3) pygame.draw.line(screen,colorCate[c][1], (x+25, y+1), (x+26, y-10), 3) def change_color(self): c = self.colorIndex if c == 2: c = 0 else: c += 1 return c class Butterfly: def __init__(self): x = random.randrange(50,950) y = random.randrange(50, 400) z = random.randrange(0,4) c = 0 self.xcoord = x self.ycoord = y self.colorIndex = c # color for Butterfly : [0 head, 1 antenna, ,2 eyes, 3 body, 4 Wings]  self.colorB = [[blue,black,yellow,green,red],[pink,yellow,red,purple,green],[white,blue,red,yellow,purple] ] def draw_critter(self,screen): x = self.xcoord y = self.ycoord c = self.colorIndex colorButter = self.colorB #left wing  pygame.draw.ellipse(screen,colorButter[c][4],[x-80,y-30,100,200]) #right wing  pygame.draw.ellipse(screen,colorButter[c][4],[x+20,y-30,100,200]) #body  pygame.draw.ellipse(screen,colorButter[c][3],[x,y+40,40,45]) pygame.draw.ellipse(screen,colorButter[c][3],[x,y+80,40,45]) pygame.draw.ellipse(screen,colorButter[c][3],[x,y+120,40,45]) #head  pygame.draw.ellipse(screen,colorButter[c][0], [x, y, 40, 45]) #eyes  pygame.draw.ellipse(screen,colorButter[c][2], [x+6, y+10, 10, 15]) pygame.draw.ellipse(screen,colorButter[c][2], [x+24, y+10, 10, 15]) #antennas  pygame.draw.line(screen,colorButter[c][1], (x+11, y+1), (x+7, y-30), 3) pygame.draw.line(screen,colorButter[c][1], (x+25, y+1), (x+29, y-30),3) def change_color(self): c = self.colorIndex if c == 2: c = 0 else: c += 1 return c 

========================================

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

=+ 9. What is inflation and what causes it?

Answered: 1 week ago