麻烦在pygame中生成级别

时间:2015-08-11 00:02:16

标签: python pygame

我目前遇到了与Pygame一起玩的墙 存储库:https://github.com/tear727/Netse

这是我的代码:

import pygame
from pygame import *
import random

pygame.init()


#############Constants##############

display_width = 800
display_height = 640
SIZE = (display_width, display_height)

black = (0,0,0)
white = (255,255,255)

clock = pygame.time.Clock()
screen = pygame.display.set_mode(SIZE)

background = Surface((32,32))
background.convert()
background.fill(Color('#783131'))

x =  0
y = 0
w = 143
h = 170
platx = 600
platy = 500
platw = 50
plath = 50
platformlst = []

obstacle_x = random.randrange(0, display_width)
obstacle_y = -500
new_x = random.randrange(0, display_width)
new_y = -500
obstacle_speed = 5
obstacle_w = 50
obstacle_h = 50

level = [
    "PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP",
    "P                                          P",
    "P                                          P",
    "P                                          P",
    "P                    PPPPPPPPPPP           P",
    "P                                          P",
    "P                                          P",
    "P                                          P",
    "P    PPPPPPPP                              P",
    "P                                          P",
    "P                          PPPPPPP         P",
    "P                 PPPPPP                   P",
    "P                                          P",
    "P         PPPPPPP                          P",
    "P                                          P",
    "P                     PPPPPP               P",
    "P                                          P",
    "P   PPPPPPPPPPP                            P",
    "P                                          P",
    "P                 PPPPPPPPPPP              P",
    "P                                          P",
    "P                                          P",
    "P                                          P",
    "P                                          P",
    "PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP",]

collisions = 4

pygame.display.set_caption("My game")

char_img = pygame.image.load("yasuo.png")
obstacle_img = pygame.image.load("poro.png")

obstacle_rect = obstacle_img.get_rect()

char_rect = char_img.get_rect()
print char_rect
print obstacle_rect

#################Objects################


class Player:
    def __init__(self, x, y, w, h, player_img):
        self.exists = True
        self.grounded = False
        self.name = "Player 1"
        self.health = 100
        self.x = x
        self.y = y
        self.w = w
        self.h = h
        self.velx = 0
        self.vely = 0
        self.player_img = player_img
        self.move_speed = 5

    def spawn(self):
        return screen.blit(self.player_img, (self.x, self.y, self.w, self.h))

    def loseHealth(self, dmg):
        self.health -= dmg
        return self.health

    def move(self):
        self.x += self.velx
        self.y += self.vely
        if not self.grounded:
            self.y += 7
        self.grounded = False

        return self.x, self.y

    def right(self):
        self.velx = 20  

    def left(self):
        self.velx = -20

    def up(self):
        self.vely = -20

    def collide(self, collisions):  
        if (obstacles.exists and 
        obstacles.x < player.x + player.w and
        obstacles.x + obstacles.w > player.x and
        obstacles.y < player.y + player.h and
        obstacles.h + obstacles.y > player.y):
        obstacles.exists = False
            player.loseHealth(25)
            collisions -= 1
            print obstacles.exists

        if (platforms.platx < self.x + self.w and
        platforms.platx + platforms.platw > self.x and
        platforms.platy < self.y + self.h and
        platforms.plath + platforms.platy > self.y):
                print "Player collision"
                self.grounded = True
        return collisions


class Obstacle:
    def __init__(self, obstacle_x, obstacle_y, ob_img):
        self.exists = True
        self.x = obstacle_x
        self.y = obstacle_y
        self.w = obstacle_w
        self.h = obstacle_h
        self.obstacle_img = ob_img
        self.speed = 10
        self.counter = 0

    def spawn(self):
        self.rect = obstacle_img.get_rect()
        if self.exists == True:
            screen.blit(obstacle_img, (self.x, self.y, self.w, self.h))
            if self.y > display_height:
                self.y = 0 - self.h
                self.x = random.randrange(0, display_width)
        else:
            self.exists = False

    def move(self, move_speed):
        self.y += (move_speed * self.speed)
        return self.y

    def update(self):
        if not self.exists:
            self.exists = True
        return self.exists

    def collide(self):
        if(platforms.platx < self.x + self.w and
    platforms.platx + platforms.platw > self.x and
    platforms.platy < self.y + self.h and
    platforms.plath + platforms.platy > self.y):
#           print "platform collision"
            self.speed = 0


class Platform:
    def __init__(self, platx, platy, platw, plath):
        self.platx = platx
        self.platy = platy
        self.platw = platw
        self.plath = plath

    def draw(self):
        pygame.draw.rect(screen, black, [self.platx, self.platy, self.platw, self.plath])


obstacles = Obstacle(obstacle_x, obstacle_y, obstacle_img)
player = Player(x, y, w, h, char_img)

####################Game Loop###################### 

game_exit = False

while not game_exit:
    clock.tick(60)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_exit = True

        if event.type == pygame.KEYDOWN:    
            print "pressed key"

            if event.key == pygame.K_LEFT:
                print "move left"
                player.left()

            if event.key == pygame.K_RIGHT:
                print "move right"
                player.right()

            if event.key == pygame.K_UP:
                print "move up"
                player.up()

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                player.velx = 0 
            if event.key == pygame.K_UP or pygame.K_DOWN:
                player.vely = 0
        if event.type == pygame.MOUSEBUTTONDOWN:
            print "pressed mouse button"
            player.velx += 20
            player.vely += 20

        if event.type == pygame.MOUSEBUTTONUP:
            print "mouse button up"
            player.velx = 0
            player.vely = 0 




    platforms = Platform(platx, platy, platw, plath)


    for i in range(32):
        for j in range(32):
            screen.blit(background, (i*32, j*32))

    for i in level:
        for j in i:
            if j == "P":
                platformlst.append(platforms)
                platforms.draw()
                player.collide(collisions)
                obstacles.collide()
            platforms.platx += 32
        platforms.platy += 32
        platforms.platx = 0

    obstacles.spawn()
    player.spawn()
    obstacles.move(1)
    if not obstacles.exists and random.randrange(0, display_width): 
        obstacles = Obstacle(new_x, new_y, obstacle_img)
        obstacles.spawn()
    player.move()
    pygame.display.update()

基本上,我正在尝试将生成的列表level设置为platforms。基本上只是块大小为32x32。我有一些水平的部分正在产生,但他们看起来并不像他们开始时的正确位置。根据我一直在研究的内容,或许最好通过将level放入矩阵(列表列表)来做到这一点?我哪里错了?感谢您的回复!

1 个答案:

答案 0 :(得分:0)

发现错误。 platxplaty的值都不等于0.我将它们设为0后,一切正常。