问题
I'm making a hide and seek/maze game. I'v bean working in the maze but when i move it sometimes skips past it even though i made conditions. I haven't added the other player yet, right know i'm trying to stop the first one from going through walls, but I can't get it to work. I'm using pygame here is my code:
import pygame
import random
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
Svel = 1
x = 225
y = 225
W1X = 50
W1Y = 50
W2X = 200
W2Y = 50
W3X = 250
W3Y = 200
W4X = 50
W4Y = 200
W5X = 250
W5Y = 100
W6X = 300
W6Y = 250
W7X = 350
W7Y = 225
W8X = 75
W8Y = 325
W9X = 25
W9Y = 150
W10X = 100
W10Y = 200
running = True
while running:
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
x -= Svel
if x <= 0:
x = 0
if red.colliderect(wall1):
if x >= W1X:
x += Svel
if x <= W1X:
x -= Svel
if y >= W1Y:
y += Svel
if y <= W1Y:
y -= Svel
if red.colliderect(wall2):
if x >= W2X:
x += Svel
if x <= W2X:
x -= Svel
if y >= W2Y:
y += Svel
if y <= W2Y:
y -= Svel
if red.colliderect(wall3):
if x >= W3X:
x += Svel
if x <= W3X:
x -= Svel
if y >= W3Y:
y += Svel
if y <= W3Y:
y -= Svel
if red.colliderect(wall4):
if x >= W4X:
x += Svel
if x <= W4X:
x -= Svel
if y >= W4Y:
y += Svel
if y <= W4Y:
y -= Svel
if red.colliderect(wall5):
if x >= W5X:
x += Svel
if x <= W5X:
x -= Svel
if y >= W5Y:
y += Svel
if y <= W5Y:
y -= Svel
if red.colliderect(wall6):
if x >= W6X:
x += Svel
if x <= W6X:
x -= Svel
if y >= W6Y:
y += Svel
if y <= W6Y:
y -= Svel
if red.colliderect(wall7):
if x >= W7X:
x += Svel
if x <= W7X:
x -= Svel
if y >= W7Y:
y += Svel
if y <= W7Y:
y -= Svel
if red.colliderect(wall8):
if x >= W8X:
x += Svel
if x <= W8X:
x -= Svel
if y >= W8Y:
y += Svel
if y <= W8Y:
y -= Svel
if red.colliderect(wall9):
if x >= W9X:
x += Svel
if x <= W9X:
x -= Svel
if y >= W9Y:
y += Svel
if y <= W9Y:
y -= Svel
if red.colliderect(wall10):
if x >= W10X:
x += Svel
if x <= W10X:
x -= Svel
if y >= W10Y:
y += Svel
if y <= W10Y:
y -= Svel
if keys[pygame.K_RIGHT]:
x += Svel
if x >= 1341:
x = 1341
if red.colliderect(wall1):
if x >= W1X:
x += Svel
if x <= W1X:
x -= Svel
if y >= W1Y:
y += Svel
if y <= W1Y:
y -= Svel
if red.colliderect(wall2):
if x >= W2X:
x += Svel
if x <= W2X:
x -= Svel
if y >= W2Y:
y += Svel
if y <= W2Y:
y -= Svel
if red.colliderect(wall3):
if x >= W3X:
x += Svel
if x <= W3X:
x -= Svel
if y >= W3Y:
y += Svel
if y <= W3Y:
y -= Svel
if red.colliderect(wall4):
if x >= W4X:
x += Svel
if x <= W4X:
x -= Svel
if y >= W4Y:
y += Svel
if y <= W4Y:
y -= Svel
if red.colliderect(wall5):
if x >= W5X:
x += Svel
if x <= W5X:
x -= Svel
if y >= W5Y:
y += Svel
if y <= W5Y:
y -= Svel
if red.colliderect(wall6):
if x >= W6X:
x += Svel
if x <= W6X:
x -= Svel
if y >= W6Y:
y += Svel
if y <= W6Y:
y -= Svel
if red.colliderect(wall7):
if x >= W7X:
x += Svel
if x <= W7X:
x -= Svel
if y >= W7Y:
y += Svel
if y <= W7Y:
y -= Svel
if red.colliderect(wall8):
if x >= W8X:
x += Svel
if x <= W8X:
x -= Svel
if y >= W8Y:
y += Svel
if y <= W8Y:
y -= Svel
if red.colliderect(wall9):
if x >= W9X:
x += Svel
if x <= W9X:
x -= Svel
if y >= W9Y:
y += Svel
if y <= W9Y:
y -= Svel
if red.colliderect(wall10):
if x >= W10X:
x += Svel
if x <= W10X:
x -= Svel
if y >= W10Y:
y += Svel
if y <= W10Y:
y -= Svel
if keys[pygame.K_UP]:
y -= Svel
if y <= 0:
y = 0
if red.colliderect(wall1):
if x >= W1X:
x += Svel
if x <= W1X:
x -= Svel
if y >= W1Y:
y += Svel
if y <= W1Y:
y -= Svel
if red.colliderect(wall2):
if x >= W2X:
x += Svel
if x <= W2X:
x -= Svel
if y >= W2Y:
y += Svel
if y <= W2Y:
y -= Svel
if red.colliderect(wall3):
if x >= W3X:
x += Svel
if x <= W3X:
x -= Svel
if y >= W3Y:
y += Svel
if y <= W3Y:
y -= Svel
if red.colliderect(wall4):
if x >= W4X:
x += Svel
if x <= W4X:
x -= Svel
if y >= W4Y:
y += Svel
if y <= W4Y:
y -= Svel
if red.colliderect(wall5):
if x >= W5X:
x += Svel
if x <= W5X:
x -= Svel
if y >= W5Y:
y += Svel
if y <= W5Y:
y -= Svel
if red.colliderect(wall6):
if x >= W6X:
x += Svel
if x <= W6X:
x -= Svel
if y >= W6Y:
y += Svel
if y <= W6Y:
y -= Svel
if red.colliderect(wall7):
if x >= W7X:
x += Svel
if x <= W7X:
x -= Svel
if y >= W7Y:
y += Svel
if y <= W7Y:
y -= Svel
if red.colliderect(wall8):
if x >= W8X:
x += Svel
if x <= W8X:
x -= Svel
if y >= W8Y:
y += Svel
if y <= W8Y:
y -= Svel
if red.colliderect(wall9):
if x >= W9X:
x += Svel
if x <= W9X:
x -= Svel
if y >= W9Y:
y += Svel
if y <= W9Y:
y -= Svel
if red.colliderect(wall10):
if x >= W10X:
x += Svel
if x <= W10X:
x -= Svel
if y >= W10Y:
y += Svel
if y <= W10Y:
y -= Svel
if keys[pygame.K_DOWN]:
y += Svel
if y >= 743:
y = 743
if red.colliderect(wall1):
if x >= W1X:
x += Svel
if x <= W1X:
x -= Svel
if y >= W1Y:
y += Svel
if y <= W1Y:
y -= Svel
if red.colliderect(wall2):
if x >= W2X:
x += Svel
if x <= W2X:
x -= Svel
if y >= W2Y:
y += Svel
if y <= W2Y:
y -= Svel
if red.colliderect(wall3):
if x >= W3X:
x += Svel
if x <= W3X:
x -= Svel
if y >= W3Y:
y += Svel
if y <= W3Y:
y -= Svel
if red.colliderect(wall4):
if x >= W4X:
x += Svel
if x <= W4X:
x -= Svel
if y >= W4Y:
y += Svel
if y <= W4Y:
y -= Svel
if red.colliderect(wall5):
if x >= W5X:
x += Svel
if x <= W5X:
x -= Svel
if y >= W5Y:
y += Svel
if y <= W5Y:
y -= Svel
if red.colliderect(wall6):
if x >= W6X:
x += Svel
if x <= W6X:
x -= Svel
if y >= W6Y:
y += Svel
if y <= W6Y:
y -= Svel
if red.colliderect(wall7):
if x >= W7X:
x += Svel
if x <= W7X:
x -= Svel
if y >= W7Y:
y += Svel
if y <= W7Y:
y -= Svel
if red.colliderect(wall8):
if x >= W8X:
x += Svel
if x <= W8X:
x -= Svel
if y >= W8Y:
y += Svel
if y <= W8Y:
y -= Svel
if red.colliderect(wall9):
if x >= W9X:
x += Svel
if x <= W9X:
x -= Svel
if y >= W9Y:
y += Svel
if y <= W9Y:
y -= Svel
if red.colliderect(wall10):
if x >= W10X:
x += Svel
if x <= W10X:
x -= Svel
if y >= W10Y:
y += Svel
if y <= W10Y:
y -= Svel
VX = x-30
VY = y-30
screen.fill([255,255,255])
vision = pygame.draw.rect(screen, [255,255,255], (VX,VY,85,85))
wall1 = pygame.draw.rect(screen, [0,0,0], (W1X,W1Y,200,25))
wall2 = pygame.draw.rect(screen, [0,0,0], (W2X,W2Y,25,250))
wall3 = pygame.draw.rect(screen, [0,0,0], (W3X,W3Y,200,25))
wall4 = pygame.draw.rect(screen, [0,0,0], (W4X,W4Y,25,200))
wall5 = pygame.draw.rect(screen, [0,0,0], (W5X,W5Y,25,200))
wall6 = pygame.draw.rect(screen, [0,0,0], (W6X,W6Y,25,200))
wall7 = pygame.draw.rect(screen, [0,0,0], (W7X,W7Y,25,200))
wall8 = pygame.draw.rect(screen, [0,0,0], (W8X,W8Y,200,25))
wall9 = pygame.draw.rect(screen, [0,0,0], (W9X,W9Y,150,25))
wall10 = pygame.draw.rect(screen, [0,0,0], (W10X,W10Y,75,100))
red = pygame.draw.rect(screen, [255,0,0], (x,y,25,25))
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
pygame.quit()
回答1:
Please learn about lists, loops and pygame.Rect.
For instance:
import pygame
pygame.init()
screen = pygame.display.set_mode((500, 500))
wrects = [(50, 50, 200, 25), (200, 50, 25, 250), (250, 200, 200, 25), (50, 200, 25, 200), (250, 100, 25, 200),
(300, 250, 25, 200), (350, 225, 25, 200), (75, 325, 200, 25), (25, 150, 150, 25), (100, 200, 75, 100)]
walls = [pygame.Rect(r) for r in wrects]
Svel = 1
red = pygame.Rect(225, 225, 25, 25)
running = True
clock = pygame.time.Clock()
while running:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
red.x = max(red.x - Svel, 0)
for wall in walls:
if red.colliderect(wall):
red.left = max(red.left, wall.right)
if keys[pygame.K_RIGHT]:
red.x = min(red.x + Svel, 500)
for wall in walls:
if red.colliderect(wall):
red.right = min(red.right, wall.left)
if keys[pygame.K_UP]:
red.y = max(red.y - Svel, 0)
for wall in walls:
if red.colliderect(wall):
red.top = max(red.top, wall.bottom)
if keys[pygame.K_DOWN]:
red.y = min(red.y + Svel, 500)
for wall in walls:
if red.colliderect(wall):
red.bottom = min(red.bottom, wall.top)
screen.fill([255,255,255])
for wall in walls:
pygame.draw.rect(screen, [0,0,0], wall)
pygame.draw.rect(screen, [255,0,0], red)
pygame.display.flip()
pygame.quit()
来源:https://stackoverflow.com/questions/63853997/hide-and-seek-maze-game-in-pygame-not-working