当函数不返回特定内容时,我该如何打印东西

时间:2019-02-18 05:34:50

标签: python function if-statement

def collideY(player, object):
    if man.x <= object.x + object.width and man.x + man.width >= object.x: # Checks if  the player is inside the bounderies of a list of objects
        return "F"

cY = collideY(Player, object)
if not cY == "F":
    print("Not F")

这始终打印“不是F”,我希望仅在不返回F时才打印 P.S这是我的完整代码,如果您想查看 这是马里奥的娱乐。游戏运行良好,但是当玩家离开平台时,我无法增加重力。我需要做的是检测玩家(人)何时不在对象的x范围内。我有一个名为CollisionY()的函数,该函数已经检查玩家是否在对象的x范围内(所有这些都位于while循环中),当它检测到x范围内的人时,我可以使该函数返回一个变量,但是由于某种原因,我无法检测到它不在对象x范围内

import pygame
from pygame import *

pygame.init()

# Screen Setup
Screen_Height = 600
Screen_Width = 800
Screen_Size = (Screen_Width, Screen_Height)
win = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Mario")

# Sprites for Mario
walkRight = [pygame.image.load('mario_move0.png'), pygame.image.load('mario_move1.png'),
             pygame.image.load('mario_move2.png')]
walkLeft = [pygame.image.load('mario_move02.png'), pygame.image.load('mario_move12.png'),
            pygame.image.load('mario_move22.png')]

char = pygame.image.load('mario.png')
char2 = pygame.image.load('mario2.png')
jumping = pygame.image.load("mario_jump.png")
jumping2 = pygame.image.load("mario_jump2.png")

#Setup Background
bg = pygame.image.load('level_1.png')
bg = pygame.transform.scale(bg, (9000, 600)).convert()
bgWidth, bgHeight = bg.get_rect().size

#Map sprites
spritesheet = pygame.image.load("tile_set.png")
character = Surface((15, 16), pygame.SRCALPHA)
character.blit(spritesheet, (-17, 0))
character = pygame.transform.scale(character, (44, 44))
brick1 = character




clock = pygame.time.Clock()

class player(object):
    def __init__(self, x, y, width, height ):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.vel = 1
        self.isJump = False
        self.left = False
        self.right = False
        self.standing = True
        self.walkCount = 0
        self.jumpCount = 10
        self.starting = True
        self.runCount = 0
        self.jumping = 0
        self.currenty = 410
        self.done = True
        self.ableJump = False
        self.scroll = 0
        self.x3 = 0
        self.fall = False
        self.rightStop = False
        self.leftStop = False
        self.grav = .4
    def draw(self, win):

        if self.walkCount + .5 >= 3:
            self.walkCount = 0

        if self.runCount >= 12:
            self.runCount = 12

        if self.left and not self.isJump and not self.standing and not self.fall:
            win.blit(walkLeft[self.walkCount], (self.x, self.y))
            self.walkCount += 1
            if self.runCount < 12:
                self.runCount += 1

        elif self.right and not self.isJump and not self.standing and not self.fall:
            win.blit(walkRight[self.walkCount], (self.x, self.y))
            self.walkCount += 1
            if self.runCount < 12:
                self.runCount += 1

        if self.standing and self.right and not self.isJump or self.starting and not self.fall:
            win.blit(char, (self.x, self.y))

        elif self.standing and self.left and not self.isJump and not self.fall:
            win.blit(char2, (self.x, self.y))

        elif self.isJump or self.fall:
            if self.right:
                win.blit(jumping, (self.x, self.y))

            elif self.left:
                 win.blit(jumping2, (self.x, self.y))

class Object(object):

    def __init__(self, x, y, width, height, type=""):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.type = type # OPTIONAL

    def draw(self, win):
        pygame.draw.rect(win, (255,0,0), (self.x, self.y, self.width, self.height), 5)

    def draw_brick(self, win):
        win.blit(brick1,(self.x, self.y, self.width, self.height))

