1

プレイヤーが剣を持っているプラ​​ットフォーマー ゲームに取り組んでいます。私は、プレイヤーが彼の下に地面がある場合にのみ攻撃できるようにしたい (したがって、彼は空中で攻撃できない)。だから私はこのコードを実装しました:

for b in instancelist:
    for p in players:
        if b.rect.collidepoint(p.rect.centerx,p.rect.bottom+4):
            grounded=1
        else:
            grounded=0
print grounded

しかし..

print grounded

毎回ゼロを返します..exit(1)プレイヤーが地面にいることをテストする部分に追加しても、それでも終了します! (接地はまだゼロに等しい間)

コード全体は次のとおりです。

import pygame,random
from collections import namedtuple
from pygame.locals import *
pygame.init()
pygame.display.set_caption('Legend of Zelda | By Sam Tubb')
screen=pygame.display.set_mode((640,480))
instancelist=[]
players=[]
enemys=[]
clock=pygame.time.Clock()
Move = namedtuple('Move', ['up', 'left', 'right'])

#load sprites
block1=pygame.image.load('block1.png').convert()

#load/init player stuffs
playersprites=[pygame.image.load('link1.png').convert(),pygame.image.load('link2.png').convert(),
pygame.image.load('link3.png').convert(),pygame.image.load('linkatk.png').convert(),pygame.image.load('linkup.png').convert()]
for s in playersprites:
    s.set_colorkey((255,0,0))
frame=0
frameplus=1
frametime=0
psprite=playersprites[frame]
max_gravity = 75
left=0
atk=0
atktime=0
grounded=0

class Enemy(object):
    def __init__(self,x,y):
        self.x=x
        self.y=y
        self.sprite=playersprites[0]
        self.rect=self.sprite.get_rect(left=x,top=y)
class Player(object):
    sprite=psprite
    def __init__(self, x, y):
        self.rect = self.sprite.get_rect(centery=y, centerx=x)
        # indicates that we are standing on the ground
        # and thus are "allowed" to jump
        self.on_ground = True
        self.xvel = 0
        self.yvel = 0
        self.jump_speed = 7
        self.move_speed = 3

    def update(self, move, blocks):

        # check if we can jump 
        if move.up and self.on_ground:
            self.yvel -= self.jump_speed

        # simple left/right movement
        if move.left:
                self.xvel = -self.move_speed
        if move.right:
                self.xvel = self.move_speed

        # if in the air, fall down
        if not self.on_ground:
            self.yvel += 0.3
            # but not too fast
            if self.yvel > max_gravity: self.yvel = max_gravity

        # if no left/right movement, x speed is 0, of course
        if not (move.left or move.right):
            self.xvel = 0

        # move horizontal, and check for horizontal collisions
        self.rect.left += self.xvel
        self.collide(self.xvel, 0, blocks)

        # move vertically, and check for vertical collisions
        self.rect.top += self.yvel
        self.on_ground = False;
        self.collide(0, self.yvel, blocks)

    def collide(self, xvel, yvel, blocks):
        # all blocks that we collide with
        for block in [blocks[i] for i in self.rect.collidelistall(blocks)]:

            # if xvel is > 0, we know our right side bumped 
            # into the left side of a block etc.
            if xvel > 0:
                    self.rect.right = block.rect.left
            if xvel < 0:
                    self.rect.left = block.rect.right

            # if yvel > 0, we are falling, so if a collision happpens 
            # we know we hit the ground (remember, we seperated checking for
            # horizontal and vertical collision, so if yvel != 0, xvel is 0)
            if yvel > 0:
                self.rect.bottom = block.rect.top+0.4
                self.on_ground = True
                self.yvel = 0
            # if yvel < 0 and a collision occurs, we bumped our head
            # on a block above us
            if yvel < 0: self.rect.top = block.rect.bottom

class Block(object):
    def __init__(self,x,y,sprite):
        self.x=x
        self.y=y
        self.sprite=sprite
        self.rect=self.sprite.get_rect(x=self.x,y=self.y)

