How can I change the location of an image displayed on python using pygame
Problem Description:
When I run the code, the image displays near the top left. How do I make the image display near the top right?
I have got a question. I am creating space invaders. Space invaders spawn in the top left… travel right… drop… travel left.. rinse repeat.
My version… I want the aliens to spawn on their sides. Spawn on the top right… travel down… drop left…. travel up… drop left… rinse repeat…
I have EVERYTHING done… but the spawn point. My aliens dont want to spawn in the top right… they keep spawning in the top left… the code runs great… the fleet is moving properly… but the aliens do not spawn where I want them to spawn… anyone mind helping me out?
Below is the entire file dedicated toward the aliens. What can I adjust to fulfill my needs?
import pygame
from pygame.sprite import Sprite
class Alien(Sprite):
"""A class to represent a single alien in the fleet."""
def __init__(self, ai_game):
"""Initialize the alien and set its starting position."""
super().__init__()
self.screen = ai_game.screen
self.sideways_settings = ai_game.sideways_settings
# self.screen_rect = ai_game.screen.get_rect()
# Load the alien image and set its rect attribute.
self.image = pygame.image.load('images/alien.bmp')
self.rect = self.image.get_rect()
# Start each new alien near the top right of the screen.
self.rect.x = self.rect.height
self.rect.y = self.rect.width
# Store the alien's exact vertical position.
self.y = float(self.rect.y)
self.x = float(self.rect.x)
def check_edges(self):
"""Return True if alien is at edge of screen."""
screen_rect = self.screen.get_rect()
if self.rect.bottom >= screen_rect.bottom or self.rect.top <= 0:
return True
def update(self):
"""Move the alien up or down."""
self.y += (self.sideways_settings.alien_speed *
self.sideways_settings.fleet_direction)
self.rect.y = self.y
# def blitme(self):
# """Draw the ship at its current location."""
# self.screen.blit(self.image, self.rect)
Solution – 1
The top right coordinate of the screen is (screen_rect.width, 0)
not (0, 0)
:
# Start each new alien near the top right of the screen.
self.rect.x = screen_rect.width - self.rect.width
self.rect.y = 0