Pygame角色图像交换

时间:2013-11-08 17:39:25

标签: android python 2d pygame

import pygame, sys
from pygame.locals import * 

try:
    import Android
except ImportError:
    Android = None

try:
    import pygame.mixer as mixer
except ImportError:
    import android.mixer as mixer    

pygame.init()

#Android to cross platform

if Android:
    android.init()
    android.map_key(android.KEYCODE_BACK, pygame.K_ESCAPE)

#Variables

score = 0
health = 100
enemy_x = 0
enemy_y = 0
#Colours

RED = (255 ,0 ,0)
BLACK = (0, 0, 0)

# Left Edge of rectangle, top, width, height
player_rect = pygame.Rect(540, 0, 32, 32)
enemy = pygame.image.load("monster.png")

size = width, height = 1276,650
screen = pygame.display.set_mode(size)

direction ="down"

# This is the score text
gamefont = pygame.font.Font(None, 30)
scoretext = gamefont.render('Player Score: '+ str(score), 2, [255,0,0])
boxsize=scoretext.get_rect()
scoreXpos=(width-boxsize[2])/2  
scoreYpos=(height-boxsize[2])/2  
screen.blit(scoretext, [scoreXpos,20])

# This is the health 
gamefonts = pygame.font.Font(None, 30)
scoretexts = gamefonts.render('Player Health: '+ str(health), 2, [255,0,0])
boxsizes=scoretexts.get_rect()
scoreXposs=(width-boxsizes[2])/2 
scoreYposs=(height-boxsizes[2])/2  
screen.blit(scoretexts, [scoreXposs,20])

PAUSED = False
font = pygame.font.SysFont("serif", 72)
text_paused = font.render("PAUSED", True, (255, 0, 0))


sound = mixer.Sound('maintheme.ogg')
sound.play()


r = 0
bif = pygame.image.load("map5.png") 
pygame.display.set_caption("Pygame 2D RPG !")
x,y=0,0
movex, movey=0,0

character_w = "character_player/character_w.png"
character_a = "character_player/character_a.png"
character_s = "character_player/character_s.png"
character_d = "character_player/character_d.png"

character_w1 = pygame.image.load(character_w).convert_alpha()
character_a1 = pygame.image.load(character_a).convert_alpha()
character_s1 = pygame.image.load(character_s).convert_alpha()
character_d1 = pygame.image.load(character_d).convert_alpha()

clock = pygame.time.Clock()
enemy_speed = 100

while True:

    # --- events ---

    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()


        if event.type==KEYDOWN:
            if event.key == K_ESCAPE:
                pygame.quit()
            sys.exit()
            elif event.key==K_q:
                PAUSED = not PAUSED

        if not PAUSED:
            if event.type==KEYDOWN:
                if event.key==K_a:
                    movex=-10
                    screen.blit(character_a1,(x,y))

                elif event.key==K_d:
                    movex=+10
                elif event.key==K_w:
                    movey=-10
                elif event.key==K_s:
                    movey=+10
            if event.type==KEYUP:
                if event.key==K_a:
                    movex=0
                elif event.key==K_d:
                    movex=0
                elif event.key==K_w:
                    movey=0
                elif event.key==K_s:
                    movey=0


if not PAUSED:
    x+=movex
    y+=movey   
    print x,y
if x in range(680,702) and y in range(377,403):  # This is the location of the tile.
    bif = pygame.image.load("map6.png")
screen.fill((r,0,0))
screen.blit(bif,(0,0))
screen.blit(character_s,(x,y))
screen.blit(enemy, (enemy_x, enemy_y))

milli = clock.tick()
seconds = milli/1000.
dm = seconds*enemy_speed
#enemy_x += dm 


#enemy_x+=10

#if enemy_x>1280:
    #enemy_x = 0

#pygame.draw.rect(screen, RED, player_rect) 

screen.blit(scoretexts, [scoreYposs, 45])
scoretexts = gamefonts.render('Player Health: '+ str(health), 2, [25,25,112])
#health +=10


screen.blit(scoretext, [scoreYpos,70])
scoretext = gamefont.render('Player Score: '+ str(score), 2, [25,25,112])
#score +=10   

if PAUSED:
    screen.blit(text_paused, (100, 100))    

pygame.display.update() 

按下a键时图像不会改变,因为它意味着切换到不同的图像,但不是。请有人帮助我,我现在不知道为什么不改变。

再次感谢您的帮助

2 个答案:

答案 0 :(得分:1)

略过代码,我认为您的图片正在发生变化,但您无法看到它。您有以下内容:

if event.type==KEYDOWN:
    if event.key==K_a:
        movex=-10
        screen.blit(character_a1,(x,y))

因此,在处理按键时生成的单数按键事件的确切时刻,您将blit替代图像显示在屏幕上。 ......然后在那个框架中,你将要绘制它:

screen.blit(character_s,(x,y))

这一行由你的代码每帧运行,这意味着图像总是要绘制。由于它位于备用图像的blit之后,因此它将始终绘制它。即使替换图像的blit在此之后出现,也只会绘制一帧,因为每次按键时你只会处理一个KEYDOWN事件。

尝试以下模式:

# Along with the rest of your global variables...
activeCharacterImage = character_s

# The rest of your code...

if event.type==KEYDOWN:
    # When the 'A' key is pressed, switch the character image.
    if event.key==K_a:
        movex=-10
        activeCharacterImage = character_a1

# Some more of your code...

# Your new character drawing call.
screen.blit(activeCharacterImage, (x,y))

答案 1 :(得分:0)

我还建议使用“服装”变量并将其设置为您要使用的角色的图像。

if player_x == 500:
    costume = "james.jpg"