Уведомления

Группа в Telegram: @pythonsu

#1 Янв. 29, 2019 00:19:58

Craig_lal
Зарегистрирован: 2019-01-17
Сообщения: 3
Репутация: +  0  -
Профиль   Отправить e-mail  

Анимация движущегося объекта

Приветствую!
Хотел бы узнать, как реализовать анимацию такого объекта, как снаряд, который должен работать только после нажатия на кнопку. Сейчас после нажатия не происходит ничего, ошибок тоже нет.
Вот собственно код:

 import pygame
pygame.init()
name = "Best Game Ever"
logo = pygame.image.load('logo.png')
disp_width=1000 #Размер дисплея
disp_height=600
player_walk_left = [pygame.image.load('walk left/left1.png'), pygame.image.load('walk left/left2.png'), pygame.image.load('walk left/left3.png'), pygame.image.load('walk left/left4.png')]
player_walk_right = [pygame.image.load('walk right/right1.png'), pygame.image.load('walk right/right2.png'), pygame.image.load('walk right/right3.png'), pygame.image.load('walk right/right4.png')]
player_stand_right = [pygame.image.load('stand/right1.png'), pygame.image.load('stand/right2.png'), pygame.image.load('stand/right3.png'), pygame.image.load('stand/right4.png')]
player_stand_left = [pygame.image.load('stand/left1.png'), pygame.image.load('stand/left2.png'), pygame.image.load('stand/left3.png'), pygame.image.load('stand/left4.png')]
disp = pygame.display.set_mode((disp_width, disp_height))
pygame.display.set_caption(name)
pygame.display.set_icon(logo)
frame = pygame.time.Clock()
class player(object):
    def __init__(self, player_x, player_y, width, height):
        self.player_x = player_x
        self.player_y = player_y
        self.width = width
        self.height = height
        self.player_speed = 5
        self.jump = False
        self.left = False
        self.right = False
        self.anim_count = 0
        self.jump_dist = 7
        self.direct = 1
        self.stand_right = True
        self.stand_left = False
    def draw(self, disp):
        if self.anim_count + 1 >= 16:
            self.anim_count = 0
        if self.left:
            disp.blit(player_walk_left[self.anim_count//4], (self.player_x,self.player_y))
            self.anim_count += 1
        elif self.right:
            disp.blit(player_walk_right[self.anim_count//4], (self.player_x,self.player_y))
            self.anim_count +=1
        else:
            if self.direct == 1:
                disp.blit(player_stand_right[self.anim_count//4], (self.player_x,self.player_y))
                self.anim_count +=1
            else:
                disp.blit(player_stand_left[self.anim_count//4], (self.player_x,self.player_y))
                self.anim_count +=1
class fireball_one(object):
    fb_left = [pygame.image.load('fireball/fb1.png'), pygame.image.load('fireball/fb2.png'), pygame.image.load('fireball/fb3.png'), pygame.image.load('fireball/fb4.png'),
               pygame.image.load('fireball/fb5.png'), pygame.image.load('fireball/fb6.png'), pygame.image.load('fireball/fb7.png')]
    def __init__(self, player_x, player_y, facing):
        self.player_x = player_x
        self.player_y = player_y
        self.facing = facing
        self.fb_vel = 8 * facing
    def draw(self, disp):
        for fireball in fireballs:
            self.move()
            self.anim_count = 0
            if self.anim_count + 1 >= 27:
                self.anim_count = 0
            if self.facing == 1:
                disp.blit(self.fb_left[self.anim_count//7], (self.player_x,self.player_y))
                self.anim_count += 1
            else:
                disp.blit(self.fb_left[self.anim_count//7], (self.player_x,self.player_y))
                self.anim_count += 1
    def move(self):
        for fireball in fireballs:
            if fireball.player_x < 500 and fireball.player_x > 0:
                fireball.player_x += fireball.fb_vel  # Moves the bullet by its vel
            else:
                fireballs.pop(fireballs.index(fireball))
                self.anim_count = 0
def redrawGameWindow():
    disp.blit(pygame.image.load('backgroung.png'), (0,0))
    cat.draw(disp)
    for fireball in fireballs:
        fireball.draw(disp)
    pygame.display.update()
cat = player(50, 500, 50, 50)
run = True #Для включенного состояния игры
fireballs = []
while run:
    frame.tick(30)
    for events in pygame.event.get():
        if events.type == pygame.QUIT:  #Выход из игры
            run = False
    keys = pygame.key.get_pressed()
    if keys[pygame.K_f]:
        if cat.direct == -1:
            facing = -1
        else:
            facing = 1
        if len(fireballs) < 10:
            fireballs.append(fireball_one(round(cat.player_x + cat.width//2), round(cat.player_y + cat.height//2), facing))
                                 # This will create a bullet starting at the middle of the character
    if keys[pygame.K_LEFT] or keys[pygame.K_a]:
        if cat.player_x > 5:
            cat.direct = -1
            cat.player_x -= cat.player_speed
            cat.left = True
            cat.right = False
            cat.stand_left = False
            cat.stand_right = False
    elif keys[pygame.K_RIGHT] or keys[pygame.K_d]:
        if  cat.player_x < 970:
            cat.direct = 1
            cat.player_x += cat.player_speed
            cat.left = False
            cat.right = True
            cat.stand_left = False
            cat.stand_right = False
    else:
        if cat.direct == -1:
            cat.stand_left = False
        elif cat.direct == 1:
            cat.stand_right = False
        cat.left = False
        cat.right = False
    if not(cat.jump):
        if keys[pygame.K_SPACE]:
                cat.jump = True
    else:
        if cat.jump_dist >= -7:
            if cat.jump_dist < 0:
                cat.player_y += (cat.jump_dist ** 2) / 3
            else:
                cat.player_y -= (cat.jump_dist ** 2) / 3
            cat.jump_dist -= 1
        else:
            cat.jump = False
            cat.jump_dist = 7
    redrawGameWindow()
pygame.quit()

Офлайн

Board footer

Модераторировать

Powered by DjangoBB

Lo-Fi Version