P
P
pythono2017-03-28 08:53:45
Python
pythono, 2017-03-28 08:53:45

Why do sprite coordinates not change in pygame?

I set the sprite coordinates to 100,300 and it is displayed in the upper left corner of 0,0
what is wrong with my code?

import sys, os
import pygame
from pygame.locals import *
     
def init_window():
    pygame.init()
    window = pygame.display.set_mode((550, 480))
    pygame.display.set_caption('ПУТИН')
     
def input(events):
    for event in events:
        if (event.type == QUIT) or (event.type == KEYDOWN and event.key == K_ESCAPE):
            sys.exit(0)
        else:
            pass
     

def load_image(name, colorkey=None):
    fullname = os.path.join('data', name)
    try:
        image = pygame.image.load(fullname)
    except:
        pass
    image = image.convert()
    if colorkey is not None:
        if colorkey is -1:
            colorkey = image.get_at((0,0))
        image.set_colorkey(colorkey, RLEACCEL)
    return image, image.get_rect()
 
def draw_background():
    screen = pygame.display.get_surface() # Получаем поверхность, на которой будем рисовать
    background = pygame.Surface(screen.get_size()) # и ее размер
    background = background.convert()
    background.fill((0, 0, 0)) # заполняем цветом
    screen.blit(background, (0, 0)) # рисуем заполненный одним цветом бэкграунд
    back, back_rect = load_image("grass.jpg") # или загружаем картинку с травой
    screen.blit(back, (0, 0)) # и рисуем ее
    pygame.display.flip() # переключаем буфер экрана
    return back

class Animal(pygame.sprite.Sprite):
    def __init__(self, img, cX, cY):
        pygame.sprite.Sprite.__init__(self)
        self.image, self.rect = load_image(img, -1)
        screen = pygame.display.get_surface()
        self.area = screen.get_rect()
        self.cX = cX
        self.cY = cY
        self.coord = (cX, cY)
        print ("Animal spawned at", self.coord)
 
class Elephant(Animal):
    def __init__(self, cX, cY):
        Animal.__init__(self, "1.png", cX, cY)

def action(bk):
    creatures_list = [] # Список со всем животными. Пригодится, если будем добавлять новых
    screen = pygame.display.get_surface()
    elephant = Elephant(100,300) # Помещаем слона по координатам х=10, у=10
    creatures_list.append(elephant)
    animals = pygame.sprite.RenderPlain(creatures_list)
    while 1:
        input(pygame.event.get())
        screen.blit(bk, (0, 0))
        animals.update() # Стандартный метод проверки, вдруг что-то изменилось. Пригодится для описания движения
        animals.draw(screen)
        pygame.display.flip()
     
def main():
    init_window()
    bk = draw_background()
    action(bk)

    
     
if __name__ == '__main__': main()

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