def collideY(player, object):
    if man.x <= object.x + object.width and man.x + man.width >= object.x: # Top check
        if man.y <= object.y + object.height and man.y + man.height >= object.y + object.height:
            return "D"
        elif man.y <= object.y and man.y + man.height >= object.y:
            return "U"
    else:
        return "F"

    return False

def collideX(player, object):
    if man.x + man.width >= object.x and man.x <= object.x: # Left side collision
        if man.y + man.height > object.y and man.y < object.y + object.height:
            return "L"

    elif man.x <= object.x + object.width and man.x + man.width >= object.x + object.width: # right side collision
        if man.y + man.height > object.y and man.y < object.y + object.height:
            return "R"

def right():
    # Right movement
    if not keys[pygame.K_LEFT]:
        if keys[pygame.K_s]:
            if man.x == 400 and not man.fall and man.x3 < 8572:
                man.scroll -= (man.vel * man.runCount) * 1.5
            else:
                man.x += (man.vel * man.runCount) * 1.5
        elif man.x == 400 and not man.fall and man.x3 < 8572:
            man.scroll -= man.vel * man.runCount
        else:
            man.x += man.vel * man.runCount

        if man.x >= 400 and man.x3 < 8572:
            man.x = 400

        man.right = True
        man.left = False
        man.standing = False
        man.starting = False

def left():
    if keys[pygame.K_s]:
        man.x -= (man.vel * man.runCount) * 1.5
    else:
        man.x -= man.vel * man.runCount
    man.left = True
    man.right = False
    man.standing = False
    man.starting = False

def standing():
    man.standing = True
    man.done = True
    man.isJump2 = False
    man.walkCount = 0
    man.runCount = 0
    if man.right or man.left:
        if not man.isJump:
            man.alex = 0

def redrawGameWindow():

    man.draw(win)
    for o in objs:
        o.draw(win)
    for o in objs_1:
        o.draw_brick(win)
    pygame.display.update()
    win.blit(bg, (man.scroll, 0))
# mainloop
man = player(200, 505, 32, 32)

run = True
while run:
    clock.tick(27)

    objs = [  # Solid Blocks
            Object(man.scroll + 1196, 451, 71, 100),
            Object(man.scroll + 1622, 410, 71, 135),
            Object(man.scroll + 1961, 368, 71, 185),
            Object(man.scroll + 2427, 368, 71, 185),

            Object(man.scroll + 5690, 490, 167, 40),
            Object(man.scroll + 5734, 449, 123, 40),
            Object(man.scroll + 5775, 408, 80, 40),
            Object(man.scroll + 5817, 365, 40, 40),

            Object(man.scroll + 5942, 490, 167, 40),
            Object(man.scroll + 5942, 449, 125, 40),
            Object(man.scroll + 5942, 408, 80, 40),
            Object(man.scroll + 5943, 365, 40, 40),

            Object(man.scroll + 6287, 490, 207, 40),
            Object(man.scroll + 6331, 449, 163, 40),
            Object(man.scroll + 6372, 408, 120, 40),
            Object(man.scroll + 6414, 365, 80, 40),

            Object(man.scroll + 6579, 490, 167, 40),
            Object(man.scroll + 6579, 449, 125, 40),
            Object(man.scroll + 6579, 408, 80, 40),
            Object(man.scroll + 6579, 365, 40, 40),

            Object(man.scroll + 6928, 451, 71, 100),
            Object(man.scroll + 7607, 451, 73, 100),

            Object(man.scroll + 7680, 492, 382, 40),
            Object(man.scroll + 7724, 452, 341, 40),
            Object(man.scroll + 7765, 410, 299, 40),
            Object(man.scroll + 7808, 366, 256, 40),
            Object(man.scroll + 7851, 324, 214, 40),
            Object(man.scroll + 7893, 281, 172, 40),
            Object(man.scroll + 7934, 238, 130, 40),
            Object(man.scroll + 7978, 194, 88, 40),

            Object(man.scroll + 8407, 491, 40, 40),
           ]

    objs_1 = [
                Object(man.scroll + 850, 381, 44, 44),
                Object(man.scroll + 938, 381, 44, 44),
                Object(man.scroll + 1026, 381, 44, 44),

             ]

