海豚动画[PYGAME]

时间:2019-01-20 03:16:33

标签: python animation pygame sprite

我正在尝试为游戏中的两个角色设置动画。我希望播放动画时不按住任何键。我看过的每个教程都要求播放器按一个键才能播放动画。您如何使海豚动画化,但仍然可以使用现有的代码工作?目前,我将海豚设置为frame[0],因此在运行时可以看到它。任何帮助表示赞赏!

图像和声音FX下载:https://mega.nz/#F!7O5zRQDK!YQhrs_zavCvdSdAMwEXEIQ

我基于的游戏:https://www.youtube.com/watch?v=9jjy9PjbeiA&t=3s

import pygame
import random
import time
import os
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d, %d" %(0, 20)
pygame.init()

SIZE = W, H = 400, 700
screen = pygame.display.set_mode(SIZE)
clock = pygame.time.Clock()

# colours
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
BACKGROUND = (94, 194, 222)
STRIPE = (60, 160, 190)
LANELINE = (255, 255, 255)

x1 = 30
x2 = 330
lane1 = 30
lane2 = 130
lane3 = 230
lane4 = 330
y = 530
width = 40
height = 64

toggle1 = 0
toggle2 = 0

target_x1 = 30
target_x2 = 330
vel_x = 10

def drawScene():
    screen.fill(BACKGROUND)
    pygame.draw.polygon(screen, STRIPE, ((200, 700), (300, 700), (400, 600), (400, 500)))
    pygame.draw.polygon(screen, STRIPE, ((0, 700), (100, 700), (400, 400), (400, 300)))
    pygame.draw.polygon(screen, STRIPE, ((0, 500), (0, 600), (400, 200), (400, 100)))
    pygame.draw.polygon(screen, STRIPE, ((0, 300), (0, 400), (400, 0), (300, 0)))
    pygame.draw.polygon(screen, STRIPE, ((0, 100), (0, 200), (200, 0), (100, 0)))
    pygame.draw.line(screen, LANELINE, (100, 0), (100, 700), 2)
    pygame.draw.line(screen, LANELINE, (200, 0), (200, 700), 4)
    pygame.draw.line(screen, LANELINE, (300, 0), (300, 700), 2)


dolphinSheet = pygame.image.load("dolphinSheet.png").convert()
cells = []
for n in range(6):
    dolphinW, dolphinH, = (31, 74)
    rect = pygame.Rect(n * dolphinW, 0, dolphinW, dolphinH)
    image = pygame.Surface(rect.size).convert()
    image.blit(dolphinSheet, (0, 0), rect)
    alpha = image.get_at((0, 0))
    image.set_colorkey(alpha)
    cells.append(image)


playerImg = cells[0]


# main loop
run = True
while run:
    clock.tick(60)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_a:
                pygame.mixer.music.load('percussiveHit.mp3')
                pygame.mixer.music.play()
                toggle1 += 1
                if toggle1 % 2 == 1:
                    target_x1 += 100
                else:
                    target_x1 -= 100
            elif event.key == pygame.K_d:
                pygame.mixer.music.load('percussiveHit.mp3')
                pygame.mixer.music.play()
                toggle2 += 1
                if toggle2 % 2 == 1:
                    target_x2 -= 100
                else:
                    target_x2 += 100

    if x1 < target_x1:
        x1 = min(x1 + vel_x, target_x1)
    else:
        x1 = max(x1 - vel_x, target_x1)

    if x2 < target_x2:
        x2 = min(x2 + vel_x, target_x2)
    else:
        x2 = max(x2 - vel_x, target_x2)

    pygame.draw.rect(screen, RED, (x1, y, width, height))
    pygame.draw.rect(screen, RED, (x2, y, width, height))
    drawScene()
    # players
    screen.blit(playerImg, (x1 + 4, y - 5))
    screen.blit(playerImg, (x2 + 4, y - 5))
    pygame.display.update()

pygame.quit()

1 个答案:

答案 0 :(得分:0)

使用pygame.time.get_ticks()根据实时毫秒值更新图像,该值返回自pygame.init()起的毫秒数。

这个想法是代码会记住更改框架的时间,然后等到经过 X 秒(下面的playerImg_show_for)之后,再切换到该组中的下一个图像。

...

playerImg = cells[0]
playerImg_cell     = -1   # start at first cell
playerImg_shown_at = 0    # time when this frame (cell) was shown
playerImg_show_for = 200  # milliseconds


# main loop
run = True
while run:
    clock.tick(60)
    ms_now = pygame.time.get_ticks()  # milliseconds since start

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    # ... handle keys, etc. removed for the sake of brevity

    pygame.draw.rect(screen, RED, (x1, y, width, height))
    pygame.draw.rect(screen, RED, (x2, y, width, height))
    drawScene()

    # players
    # If this frame of animation has been shown for long enough, then
    # switch to the next frame
    if ( ms_now - playerImg_shown_at > playerImg_show_for ):
        playerImg_cell += 1   # find the next cell, with wrap-around
        if ( playerImg_cell >= len( cells ) ):
            playerImg_cell = 0
        playerImg_shown_at = ms_now        # use new start time
        playerImg = cells[playerImg_cell]  # use new animation cell

    screen.blit(playerImg, (x1 + 4, y - 5))
    screen.blit(playerImg, (x2 + 4, y - 5))
    pygame.display.update()

pygame.quit()

animation demo

当然,这将更适合作为PyGame Sprite对象,并且全部由Sprite的update()函数处理。