如何在玩家精灵(图片)和简单框

时间:2015-05-27 09:38:58

标签: python pygame sprite collision

我正在制作一个简单的马里奥游戏。我只是想拥有一个角色,显然马里奥会跳进一组碰撞盒,这些碰撞盒将显示一个数学问题的答案。到目前为止,我已经设法将重力和水平碰撞放在一起,但我似乎无法解决如何让我的角色在与简单的盒子/矩形碰撞时给我一个输出。到目前为止,这是我的代码:

import pygame
from block import *
from Player import *
pygame.mixer.pre_init(44100,  -16,  2,  2048)
pygame.init()
window = pygame.display.set_mode((800,  600))
pygame.display.set_caption("Super Mario Bros")
myfont = pygame.font.SysFont("Comic Sans MS", 100)
mariosound = pygame.mixer.music.load("data/sound.mp3")
pygame.mixer.music.play(-1)
gravity = -0.5
black = (0,  0,  0)
orange = (255,  174,  0)
blue = (11,  183,  217)
clock = pygame.time.Clock()
player = Player(0,  0)
label = myfont.render("9+10=", 1, black)

level1 = [
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
]

def printText(txtText, Textfont, Textsize , Textx, Texty, Textcolor):
    myfont = pygame.font.SysFont(Textfont, Textsize)
    label = myfont.render(txtText, 1, Textcolor)
    window.blit(label, (Textx, Texty))
    pygame.display.flip()

blockList = []
for y in range(0,  len(level1)):
    for x in range(0,  len(level1[y])):
        if (level1[y][x]==1):
            blockList.append(Block(x*32,  y*32))

movex,  movey = 0, 0

gameLoop = True
while gameLoop:
    for event in pygame.event.get():
        if (event.type == pygame.QUIT):
            gameLoop = False
        if (event.type == pygame.KEYDOWN):
            if (event.key == pygame.K_RIGHT):
                movex = 5
            elif (event.key == pygame.K_LEFT):
                movex = -5
            elif (event.key == pygame.K_UP):
                player.jump()
        if (event.type == pygame.KEYUP):
             if (event.key == pygame.K_RIGHT):
                movex = 0
             elif (event.key == pygame.K_LEFT):
                movex = 0

    window.fill(blue)
    window.blit(label, (275, 100))
    for block in blockList:
        block.render(window)
    player.x += movex
    player.update(gravity,  blockList)
    player.render(window)
    clock.tick(60)
    pygame.display.flip()

pygame.quit()

我在不同的python文件中有一个播放器和碰撞子类,命名为:Block和Player,只做碰撞和简单的重力等。

感谢您提供任何帮助,因为我说我非常新,所以任何解释或帮助都会受到赞赏。

这是我的其他代码: 我的Player.py: 导入pygame

class Player:
    def __init__(self, x, y):
        self.x=x
        self.y=y
        self.width=32
        self.height=32
        self.velocity=0
        self.falling=True
        self.onGround=False

        self.images = pygame.image.load('resources/sprite.png')
        self.images = pygame.transform.scale(self.images, (35, 35))
    def jump(self):
        if (self.onGround == False):
            return
        self.velocity = 14
        self.onGround = False

    def detectCollisions(self, x1, y1, w1, h1, x2, y2, w2, h2):
        if (x2 + w2 > x1 >= x2 and y2 + h2 >= y1 >= y2):
            return True
        elif (x2 + w2 >= x1 + w1 > x2 and y2 + h2 >= y1 >= y2):
            return True
        elif (x2 + w2 >= x1 >= x2 and y2 + h2 >= y1 + h1 >= y2):
            return True
        elif (x2 + w2 >= x1 + w1 >= x2 and y2 + h2 >= y1 + h1 >= y2):
            return True
        else:
            return False

    def update(self, gravity, blockList):
        if (self.velocity < 0):
            self.falling = True
        collision = False
        blockX,blockY = 0,0
        for block in  blockList:
            collision = self.detectCollisions(self.x, self.y, self.width, self.height, block.x, block.y, block.width, block.height)
            if (collision == True):
                blockX=block.x
                blockY=block.y
                break
        if (collision == True):
            if (self.falling == True):
                self.falling = False
                self.onGround = True
                self.velocity = 0
                self.y = blockY - self.height

        if (self.onGround == False):
            self.velocity += gravity

        self.y-=self.velocity

    def render(self, window):
        window.blit(self.images, (self.x, self.y))

    def render1(self, collision):
        if (collision==True):
            pygame.draw.rect(window,red,(self.x,self.y,self.width,self.height))
        else:
            pygame.draw.rect(window,black,(self.x,self.y,self.width,self.height))

和block.py: 导入pygame

class Block:
    def __init__(self, x, y):
        self.x=x
        self.y=y
        self.width=32
        self.height=32

    def render(self, window):
        pygame.draw.rect(window, (255,174,0),(self.x, self.y, self.width, self.height))

1 个答案:

答案 0 :(得分:0)

我不知道图像和盒子的大小,所以我记下了一些随机大小。希望它会对你有所帮助。 这是代码:

simplebox_width = 20 #pixels
simplebox_height = 20

image_width = 50
image_height = 250

image_x_coord = 100 #coords
image_y_coord = 200

simplebox_x_coord = 140
simplebox_y_coord = 250

image_x_side = range(image_x_coord, image_x_coord+image_width)
image_y_side = range(image_y_coord, image_y_coord+image_height)

simplebox_x_side = range(simplebox_x_coord, simplebox_x_coord+simplebox_width)
simplebox_y_side = range(simplebox_y_coord, simplebox_y_coord+simplebox_height)

for x in image_x_side:
    if x in simplebox_x_side:
        for y in image_y_side:
            if y in simplebox_y_side:
                collision = True
    else:
        collision = False

print collision