python - Pygame Updating Levels Without Overlap -
in library pygame, trying program game. in game, if collide goal rectangle, move onto new level. works when collide goal rectangle, draws new level on top of old level. want clear screen, , draw level. how this? here code:
import os, random, pygame, eztext 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 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 enemy in enemies: if self.rect.colliderect(enemy.rect): player.move(-(self.rect.x - 16), -(self.rect.y - 368)) 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 z in level: 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 z in leveltwo: 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 z in levelthree: 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 z in levelfour: 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) 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)) wall in walls: pygame.draw.rect(screen, (0, 0, 0), wall.rect) pygame.draw.rect(screen, (255, 200, 0), player.rect) enemy in enemies: pygame.draw.rect(screen, (255, 0, 0), enemy.rect) goal in goals: pygame.draw.rect(screen, (0, 0, 255), goal.rect) pygame.display.flip()
when ever beat level, reset walls, enemies , goals lists. in other words, make them = [] before appending new level data them
Comments
Post a Comment