1

Through I have loaded the image and used draw method, the sprite images still do not appear in the window. Th aim of the final code is a sort of tag game where there is an ai opponent who chases the player and when they collide, the roles switch. I tried this project to improve my pygame skills as i am new to it. Here is my unfinished code:

import pygame, sys, os, random

class Block(pygame.sprite.Sprite):
    def __init__(self, path, x_pos, y_pos, player_group):
        super().__init__()
        self.image = pygame.image.load(path)
        self.rect = self.image.get_rect(center = (x_pos, y_pos))
        self.status = 1
        self.time = 0
        self.active = True
        self.player_group = player_group
    def screen_restraints(self):
        if self.rect.top >= screen_height:
            self.rect.top = 0
        if self.rect.bottom <= 0:
            self.rect.bottom = screen_height
        if self.rect.right >= screen_width:
            self.rect.right = 0
        if self.rect.left <= 0:
            self.rect.left = screen_width
    def TAGGED(self):
        current_time = pygame.time.get_ticks()
        if self.status < 0 and current_time - self.time <= 2100:
            tagged_display = game_font.render("TAGGED", True, light_grey)
            tagged_display_rect = tagged_display.get_rect(center = (screen_width/2, 30))
            pygame.draw.rect(screen, light_grey, tagged_display_rect)
            screen.blit(tagged_display, tagged_display_rect)
        if current_time - self.time > 2100:
            self.acive = True
    def collision(self):
        if pygame.sprite.spritecollide(self, self.player_group, False) and self.active:
            self.time = pygame.time.get_ticks()
            self.status *= -1
            self.active = False
    
class Player(Block):
    def __init__(self, path, x_pos, y_pos, speed_x, speed_y, player_group):
        super().__init__(path, x_pos, y_pos, player_group)
        self.speed_x = speed_x
        self.speed_y = speed_y
        self.motion_y = 0
        self.motion_x = 0
    def update(self):
        self.rect.y += self.motion_y
        self.rect.x += self.motion_x
        self.screen_restraints

class Tagged(Block):
    def __init__(self, path, x_pos, y_pos, speed_x, speed_y, player_group):
        super().__init__(path, x_pos, y_pos, player_group)
        self.speed_x = speed_x
        self.speed_y = speed_y
        self.motion_x = 0
        self.motion_y = 0
    def tagged_motion(self):
        if self.rect.bottom >= self.player_group.sprite.rect.y:
            self.motion_y += self.speed_y
        if self.rect.top <= self.player_group.sprite.rect.y:
            self.motion_y -= self.speed_y
        if self.rect.left >= self.player_group.sprite.rect.x:
            self.motion_x -= self.speed_x
        if self.rect.right <= self.player_group.sprite.rect.x:
            self.motion_x += self.speed_x
    def update(self):
        self.rect.y += self.motion_y
        self.rect.x += self.motion_x
        self.screen_restraints

os.chdir(r"C:\Users\tssac\OneDrive\Desktop\Sachin\CODING\Tag")

pygame.init()
clock = pygame.time.Clock()

screen_width = 1280
screen_height = 960
screen = pygame.display.set_mode((screen_width, screen_height))

player_group = pygame.sprite.Group()
player = Player("Ball1.png", screen_width/4, screen_height/2, 5, 5, player_group)
tagged = Tagged("Ball.png", 3*screen_width/4, screen_height/2, 5, 5, player_group)
player_group.add(player)
player_group.add(tagged)

bg_color = pygame.Color('grey12')
light_grey = (200, 200, 200)
game_font = pygame.font.Font("freesansbold.ttf", 32)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_DOWN:
                player.motion_y += player.speed_y
            if event.key == pygame.K_UP:
                player.motion_y -= player.speed_y
            if event.key == pygame.K_LEFT:
                player.motion_x -= player.speed_x
            if event.key == pygame.K_DOWN:
                player.motion_x += player.speed_x
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_DOWN:
                player.motion_y -= player.speed_y
            if event.key == pygame.K_UP:
                player.motion_y += player.speed_y
            if event.key == pygame.K_LEFT:
                player.motion_x += player.speed_x
            if event.key == pygame.K_DOWN:
                player.motion_x -= player.speed_x
    player_group.update()
    player_group.draw(screen)
    screen.fill(bg_color)

    pygame.display.flip()
    clock.tick(60)

I tried using the blit function but i dont think it applies to sprite groups as, when i tried, it gave an error.

Frezza
  • 11
  • 1

1 Answers1

2

You must draw the background before you draw the sprites, not afterwards. Drawing the background covers everything that was drawn before.

while True:
    # [...]

    # draw background
    screen.fill(bg_color)
    # draw the objects in the scene onto the background
    player_group.draw(screen)
    # update the display
    pygame.display.flip()

    clock.tick(60)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174