import pygame, os, time pygame.init() whattoprint_Youwin = "" keycount = "Keys: " thekeycounter = "0" gateopen = False whattoprint_TheKeys = "Get the key, save the world. It is yellow" whattoprint_instructions_1 = "Gray squares are shields against bombs" whattoprint_instructions_2 = "Blue squares are bombs and need to be shielded against" class Player(object): def __init__(self): self.rect = pygame.Rect(32, 32, 16, 16) # this little function checks along which axis the player is moving def move(self, dx, dy): # Move each axis separately. Note that this checks for collisions both times. if dx != 0: self.move_single_axis(dx, 0) if dy != 0: self.move_single_axis(0, dy) def move_single_axis(self, dx, dy): # Move the rectangle: New coordinates are coming in so # they are added to the current position. self.rect.x += dx self.rect.y += dy # Now this is a test to see if the orange dude collides with the walls. # The loop goes through all the wall elements and checks for all possible # sides of the orange dude. If you collide with a wall you can go no # further in that direction for wall in walls: if self.rect.colliderect(wall.rect): if dx > 0: # Moving right; Hit the left side of the wall self.rect.right = wall.rect.left if dx < 0: # Moving left; Hit the right side of the wall self.rect.left = wall.rect.right if dy > 0: # Moving down; Hit the top side of the wall self.rect.bottom = wall.rect.top if dy < 0: # Moving up; Hit the bottom side of the wall self.rect.top = wall.rect.bottom # The walls(wall) is made up of rectangle elements class Wall(object): def __init__(self, pos): self.rect = pygame.Rect(pos[0], pos[1], 16, 16) # This class represents the Gate through which the player needs to go # to escape the maze. We could do without having a while class for the # gate but we simply can't resist. class Gate(object): def __init__(self,pos): self.rect = pygame.Rect(pos[0], pos[1], 16, 16) # This class represents an armour or a cloak that are situated in the # maze and the player(orange dude) should try to get as many of those as # he can because if he collects one he is immune to one bomb. class Armour(object): def __init__(self,pos): self.rect = pygame.Rect(pos[0], pos[1], 16, 16) class Bomb(object): def __init__(self,pos): self.rect = pygame.Rect(pos[0], pos[1], 16, 16) class TheKey(object): def __init__(self,pos): self.rect = pygame.Rect(pos[0], pos[1], 16, 16) # Initialize os.environ["SDL_VIDEO_CENTERED"] = "1" pygame.init() # Set up the display pygame.display.set_caption("Get to the red square!") screen = pygame.display.set_mode((640, 640)) clock = pygame.time.Clock() Thekey = [] # List to hold keys gates = [] # List to hold the Gates walls = [] # List to hold the walls armours = [] # List to hold the armour bombs = [] # List to hold the bombs player = Player() # Create the player # Holds the level layout in a list of strings. level = [ "WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW", "W BAW W W", "W WWWWW W WWWWBWW W", "W WWWW W W W W", "W W WWWW WWW WWW W", "W WWWW WWWW A M W W W", "W AW W W W WWWWW", "W WW W WWW W W B W", "W WWW WWW W W W W W WW", "W W W W W W W", "WWWBWWWWW WWWWW W W W WWW", "W W W B W B W", "WBW WWWW WWW W W W", "WKB WA AW B W GW", "WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW", ] # Parse the level string above. W = wall,A = armour, B = bomb, E = exit x = y = 0 for row in level: for col in row: if col == "W": walls.append(Wall((x, y))) if col == "G": gate = Gate((x, y)) if col == "B": bombs.append(Wall((x, y))) if col == "A": armours.append(Wall((x, y))) if col == "K": thekey = TheKey((x,y)) x += 16 y += 16 x = 0 running = True while running: clock.tick(60) if player.rect.colliderect(thekey.rect): gateopen = True whattoprint_TheKeys = "You found the key!" if player.rect.colliderect(gate.rect) and gateopen == True: whattoprint_Youwin = "You win!" if player.rect.colliderect(gate.rect) and gateopen == False: whattoprint_TheKeys = "You need a key!" for e in pygame.event.get(): if e.type == pygame.QUIT: running = False if e.type == pygame.KEYDOWN and e.key == pygame.K_ESCAPE: running = False # Move the player if an arrow key is pressed key = pygame.key.get_pressed() if key[pygame.K_LEFT]: player.move(-16, 0) time.sleep(0.2) if key[pygame.K_RIGHT]: player.move(16, 0) time.sleep(0.2) if key[pygame.K_UP]: player.move(0, -16) time.sleep(0.2) if key[pygame.K_DOWN]: player.move(0, 16) time.sleep(0.2) # Draw the scene screen.fill((0, 0, 0)) for wall in walls: pygame.draw.rect(screen, (255, 255, 255), wall.rect) for bomb in bombs: pygame.draw.rect(screen, (0, 0, 255), bomb.rect) for armour in armours: pygame.draw.rect(screen, (100, 100, 100), armour.rect) for gate in gates: pygame.draw.rect(screen, (255,0,0), gate.rect) #for Keys in keys: #pygame.draw.reason(screen, (255,255,0), Keys.rect) pygame.draw.rect(screen,(255,255,0), thekey.rect) pygame.draw.rect(screen, (255, 0, 0), gate.rect) pygame.draw.rect(screen, (255, 200, 0), player.rect) #whattoprint_Youwin = "" my_font = pygame.font.SysFont("Comic Sans MS", 30) pygame.Rect(30, 30, 60, 60) sometextonscreen_k = my_font.render(whattoprint_TheKeys, 1, (255,255,0)) moretextonscreen_i = my_font.render(whattoprint_instructions_1, 1, (255,255,0)) moretextonscreen_i2 = my_font.render(whattoprint_instructions_2, 1, (255,255,0)) moretextonscreen_YW = my_font.render(whattoprint_Youwin, 1, (255,0,255)) screen.blit(sometextonscreen_k, (47, 300)) screen.blit(moretextonscreen_i, (47, 340)) screen.blit(moretextonscreen_i2, (47, 370)) screen.blit(moretextonscreen_YW, (47, 450)) pygame.display.flip()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question