x = y = 0
level = [
"BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB",
"B                                       B",
"B                                       B",
"B               L                       B",
"B                                       B",
"B                                       B",
"B                                       B",
"B                       E               B",
"B                                       B",
"B         BBBBBBBBBBBBBBB               B",
"B        B                              B",
"B     E B                               B",
"B      B                                B",
"B     B                                 B",
"B    B                                  B",
"B   B                                   B",
"B                   E                   B",
"B                                       B",
"BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB",
]
# build the level
for row in level:
    for col in row:
        if col == "B":
            p = Block(x,y,block1)
            instancelist.append(p)
        if col=="L":
             players.append(Player(x, y))
        if col=="E":
            enemys.append(Enemy(x,y))
        x += 16
    y += 16
    x = 0

#mainloop
while True:
    for b in instancelist:
        for p in players:
            if b.rect.collidepoint(p.rect.centerx,p.rect.bottom+4):
                grounded=1
            else:
                grounded=0
    print grounded
    #total_level_width  = len(level[0])*16
    #total_level_height = len(level)*16
    screen.fill((0,0,0))
    mse=pygame.mouse.get_pos()
    key=pygame.key.get_pressed()
    if atk==0:
        if not key[K_w]:
            if key[K_a]:
                left=1
                if frameplus==1:
                    if frame<2:
                        frameplus=0
                        frametime=5
                        frame+=1
                    else:
                        frame=0
                psprite=pygame.transform.flip(playersprites[frame],1,0)
            if key[K_d]:
                left=0
                if frameplus==1:
                    if frame<2:
                        frameplus=0
                        frametime=5
                        frame+=1
                    else:
                        frame=0
                psprite=playersprites[frame]
            if not key[K_a]:
                if not key[K_d]:
                    if left==0:
                        psprite=playersprites[0]
                    else:
                        psprite=pygame.transform.flip(playersprites[0],1,0)
        else:
            if left==0:
                psprite=playersprites[4]
            else:
                psprite=pygame.transform.flip(playersprites[4],1,0)
    if key[K_SPACE]:
        if atk==0:
            atk=1
            atktime=15
            if left==0:
                psprite=playersprites[3]
                p.rect.right+=4
            else:
                psprite=pygame.transform.flip(playersprites[3],1,0)
                p.rect.right-=16
    for e in pygame.event.get():
        if e.type==QUIT:
            exit(1)
    for inst in instancelist:
        screen.blit(inst.sprite,inst.rect)
    for p in players:
        if atk==0:
            p.rect=psprite.get_rect(x=p.rect.left,y=p.rect.top)
            move = Move(key[K_w], key[K_a], key[K_d])
            p.update(move, instancelist)
        #scrolling
        if p.rect.right>=500:
            for inst in instancelist:
                inst.rect.left-=4
            for e in enemys:
                e.rect.left-=4
            p.rect.left-=4
        if p.rect.left<=140:
            for inst in instancelist:
                inst.rect.left+=4
            for e in enemys:
                e.rect.left+=4
            p.rect.left+=4
        if p.rect.top<=80:
            for inst in instancelist:
                inst.rect.top+=4
            for e in enemys:
                e.rect.top+=4
            p.rect.top+=4
        if p.rect.bottom>300:
            for inst in instancelist:
                inst.rect.bottom-=4
            for e in enemys:
                e.rect.bottom-=4
            p.rect.bottom-=4


        for b in instancelist:
            if p.rect.colliderect(b.rect):
                p.ground=1
        screen.blit(psprite,p.rect)

        for e in enemys:
            if atk==1:
                if left==0:
                    if p.rect.left<e.rect.left:
                        if p.rect.colliderect(e.rect):
                            enemys.remove(e)
                else:
                    if p.rect.right>e.rect.right:
                        if p.rect.colliderect(e.rect):
                            enemys.remove(e)
            screen.blit(e.sprite,e.rect)

    #timers
    if frametime>0:
        frametime-=1
    else:
        frameplus=1

    if atktime>0:
        atktime-=1
    else:
        if atk==1:
            if left==1:
                p.rect.right+=16
            else:
                p.rect.right-=4
        atktime=0
        atk=0

    p.rect=psprite.get_rect(x=p.rect.left,y=p.rect.top)
    pygame.draw.rect(screen, ((255,0,0)), p.rect, 1)
    clock.tick(60)
    pygame.display.flip()

誰か手を貸してくれませんか?:)

4

2 に答える 2