L
L
likel0vsky2021-08-22 20:16:38
pygame
likel0vsky, 2021-08-22 20:16:38

How to remove an object from the screen in Pygame?

I am writing a game on PyGame, I ran into such a problem as deleting a sprite - nowhere in the documentation, on the forums there is no information on how to delete a sprite
There is a .kill () method, but personally it does not work for me - moving an object stops working, etc. etc, but the object itself is not removed from the screen. How can I do that?
The code:

import random
se_tostring = str
import time

import pygame, sys
from pygame.locals import *
pygame.init()
pygame.font.init()

pygame_screen = pygame.display.set_mode((0,0),pygame.FULLSCREEN)
pygame_all_sprites = pygame.sprite.Group()
  #SE FUNCS

def readFile(path):
    return open(path, 'r').read()
def split(str,sep):
    return str.split(sep)

import datetime
now = datetime.datetime.now

pygameObject_bg = pygame.sprite.Sprite(pygame_all_sprites)
try:
   pygameObject_bg.image = pygame.image.load("bg.png")
   pygameObject_bg.rect = pygameObject_bg.image.get_rect()
except Exception:
   print("Файл не найден")
pygameObject_bg_pos = (-1, 0)

pygameObject_bg.rect.x = pygameObject_bg_pos[0]
pygameObject_bg.rect.y = pygameObject_bg_pos[1]
pygameObject_zombie = pygame.sprite.Sprite(pygame_all_sprites)
try:
   pygameObject_zombie.image = pygame.image.load("zomb.png")
   pygameObject_zombie.rect = pygameObject_zombie.image.get_rect()
except Exception:
   print("Файл не найден")
pygameObject_zombie_pos = (50, -10)

pygameObject_zombie.rect.x = pygameObject_zombie_pos[0]
pygameObject_zombie.rect.y = pygameObject_zombie_pos[1]
pygameObjectText_text1 = pygame.font.SysFont( 'serif', 48).render("text", True, (0,0,0) )
pygame_screen.blit( pygameObjectText_text1 , (0,0) )
while True:
      pygame.display.flip()
      pygame.display.update()
      pygame_all_sprites.draw(pygame_screen)	
      for pygame_event in pygame.event.get():
            if pygame_event.type == pygame.KEYDOWN and pygame_event.key == pygame.K_o:
                  pygameObject_zombie_pos = (pygameObject_zombie_pos[0]+5, 0)

                  pygameObject_zombie.rect.x = pygameObject_zombie_pos[0]
                  pygameObject_zombie.rect.y = pygameObject_zombie_pos[1]
            
            if pygame_event.type == pygame.KEYDOWN and pygame_event.key == pygame.K_o:
                  pygameObject_zombie.kill()

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question