Pygame碰撞互动

时间:2013-11-06 22:05:46

标签: 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


#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="boy.png"
player=pygame.image.load(character).convert_alpha()

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
            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
# Enemy 
if direction == "down":
        player_rect.centery += 1
elif direction == "up":
        player_rect.centery -= 1  

if player_rect.bottom > size[1]:
    direction = "up"
if player_rect.top < 0:
    direction = "down"                   

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(player,(x,y))
screen.blit(enemy, player_rect)
#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() 

现在我想知道是否有人知道怎么做碰撞,例如玩家男孩正在触摸敌人的怪物然后吸引到屏幕的生命值下降。 谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

这是我对stackoverflow上另一个问题的回答的代码,稍后进行了一些修改。

我在这里有碰撞检测,但它并不理想。

寻找pygame.sprite.collide_rect(self.ball, self.player1)

使用矩形区域来检测碰撞

PyGame Sprite上的更多碰撞检测功能(矩形,圆形,带蒙版)

(移动player1(键:W / S)或player2(键:ARROWS UP / DOWN)与球发生碰撞)


位置(player,{{1}

ps。: ballself.rect需要self.rect.xpygame.Rect()) })和尺寸(self.rect.yself.rect.width)可以与self.rect.height一起使用。如果您使用PyGame Sprite创建玩家类(pygame.sprite.collide_rect()),则无需创建class Player(pygame.sprite.Sprite):,因为Sprite已经有self.rect - 但您仍需要更改{{1}移动玩家时移动。{/ p>。}和self.rect


self.rect.x

截图

screenshot


游戏中使用的图片:

ball.png ball

player.png player

background.jpg background