我在做一个捉迷藏/迷宫游戏。我在迷宫里工作,但当我移动它时,它有时会跳过它,即使我创造了条件。我还没有添加另一个玩家,我知道我正试图阻止第一个玩家穿过墙,但我无法让它工作。我正在使用pygame这是我的代码:
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()
请了解lists、loops和^{}
例如:
相关问题 更多 >
编程相关推荐