# Jump Height
    if man.standing or man.left or man.right:
        if not man.isJump:
            man.currenty = man.y
    jumpHeight = man.currenty - man.y
    jumpHeight = int(jumpHeight)

# Collisions / interactions for solid blocks
    for obj in objs:
        cX = collideX(man, obj)
        cY = collideY(man, obj)
        if man.isJump :
            if cY == "U":
                print("Up")
                man.standing = True
                man.isJump = False
                man.ableJump = True
                man.y = obj.y - man.height
            elif cY == "D":
                print("Down")
                man.y = obj.y + obj.height + 1
            elif cX == "R" and cY == False:
                print("Right")
                man.x = obj.x + obj.width + 1
            elif cX == "L" and cY == False:
                print("Left")
                man.x = obj.x - man.width - 1
        else:
            if cX == "R":
                print("Right")
                man.x = obj.x + obj.width + 1
            elif cX == "L":
                print("Left")
                man.x = obj.x - man.width - 1

# Collision / interactions with brick1
    for obj in objs_1:
        cX = collideX(man, obj)
        cY = collideY(man, obj)
        if man.isJump :
            if cY == "U":
                print("Up")
                man.standing = True
                man.isJump = False
                man.ableJump = True
                man.y = obj.y - man.height
            elif cY == "D":
                print("Down")
                if man.y < obj.y + obj.height:
                    man.y = obj.y + obj.height
                man.ableJump = False
            elif cX == "R" and cY == False:
                print("Right")
                man.x = obj.x + obj.width + 1
            elif cX == "L" and cY == False:
                print("Left")
                man.x = obj.x - man.width - 1
        elif not man.isJump:
            if cX == "R":
                print("Right")
                man.x = obj.x + obj.width + 1
            elif cX == "L":
                print("Left")
                man.x = obj.x - man.width - 1
        if not cY == "F":
            print("00")
        #else:
            #print("69")

# X.3 is the equivalent x cordinate on the map
    man.x3 = -1 * man.scroll + man.x

# Able to Quit without crashing
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

# Defies the movement keys
    keys = pygame.key.get_pressed()

# Left movement
    if keys[pygame.K_LEFT] and man.x > 4 and not man.leftStop:
        left()

# Right movement
    elif keys[pygame.K_RIGHT] and man.x3 < 8956 and not man.rightStop:
        right()

    elif not man.isJump:
        standing()

# Jump mechanics
    if keys[pygame.K_UP] and man.ableJump:
        if jumpHeight < 200:
            man.isJump = True
            man.runCount = 10
            man.jumpCount = 6

        else:
            man.ableJump = False
    elif man.isJump:
        man.ableJump = False

    if man.isJump:
        if man.jumpCount >= -8:
            neg = 1
            if man.jumpCount < 0:
                neg = -1
            man.jumpCount -= 1
        else:
            man.jumpCount = -8
        man.y -= (man.jumpCount ** 2) * 0.5 * neg



# Ground floor
    if man.y >= 505:
        if man.x3 > 2927 and man.x3 < 2985 or man.x3 > 3649 and man.x3 < 3750 or man.x3 > 6492 and man.x3 < 6552:
            man.fall = True
            man.y -= (man.jumpCount ** 2) * 0.09 * -1
        elif not man.fall:
            man.y = 505
            man.isJump = False
            man.ableJump = True

    redrawGameWindow()

pygame.quit()

1 个答案:

答案 0 :(得分:0)

几个问题

  1. 问题是,您的collideY函数永远不会返回“ F”,因为您没有将其传递给类的实例。相反,您将类本身传递给它。

  2. object是python内部类。不要命名变量object

尝试将collideY方法更改为此

def collideY(man, bounds):
    if man.x <= bounds.x + bounds.width and man.x + man.width >= bounds.x: # Checks if  the player is inside the bounderies of a list of objects
        return "F"

并调用collideY(man, bounds)

这样的函数

您正在传递一个类实例,但是如果您希望它可以工作,则应该传递一个像man这样的对象实例。