Форум сайта python.su
0
Много часов пытаюсь исправить непонятный баг: для анимации ходьбы персонажа прописал 4 одинаковых кода, меняя только переменные чтобы в разные стороны была разная анимация, а так код одинаковый, но работает только когда персонаж идёт от меня (вверх)
вот видео: https://vk.com/video211748306_456239496
import pygame from pygame import * import random import os # настройка папки ассетов game_folder = os.path.dirname(__file__) img_folder = os.path.join(game_folder, 'gkjh') ШИР = 1300 ВЫС = 700 FPS = 30 left = False right = False komne = False otmena = False ls = 2 speed = 10 x = 700 y = 230 # Задаем цвета WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) # Создаем игру и окно pygame.init() pygame.mixer.init() screen = pygame.display.set_mode((1300, 700)) pygame.display.set_caption("My Game") clock = pygame.time.Clock() player_img = pygame.image.load(os.path.join(img_folder, 'pstat.png')).convert() polfull_img = pygame.image.load(os.path.join(img_folder, 'polfull.png')).convert() walkleft1 = pygame.image.load(os.path.join(img_folder, 'idetvl1.png')).convert() walkleft2 = pygame.image.load(os.path.join(img_folder, 'idetvl2.png')).convert() walkright1 = pygame.image.load(os.path.join(img_folder, 'idetvp1.png')).convert() walkright2 = pygame.image.load(os.path.join(img_folder, 'idetvp2.png')).convert() walkotmena1 = pygame.image.load(os.path.join(img_folder, 'zid1.png')).convert() walkotmena2 = pygame.image.load(os.path.join(img_folder, 'zid2.png')).convert() walkkomne1 = pygame.image.load(os.path.join(img_folder, 'pid1.png')).convert() walkkomne2 = pygame.image.load(os.path.join(img_folder, 'pid2.png')).convert() statotmena = pygame.image.load(os.path.join(img_folder, 'zstat.png')).convert() statkomne = player_img statleft = pygame.image.load(os.path.join(img_folder, 'svlst.png')).convert() statright = pygame.image.load(os.path.join(img_folder, 'svpst.png')).convert() all_sprites = pygame.sprite.Group() #pol1 = pygame.image.load(os.path.join(img_folder, 'pol1.png')).convert() pskin = statkomne #class Bl1(pygame.sprite.Sprite): # def __init__(self): # pygame.sprite.Sprite.__init__(self) # self.image = bl1_img # self.rect = self.image.get_rect() # self.rect.center = (WIDTH / 2, HEIGHT / 2) class Polfull(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__init__(self) self.image = polfull_img # self.image.set_colorkey(WHITE) self.rect = self.image.get_rect() self.rect.center = (ШИР / 2, ВЫС / 2) def update(self): self.rect.x += 0 if self.rect.left > ШИР: self.rect.right = 0 class Player(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__init__(self) self.image = pskin self.image.set_colorkey(WHITE) self.rect = self.image.get_rect() self.rect.center = (ШИР / 2, ВЫС / 2) def update(self): #self.rect.x += 5 # if self.rect.left > ШИР: # self.rect.right = 0 #screen.blit(polfull_img, (70, 0)) print('a', left, 'w', otmena, 'd', right, 's', komne, animCount, oparle, oparko, oparri, oparot, opar) if left: #pskin = statleft[animCount - 1], (x, y) self.rect.x -= 10 #self.image.blit(statleft, (x, y)) if right: #pskin = statright[animCount - 1], (x, y) self.rect.x += 10 if komne: #pskin = statkomne[animCount - 1], (x, y) self.rect.y += 10 if otmena: #pskin = statotmena[animCount - 1], (x, y) self.rect.y -= 10 else: if lsf == 1: player.image = statleft player.image.set_colorkey(WHITE) elif lsf == 2: player.image = statkomne player.image.set_colorkey(WHITE) elif lsf == 3: player.image = statright player.image.set_colorkey(WHITE) elif lsf == 4: player.image = statotmena player.image.set_colorkey(WHITE) pygame.display.update() ####class pol1(pygame.sprite.Sprite): #### def __init__(self): #### pygame.sprite.Sprite.__init__(self) #### self.image = pol1_img #### self.rect = self.image.get_rect() #### self.rect.center = (WIDTH / 2, HEIGHT / 2) walkpod = 0 opar = 0 def oparish(): global walkpod global opar opar += 4 if opar >= 20: opar = 0 if ls == 1: walkpod1 = walkleft1 walkpod2 = walkleft2 elif ls == 2: walkpod1 = walkkomne1 walkpod2 = walkkomne2 elif ls == 3: walkpod1 = walkright1 walkpod2 = walkright2 elif ls == 4: walkpod1 = walkotmena1 walkpod2 = walkotmena2 if opar // 10 == 0: player.image = walkpod1 elif opar // 10 == 1: player.image = walkpod2 player.image.set_colorkey(WHITE) polfull = Polfull() player = Player() all_sprites.add(polfull, player) #def drawWindow(): #global animCount #screen.blit(polfull_img, (70, 0)) #if left: #player.rect.x -= 5 #elif right: #player.rect.x += 5 # elif komne: #player.rect.y -= 5 # elif otmena: # player.rect.y += 5 # pygame.display.update() oparot = 0 oparko = 0 oparri = 0 oparle = 0 # Цикл игры running = True while running: # Держим цикл на правильной скорости clock.tick(FPS) # Ввод процесса (события) for event in pygame.event.get(): # check for closing window if event.type == pygame.QUIT: running = False keys = pygame.key.get_pressed() if oparko >= 20: oparko = 0 if oparri >= 20: oparri = 0 if oparle >= 20: oparle = 0 if oparot >= 20: oparot = 0 if keys[pygame.K_a] and not keys[pygame.K_s] and not keys[pygame.K_w] and not keys[pygame.K_d]: right = False otmena = False komne = False left = True ls = 1 lsf = 1 oparish() #oparle += 4 #if oparle // 10 == 0: #player.image = walkleft1 #elif oparle // 10 == 1: #player.image = walkleft2 #player.image.blit(walkleft[animCount - 1], (x, y)) elif keys[pygame.K_s] and not keys[pygame.K_d] and not keys[pygame.K_w] and not keys[pygame.K_a]: right = False otmena = False komne = True left = False ls = 2 lsf = 2 oparish() #oparko += 4 #if oparko // 10 == 0: #player.image = walkkomne1 #elif oparko // 10 == 1: #player.image = walkkomne2 elif keys[pygame.K_w] and not keys[pygame.K_s] and not keys[pygame.K_a] and not keys[pygame.K_d]: right = False otmena = True komne = False left = False ls = 4 lsf = 4 oparish() #oparot += 4 #if oparot // 10 == 0: #player.image = walkotmena1 #elif oparot // 10 == 1: #player.image = walkotmena2 #player.image.set_colorkey(WHITE) elif keys[pygame.K_d] and not keys[pygame.K_s] and not keys[pygame.K_w] and not keys[pygame.K_a]: left = False right = True otmena = False komne = False ls = 3 lsf = 3 oparish() #oparri += 4 #if oparri // 10 == 0: #player.image = walkright1 #elif oparri // 10 == 1: #player.image = walkright2 #player.image.set_colorkey(WHITE) elif keys[pygame.K_a] and keys[pygame.K_s] and not keys[pygame.K_w] and not keys[pygame.K_d]: left = True komne = True right = False otmena = False ls = 2 lsf = 2 elif keys[pygame.K_d] and keys[pygame.K_s] and not keys[pygame.K_w] and not keys[pygame.K_a]: right = True komne = True otmena = False left = False ls = 2 lsf = 2 elif keys[pygame.K_w] and keys[pygame.K_a] and not keys[pygame.K_d] and not keys[pygame.K_s]: left = True otmena = True right = False komne = False ls = 4 lsf = 4 elif keys[pygame.K_w] and keys[pygame.K_d] and not keys[pygame.K_a] and not keys[pygame.K_s]: right = True otmena = True komne = False left = False ls = 4 lsf = 4 elif keys[pygame.K_a] and keys[pygame.K_w] and keys[pygame.K_d] and not keys[pygame.K_s]: right = False otmena = True komne = False left = False ls = 4 lsf = 4 elif keys[pygame.K_s] and keys[pygame.K_d] and keys[pygame.K_w] and not keys[pygame.K_a]: right = True otmena = False komne = False left = False ls = 4 lsf = 4 elif keys[pygame.K_a] and keys[pygame.K_s] and keys[pygame.K_d] and not keys[pygame.K_w]: right = False otmena = False komne = True left = False ls = 2 lsf = 2 elif keys[pygame.K_w] and keys[pygame.K_a] and keys[pygame.K_s] and not keys[pygame.K_d]: right = False otmena = False komne = False left = True ls = 2 lsf = 2 else: left = False right = False komne = False otmena = False animCount = 0 lsf = ls # Обновление all_sprites.update() # Рендеринг # screen.fill(BLACK) all_sprites.draw(screen) all_sprites.draw(player.image) # После отрисовки всего, переворачиваем экран pygame.display.flip() #drawWindow() pygame.quit()
Отредактировано GDEgor2330 (Апрель 29, 2022 22:04:06)
Офлайн
0
Блин, пробелы не сохраняются
вот файл тогда
Прикреплённый файлы:
untitled3-1.py (10,5 KБ)
Офлайн
124
[code python]код вставлять между этими тегами[/code]
Офлайн
0
xam1816отредактировал
Офлайн