[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [pygame] Fill a Rect object with color?
Lemme borrow Russell's method to show what I think is your missing
piece: some way of getting a boom tower's rectangle onto the game's
playfield (the main PyGame surface). Run it, and left-click the mouse to
change the state of the boom tower.
import pygame
from pygame.locals import *
class BoomTower:
def __init__(self, bounds, normal_color, fire_color):
self.normal_image = pygame.Surface((bounds.w,bounds.h))
self.normal_image = self.normal_image.convert()
self.normal_image.fill(normal_color) ## Set the colour to the
default
self.fire_image = pygame.Surface((bounds.w,bounds.h))
self.fire_image = self.fire_image.convert()
self.fire_image.fill(fire_color)
self.current_image = self.normal_image
def get_current_image(self):
return self.current_image
def shoot(self):
## Your other code here
if self.current_image is self.normal_image:
self.current_image = self.fire_image
else:
self.current_image = self.normal_image
def draw(self, surface, pos):
rect = self.current_image.get_rect()
rect.center = pos
surface.blit(self.current_image, rect)
pygame.init()
pygame.display.set_mode((800,600))
s = pygame.display.get_surface()
r = pygame.Rect(50,50,100,100)
boom_tower = BoomTower(r, Color("white"), Color("red"))
while 1:
for event in pygame.event.get():
if event.type == MOUSEBUTTONDOWN:
boom_tower.shoot()
boom_tower.draw(s, r.center)
pygame.display.flip()
Gumm
Russell Cumins wrote:
I'm not 100% sure what you are wanting to change the colour so forgive
me if I'm going off on a tangent here. From what you have said thus
far I am assuming you want to change the colour of the BoomTower
object when it if firing.
What I would do is this...
class BoomTower:
def __init__(self,position,size,colour,fireColour):
self.colour = colour
self.fireColour = fireColour
self.rect = Rect(position,size)
self.image = Surface(size)
self.image.fill(colour) ## Set the colour to the default
def shoot(self,...):
...## Your other code here
self.image.fill(self.fireColour)
You probably need to add some more code elsewhere to change the colour
back for when the BoomTower object is not firing.