如何使我的玩家跳入pygame?

时间:2018-11-02 10:24:23

标签: python pygame

我要在我的pygame游戏中介绍跳跃功能,但仍然无法正常工作,我试图定义一个函数来检查玩家是否发生碰撞,并让玩家在未发生碰撞的情况下跳跃,但是由于什么原因失败了python调用“影子名称”(Shadows name)我不确定现在该怎么办,您能否检查代码并告诉我如何实现跳跃以及我的努力出了什么问题?

import pygame, sys, random
from pygame.locals import *

pygame.init()
mainClock = pygame.time.Clock()
windowWidth = 600
windowHeight = 400
windowSize = (600, 400)
windowSurface = pygame.display.set_mode((windowWidth, windowHeight), 0,     32)
display = pygame.Surface((300, 200))
black = (0, 0, 0)
white = (225, 225, 255)
green = (0, 255, 0)
moveRight = False
moveLeft = False
moveSpeed = 6
tileRect = []

level= [['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],
    ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],
    ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],
    ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],
    ['0','0','0','0','0','0','0','2','2','2','2','2','0','0','0','0','0','0','0'],
    ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],
    ['2','2','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','2','2'],
    ['1','1','2','2','2','2','2','2','2','2','2','2','0','0','2','2','2','1','1'],
    ['1','1','1','1','1','1','1','1','1','1','1','1','0','0','1','1','1','1','1'],
    ['1','1','1','1','1','1','1','1','1','1','1','1','0','0','1','1','1','1','1'],
    ['1','1','1','1','1','1','1','1','1','1','1','1','0','0','1','1','1','1','1'],
    ['1','1','1','1','1','1','1','1','1','1','1','1','0','0','1','1','1','1','1'],
    ['1','1','1','1','1','1','1','1','1','1','1','1','0','0','1','1','1','1','1']]

grass = pygame.image.load('grass.png')
dirt = pygame.image.load('dirt.png')
player = pygame.image.load('player.png').convert()
playerRect = pygame.Rect(100, 100, 5, 13)
player.set_colorkey((255,255,255))
gravity = 1.2
isStanding = False
isJumping = False
yVelocity = 0
playerMovement = [0, 0]

def jump(): # the function I tryed to make, isn't finished yet.
    yVelocity = 0
    if player.colliderect(tileRect == '1', '2'):
        isStanding = True:
    else:
        isJumping = True:
        yVelocity += 3



while True:
    # Handle events.
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.type == pygame.K_RIGHT or event.key == pygame.K_d:
                playerMovement[0] = 2  # Set the x-velocity to the desired value.
            if event.type == pygame.K_LEFT or event.key == pygame.K_a:
                playerMovement[0] = -2
        if event.type == pygame.KEYUP:
            if event.type == pygame.K_RIGHT or event.key == pygame.K_d:
                playerMovement[0] = 0  # Stop the player when the button is released.
            if event.type == pygame.K_LEFT or event.key == pygame.K_a:
                playerMovement[0] = 0

# Game logic.
playerRect.move_ip(playerMovement)

# Draw everything.
display.fill((214, 42, 78))

y = 0
for row in level:
    x = 0
    for col in row:
        if col == '2':
            display.blit(grass, (x*16, y*16))
        if col == '1':
            display.blit(dirt, (x*16, y*16))
        if col != '0':
            tileRect.append(pygame.Rect(x*16,y*16,16,16))
        x += 1
    y += 1

display.blit(player, playerRect)
windowSurface.blit(pygame.transform.scale(display, windowSize), (0, 0))
pygame.display.update()
mainClock.tick(60)

0 个答案:

没有答案