PyGame引力和跳跃

时间:2015-10-25 00:05:11

标签: python pygame collision gravity

我需要在游戏中跳跃和重力。我该如何实现呢?这就是我所拥有的:

import pygame

# initialise pygame

pygame.init()

#variables

level = 0

velx = 0
vely = 0

health = 1
floor_group = set([])


clock = pygame.time.Clock()

#screen size the same size as my background

window = pygame.display.set_mode((900,563))

#Load Images: Backgrounds

background0 = pygame.image.load("background0.png").convert()

#Load Sprites

Spike = pygame.image.load("spike.png").convert_alpha()
spider = pygame.image.load("spider.png").convert_alpha()
backgrounddeath = pygame.image.load("backgrounddeath.png").convert_alpha()

#Window title

pygame.display.set_caption("I wanna be the Mario")

def detectCollisions(x1, y1, w1,h1, x2, y2, w2, h2):                #Collision
    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

# character class
class Sprite:
    def __init__(self,x,y):
        self.x = x
        self.y = y
        self.width = 42
        self.height = 44
        self.velx = 0
        self.vely = 0
        self.image0 = pygame.image.load("char1.png")
        self.image1 = pygame.image.load("char2.png")
        self.timeTarget = 10
        self.timeNumber = 0
        self.currentImage = 0


    def update(self):
        self.timeNumber += 1
        if (self.timeNumber == self.timeTarget):
            if (self.currentImage == 0):
                self.currentImage = 1
            else:
                self.currentImage = 0
            self.timeNumber = 0
        self.render()

    def render(self):
        if (self.currentImage == 0):
            window.blit(self.image0, (self.x, self.y))
        else:
            window.blit(self.image1, (self.x, self.y))


# Floor class
class Floor:
    def __init__(self,x,y):
        self.x = x
        self.y = y
        self.width = 43
        self.height = 44
        self.image0 = pygame.image.load("floor.png")

    def update(self):
        self.render()

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

def floor_spawner(row):
    global floor_group
    for i in range(0,946,43):
        floor_group.add(Floor(i,row))

#Create first level floor        
floor_spawner(519)
floor_spawner(475)

#player
player = Sprite(0,431)

#create our main loop

gameloop = True

while gameloop:

    for event in pygame.event.get():       #get function handles events
        if  (event.type == pygame.QUIT):   #if Quit (red x) pressed exit loop
            gameloop = False

        if (event.type == pygame.KEYDOWN):      #If a key is pressed down

            if (event.key ==pygame.K_LEFT):      #If Left Arrow
                velx = -7

            if (event.key ==pygame.K_RIGHT):     #If Right Arrow
                velx = 7

            if (event.key ==pygame.K_UP):        #If Up Arrow
                vely = -7
            if (event.key ==pygame.K_DOWN):      #If Down Arrow
                vely = 7

        if (event.type == pygame.KEYUP):      #If a key is pressed down

            if (event.key ==pygame.K_LEFT):      #If Left Arrow
                velx = 0
            if (event.key ==pygame.K_RIGHT):     #If Right Arrow
                velx = 0
            if (event.key ==pygame.K_UP):        #If Up Arrow
                vely = 0
            if (event.key ==pygame.K_DOWN):      #If Down Arrow
                vely = 0

    #Death screen
    if health == 0:
        level =5
    if level == 5:
        window.blit(backgrounddeath, (0,0))
        pygame.mixer.music.load('Death.OGG')
        pygame.mixer.music.play(0)


    #Level 0
    if level == 0:
        window.blit(background0, (0,0))    #Bottom bricks
        for f in list(floor_group):
            if detectCollisions(player.x, player.y, player.width, player.height, f.x, f.y, f.width, f.height):
                vely = 0
            f.render()

        if player.x <= 0:                      #Left side collision
            player.x = 0

        #Spike spawn 
        if player.x >= 430:
            window.blit(Spike, (470, 436))
        if detectCollisions(player.x, player.y, player.width, player.height, 470, 436, 43, 40):
            level = 5
            player.y = 999


        if player.x >= 900:                    #Level change
            level = 1
            player.x = 0

    #Level 1
    if level == 1:
        if player.x < 0:                    # Level change
            level = 0
            player.x = 900
        window.blit(background0, (0,0))    #Bottom bricks
        for f in list(floor_group):
            #if detectCollisions(player.x, player.y, player.width, player.height, f.x, f.y, f.width, f.height):

            f.render()

    print(health)
    player.x += velx
    player.y += vely
    player.update()

    clock.tick(50)                         #Tick Tock Tick Tock

    pygame.display.flip()                  #Updates the window

pygame.quit()

0 个答案:

没有答案