Reputation: 911
I am making a Guardian game where you fire bullets at enemies and evolve. When you press the r key, your image changes. The image I want the player the turn into when the player hits the r key is an orange triangle with a red outline. The image that the player is before you press the r key is a yellow triangle with an orange outline. However, when I pressed the r key, instead of turning into an orange triangle with a red outline, you turn into a large yellow triangle with an orange outline.
Here is the player class (named Polygon):
class Polygon(Sprite):
"""Is it going to be a triangle, a square, a pentagon, or something else?"""
def __init__(self, sides, window, x, y, size=None):
self.size = size
self.sides = sides
self.window = window
self.x = x
self.y = y
self.check_image()
Sprite.__init__(self, self.window, self.x, self.y, self.image, self.size)
def check_for_movement(self, move_amount=1):
"""Let's get moving!"""
key = pygame.key.get_pressed()
if key[K_w]:
self.y -= move_amount
if key[K_s]:
self.y += move_amount
if key[K_a]:
self.x -= move_amount
if key[K_d]:
self.x += move_amount
def check_for_power_up(self):
key = pygame.key.get_pressed()
if key[K_r]:
self.upgrade()
def evolve(self):
self.sides += 1
self.check_image()
def check_image(self):
if self.sides == 3: # Is it a triangle?
self.image = "triangle.png"
elif self.sides == 4: # A square?
self.image = "square.png"
elif self.sides == 5: # Or even a pentagon?
self.image = "pentagon.png"
elif self.sides == 6: # A hexagon?
self.image = "hexagon.png"
@staticmethod
def check_bullet_type():
"""Who would win? A bullet or a bomb?"""
global bullet_type, bullet_type_word
key = pygame.key.get_pressed()
if key[K_2]:
bullet_type = 2
bullet_type_word = "Bomb"
if key[K_1]:
bullet_type = 1
bullet_type_word = "Bullet"
def upgrade(self):
if self.sides == 3:
self.image = "triangle_plus.png"
self.check_image()
And here is my loop:
while running:
"""Event loop"""
for event in pygame.event.get():
if event.type == QUIT:
running = False
"""These lines creates a bullet object when the player clicks the screen"""
if event.type == MOUSEBUTTONDOWN:
if bullet_type == 1:
if player.sides == 6:
bullet_group.add(Bullet(6, screen, player.x + 3, player.y + 5, size=(270, 140)))
elif player.sides == 5:
bullet_group.add(Bullet(5, screen, player.x + 3, player.y + 5, size=(270, 140)))
elif player.sides == 4:
bullet_group.add(Bullet(4, screen, player.x + 185, player.y + 30, size=(320, 190)))
elif player.sides == 3:
bullet_group.add(Bullet(3, screen, player.x + 4, player.y - 15, size=(170, 100)))
elif bombs > 0 and bullet_type == 2:
"""This is for bomb firing detection"""
if player.sides == 3:
bullet_group.add(Bomb(screen, player.x + 3, player.y + 2, size=(370, 240)))
elif player.sides == 4:
bullet_group.add(Bomb(screen, player.x + 185, player.y + 30, size=(370, 240)))
elif player.sides == 5:
bullet_group.add(Bomb(screen, player.x + 5, player.y - 15, size=(370, 240)))
elif player.sides == 6:
bullet_group.add(Bomb(screen, player.x - 10, player.y - 15, size=(370, 240)))
"""This will teach the player not to spam bombs!"""
bombs -= 1
"""This creates a enemy every 2 seconds"""
if event.type == enemy_event:
if player.sides == 3:
enemy_group.add(Enemy(3, screen, random.randint(0, 900), 50, size=(230, 160)))
elif player.sides == 4:
enemy_given_score = 20
enemy_group.add(Enemy(4, screen, random.randint(0, 900), 50, size=(550, 350)))
elif player.sides == 5:
enemy_given_score = 30
enemy_group.add(Enemy(5, screen, random.randint(0, 900), 50, size=(390, 215)))
elif player.sides == 6:
enemy_given_score = 40
enemy_group.add(Enemy(6, screen, random.randint(0, 900), 50, size=(390, 215)))
screen.fill(THECOLORS["skyblue"])
"""Checks the bullet type"""
player.check_bullet_type()
"""Updates the player position and drawing the player"""
player.update()
player.check_for_movement(9)
"""Allows the bullets to appear and fire towards the top of the screen"""
bullet_group.draw(screen)
bullet_group.update()
"""
Updates the enemy sprite(s) and allows the enemy to fly
toward the bottem of the screen
"""
enemy_group.draw(screen)
enemy_group.update()
"""Draws the CollectableBombs"""
bomb_group.draw(screen)
bomb_group.update()
"""Updates and blits the score text"""
score_text_surface = font.render("Score: {:,}".format(score), True, (0, 0, 0))
screen.blit(score_text_surface, (650, 10))
"""Updates and blits the lives text"""
lives_text_surface = font.render("Lives: {:,}".format(lives), True, (0, 0, 0))
screen.blit(lives_text_surface, (650, 60))
"""Updates and blits the bombs text"""
bombs_text_surface = font.render("Bombs: {:,}".format(bombs), True, (0, 0, 0))
screen.blit(bombs_text_surface, (650, 110))
"""Updates and blits the bullet type text"""
bullet_type_text_surface = font.render("Bullet Type: {0}".format(bullet_type_word),
True, (0, 0, 0))
if bullet_type == 1:
screen.blit(bullet_type_text_surface, (550, 160))
elif bullet_type == 2:
screen.blit(bullet_type_text_surface, (555, 160))
"""This blits the game over image if it's game over"""
if lives <= 0:
game_over_bg = Image(screen, 390, 300, "game_over.png")
screen.blit(game_over_bg.image, game_over_bg.rect)
"""
If a bullet and a enemy collide, delete them both and increase score by 1.
If you are lucky enough, you may even get a bomb!
"""
for i in enemy_group:
if pygame.sprite.spritecollide(i, bullet_group, True):
score += enemy_given_score
if random.choice([0, 1]) == 1:
bombs += 1
i.kill()
"""POWER UPPPPPPPPPP!"""
player.check_for_power_up()
"""If a enemy and the player collide, decrease lives by 1"""
if pygame.sprite.spritecollide(player, enemy_group, True):
lives -= 1
"""Evolves the player into a square if score is in the 100's"""
if keep_checking_for_square and 100 <= score < 201:
player.evolve()
keep_checking_for_square = False
"""Evolves the player into a pentagon if score is in the 200's"""
if keep_checking_for_pentagon and 200 <= score < 301:
player.evolve()
player.__init__(5, screen, player.x, player.y, size=(385, 230)) # Yeah, this line is for changing the size of the player
keep_checking_for_pentagon = False
"""Evolves the player into a hexagon if score is from 300 to 410"""
if keep_checking_for_hexagon and 300 <= score < 411:
player.evolve()
player.__init__(6, screen, player.x, player.y, size=(475, 280)) # This line also changes the size of the player
keep_checking_for_hexagon = False
"""If score is 1000 or more, victory!"""
if score >= 1000:
cake = Sprite(screen, SCREEN_X // 2 + 10, SCREEN_Y // 2 + 30, "cake.png")
screen.fill(Color(71, 181, 245))
screen.blit(cake.image, cake.rect)
win_text_surface_upper = font.render("CONGRATULATIONS!", True, (0, 0, 0)) # WINNER WINNER
win_text_surface_lower = font.render("YOU SAVED THE INFINITE SIDED POLYGON!", True, (0, 0, 0)) # CHICKEN DINNER
screen.blit(win_text_surface_upper, (SCREEN_X // 2 - 155, SCREEN_Y // 2 - 165))
screen.blit(win_text_surface_lower, (SCREEN_X // 2 - 275, SCREEN_Y // 2 + 75))
pygame.display.flip()
clock.tick(40)
Upvotes: 0
Views: 60
Reputation: 3599
In your Sprite.upgrade()
function, you first assign self.image = "triangle_plus.png"
but immediately afterwards call check_image()
which then overwrites self.image
, undoing your previous change. You will probably want to return
once you updated the image.
Upvotes: 2