Reputation: 35
My enemy sprites are cut in half and one row is lower than the other.
pygame.init()
done = False
display_width = 800
display_height = 600
BASE_PATH = abspath(dirname(__file__))
FONT_PATH = BASE_PATH + '/fonts/'
IMAGE_PATH = BASE_PATH + '/sprites/'
SOUND_PATH = BASE_PATH + '/sounds/'
SCORE_PATH = BASE_PATH + '/scores/'
#-----------------------------------------------------------------------------#
#Classes and Functions
#objects
all_sprites = pygame.sprite.Group()
################################################################################
#Enemy class
#Creates class image and positons and size, true or false statments for movement and updating positions
##############################################################################################
class Enemy(pygame.sprite.Sprite):
def __init__(self,x,y,direction,enemy_type):
pygame.sprite.Sprite.__init__(self)
self.EnemyType = enemy_type
self.Direction = direction
if enemy_type == 1:
enemy_image = pygame.image.load("sprites\\enemy1_1.png")
self.Speed = 1
self.Score = 5
if enemy_type == 2:
enemy_image = pygame.image.load("sprites\\enemy1_1.png")
self.Score = 15
self.Speed = 1
if enemy_type == 3:
enemy_image = pygame.image.load("sprites\\enemy1_1.png")
self.Score = 10
self.Speed = 1
if enemy_type == 4:
enemy_image = pygame.image.load("sprites\\enemy1_1.png")
self.Score = 20
self.Speed = 1
if enemy_type == 5:
enemy_image = pygame.image.load("sprites\\enemy1_1.png")
self.Score = 25
self.Speed = 1
self.image = pygame.Surface([26, 50])
self.image.set_colorkey(BLACK)
self.image.blit(enemy_image,(0,0))
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
def move_enemy(self):
if self.Direction == "right":
self.rect.x += self.Speed
if self.Direction == "left":
self.rect.x -= self.Speed
#draw image
def draw(self, screen):
screen.blit(self.image, self.rect)
allEnemies = pygame.sprite.Group()
a_enemies = Enemy(200,200,"right",1)
allEnemies.add(a_enemies)'''
#############################################################################################
#function: in game screen
#draws and calls classes and any functions that will be needed when playing the game
###########################################################################################
'''def screen_game():
global lives
global score
global gameDisplay
game_screen = True
#For X coords
spawnPositions = [90,180,270,360,450,540,630]
yCoord = 10
#creating enemies
for n in range(5):
for i in range(len(spawnPositions)):
xCoord = spawnPositions[i]
enemy_type = random.randint(1,5)
enemy = Enemy(xCoord, yCoord,"right", enemy_type)
allEnemies.add(enemy)
yCoord = yCoord + 50
#creating one player
player = Player(500,500, 'sprites\\ship.png')
#game loop
while game_screen:
#handling events
eventlist = pygame.event.get()
for event in eventlist:
#print(event)
if event.type == pygame.QUIT:
pygame.quit()
quit()
#handle arrow keys
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.handle_event(event)
if event.key == pygame.K_RIGHT:
player.handle_event(event)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
player.handle_event(event)
if event.key == pygame.K_RIGHT:
player.handle_event(event)
#draw game display
gameDisplay.fill(BLACK)
#draw player class
player.update()
player.draw(gameDisplay)
#update enemy positions
loop = 0
for enemy in (allEnemies.sprites()):
if enemy.rect.x < 0:
enemy.rect.y = enemy.rect.y + 10
enemy.Direction = "right"
if enemy.rect.x > 625:
enemy.rect.y = enemy.rect.y + 10
enemy.Direction = "left"
loop =+1
for enemy in (allEnemies.sprites()):
enemy.move_enemy()
#draw enemies
allEnemies.draw(gameDisplay)
#update display
pygame.display.update()
clock.tick(60)
I've shortened the code down on here to only include what I think are the relevant sections.
How can that be fixed?
Upvotes: 1
Views: 60
Reputation: 14916
I suspect that the Surface
created in the sprite __init()__
is not large enough to hold the image.
self.image = pygame.Surface([26, 50]) # <-- HERE
self.image.set_colorkey(BLACK)
self.image.blit(enemy_image,(0,0))
It's not really necessary to load the image, then blit it onto a surface, as the image is a surface:
if enemy_type == 5:
self.image = pygame.image.load("sprites\\enemy1_1.png")
self.Score = 25
self.Speed = 1
self.image.set_colorkey(BLACK)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
Just to simplify this a bit, all the Enemy stats and images could be placed into a list:
class Enemy(pygame.sprite.Sprite):
def __init__( self, x, y, direction, enemy_type_code ):
pygame.sprite.Sprite.__init__( self )
enemy_type = [ [ "sprites\\enemy1_1.png", 5, 1 ], # image, score, speed
[ "sprites\\enemy1_1.png", 15, 1 ],
[ "sprites\\enemy1_1.png", 10, 1 ],
[ "sprites\\enemy1_1.png", 20, 1 ],
[ "sprites\\enemy1_1.png", 25, 1 ] ]
self.EnemyType = enemy_type
self.Direction = direction
self.Speed = enemy_type[enemy_type_code][2]
self.Score = enemy_type[enemy_type_code][1]
self.image = pygame.image.load( enemy_type[enemy_type_code][0] )
self.image.set_colorkey(BLACK)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
Upvotes: 2