如何在没有spritsheets的情况下制作行走角色 - Python与Pygame

时间:2015-05-23 00:09:25

标签: python animation python-3.x pygame

我想在不使用精灵表的情况下改变它移动的方向时让我的角色改变精灵。我没有使用精灵表的原因是因为它似乎不适用于我想要做的事情。

import pygame
import random
import time

pygame.init()

health = 100
food = 10

#Window parameters
display_width = 800
display_height = 600

black = (0,0,0)
white = (255,255,255)
red = (200,0,0)
green = (0,200,0)
blue = (0,0,255)
titlebrownic = (77, 51, 43)
titlebrownac = (128, 85, 70)

#Player Sprites
PlayerStandRight = pygame.image.load('Resources/PlayerStandRight.png')
PlayerStandLeft = pygame.image.load('Resources/PlayerStandLeft.png')
PlayerStrikeRight = pygame.image.load('Resources/PlayerStrikeRight.png')
PlayerStrikeLeft = pygame.image.load('Resources/PlayerStrikeLeft.png')
PlayerWalkRight1 = pygame.image.load('Resources/PlayerWalkRight1.png')
PlayerWalkRight2 = pygame.image.load('Resources/PlayerWalkRight2.png')
PlayerWalkLeft1 = pygame.image.load('Resources/PlayerWalkLeft1.png')
PlayerWalkLeft2 = pygame.image.load('Resources/PlayerWalkLeft2.png')

#Monster Sprites

#Display Stuff and Clock
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Endless Onslaught')
clock = pygame.time.Clock()

PlayerSpr = PlayerStandRight
#Functions Start
def quitgame():
    pygame.quit
    quit()

def player(x,y):
    gameDisplay.blit(PlayerSpr,(x,y))

def text_objects(text, font):
    textSurface = font.render(text, True, black)
    return textSurface, textSurface.get_rect()

def message_display(text):
    largeText = pygame.font.Font('freesansbold.ttf',115)
    TextSurf, TextRect = text_objects(text, largeText)
    TextRect.center = ((display_width/2), (display_height/2))
    gameDisplay.blit(TextSurf, TextRect)

    pygame.display.update()

    time.sleep(2)
    gameloop()
def button(msg, x, y, w, h, ic, ac, action = None):

    mouse = pygame.mouse.get_pos(msg, x, y, w, h, ic, ac)
    click = pygame.mouse.get_pressed()

    if x + 100 > mouse[0] > x and y + h > mouse[1] > y:
        pygame.draw.rect(gameDisplay, ac, (x,y,w,h))
        if click[0] == 1 and action != None:
            action()

    else:
        pygame.draw.rect(gameDisplay, ic, (x,y,w,h))

    smallText = pygame.font.Font('freesansbold.ttf', 20)
    textSurf, textRect = text_objects(msg, smallText)
    textRect.center = ((x + (w/2), y + (h/2)))
    gameDisplay.blit(textSurf, textRect)

def game_intro():

    intro = True

    while intro:
        for event in pygame.event.get():
            print(event)
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        gameDisplay.fill(white)
        largeText = pygame.font.Font('freesansbold.ttf',80)
        TextSurf, TextRect = text_objects("Endless Onslaught", largeText)
        TextRect.center = ((display_width/2),(display_height/2.85))
        gameDisplay.blit(TextSurf, TextRect)

        #The start button
        button('Go!', 150,450, 100, 50, titlebrownic, titlebrownac,game_loop)

        #The quit button
        button('Quit!', 550, 450, 100, 50, titlebrownic, titlebrownac,quitgame)

        pygame.display.update()
        clock.tick(15)

def game_loop():
    PlayerSpr = PlayerStandRight

    x = (display_width / 2)
    y = (display_height / 2)
    #When x changes
    x_change = 0

    player(display_width/2,display_height/2)

    while health > 0:

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

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -5
                    PlayerSpr = PlayerWalkLeft1
                    PlayerSpr = PlayerWalkLeft2
                if event.key == pygame.K_RIGHT:
                    x_change = 5

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    x_change = 0


        x += x_change 
        gameDisplay.fill(white)

        player(x,y)

        pygame.display.update()
        clock.tick(60)




#Functions end     
game_intro()        
game_loop()
pygame.quit
quit()

0 个答案:

没有答案