Pygame在没有重叠的情况下更新级别

时间:2015-03-17 02:18:17

标签: python pygame pygame-surface

在图书馆Pygame中,我正在尝试编写游戏。在游戏中,如果您与目标矩形发生碰撞,则会进入新的水平。一切正常但是当你与目标矩形发生碰撞时,它会在旧级别的顶层上绘制新级别。我希望它清除屏幕,然后绘制水平。我该怎么做?这是我的代码:

import os, random, pygame, eztext
from time import *
speed = 1
gravityNum = 1
gravity = True
keyDown = True
rightNum = 0
running = True
pygame.init()
white = (255, 255, 255)
screen = pygame.display.set_mode((880, 400))
class Player(object):
    def __init__(self):
        self.rect = pygame.Rect(16, 368, 16, 16)
        self.level = 1
    def move(self, dx, dy):
        if dx != 0:
            self.move2(dx, 0)
        if dy != 0:
            self.move2(0, dy)
    def move2(self, dx, dy):
        self.rect.x += dx
        self.rect.y += dy
        for wall in walls:
            if self.rect.colliderect(wall.rect):
                if dx > 0:
                    self.rect.right = wall.rect.left
                if dx < 0:
                    self.rect.left = wall.rect.right
                if dy > 0:
                    self.rect.bottom = wall.rect.top
                if dy < 0:
                        self.rect.top = wall.rect.bottom
        for enemy in enemies:
            if self.rect.colliderect(enemy.rect):
                player.move(-(self.rect.x - 16), -(self.rect.y - 368))
        for goal in goals:
            if self.rect.colliderect(goal.rect):
                player.move(-(self.rect.x - 16), -(self.rect.y - 368))
                self.level += 1
                self.levelCheck()
    def drawLevel1(self):
        x = 0
        y = 0
        for z in level:
            for col in z:
                if col == "W":
                    Wall((x, y))
                if col == "E":
                    Enemy((x, y))
                if col == "G":
                    Goal((x, y))
                x += 16
            y += 16
            x = 0
    def drawLevel2(self):
        x = 0
        y = 0
        for z in levelTwo:
            for col in z:
                if col == "W":
                    Wall((x, y))
                if col == "E":
                    Enemy((x, y))
                if col == "G":
                    Goal((x, y))
                x += 16
            y += 16
            x = 0
    def drawLevel3(self):
        x = 0
        y = 0
        for z in levelThree:
            for col in z:
                if col == "W":
                    Wall((x, y))
                if col == "E":
                    Enemy((x, y))
                if col == "G":
                    Goal((x, y))
                x += 16
            y += 16
            x = 0
    def drawLevel4(self):
        x = 0
        y = 0
        for z in levelFour:
            for col in z:
                if col == "W":
                    Wall((x, y))
                if col == "E":
                    Enemy((x, y))
                if col == "G":
                    Goal((x, y))
                x += 16
            y += 16
            x = 0        
    def levelCheck(self):
        if self.level == 1:
            self.drawLevel1()
        if self.level == 2:
            pygame.display.flip()
            self.drawLevel2()
        if self.level == 3:
            self.drawLevel3()
        if self.level == 4:
            self.drawLevel4()

class Wall(object):
    def __init__(self, pos):
        walls.append(self)
        self.rect = pygame.Rect(pos[0], pos[1], 16, 16)
class Enemy(object):
    def __init__(self, pos):
        enemies.append(self)
        self.rect = pygame.Rect(pos[0], pos[1], 16, 16)
class Goal(object):
    def __init__(self, pos):
        goals.append(self)
        self.rect = pygame.Rect(pos[0], pos[1], 16, 16)

pygame.display.set_caption("Game")
display = pygame.Surface((880, 400))
goals = []
walls = []
levelNumber = 1
enemies = []
player = Player()
level = [
"WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
"W                                                 GGGGW",
"W                                                 GGGGW",
"W                                                 GGGGW",
"W                          EEE                        W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                         WWW   WWW                   W",
"W                         WWW   WWW                   W",
"WWWWWWWW                  WWW   WWW                   W",
"W       W                 WWW   WWW                   W",
"W        W                                            W",
"E   W     W               WWW   WWW                   W",
"E    W     W                                          W",
"E     W     W                                         W",
"WW     W     W                                        W",
"WWW     W     WW                                      W",
"WWWW     WW    W                                      W",
"WWWWW     W    W                                      W",
"W       G  W                                          W",
"WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
]

levelTwo = [
"WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
"W                                                 GGGGW",
"W                                                 GGGGW",
"W                                                 GGGGW",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
]

levelThree = [
"WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
"W                                                 GGGGW",
"W                                                 GGGGW",
"W                                                 GGGGW",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
]
levelFour = [
"WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
"W                                                 GGGGW",
"W                                                 GGGGW",
"W                                                 GGGGW",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"W                                                     W",
"WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
]
player.levelCheck()
while running:
    player.move(rightNum, 0)
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_DOWN:
                gravityNum = 1
            if event.key == pygame.K_UP:
                gravityNum = -1
            if event.key == pygame.K_RIGHT:
                rightNum = 1
            if event.key == pygame.K_LEFT:
                rightNum = -1

    if gravity == True:
        player.move(0, gravityNum)
    elif gravity == False:
        player.move(0, -gravityNum)
    screen.fill((255, 255, 255))
    for wall in walls:
        pygame.draw.rect(screen, (0, 0, 0), wall.rect)
        pygame.draw.rect(screen, (255, 200, 0), player.rect)
    for enemy in enemies:
        pygame.draw.rect(screen, (255, 0, 0), enemy.rect)
    for goal in goals:
        pygame.draw.rect(screen, (0, 0, 255), goal.rect)

    pygame.display.flip()

1 个答案:

答案 0 :(得分:0)

当你击败关卡时,重置你的墙壁,敌人和目标列表。 换句话说,在将所有新级别数据附加到它们之前使它们= []