微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

pygame中的捉迷藏游戏不起作用

如何解决pygame中的捉迷藏游戏不起作用

我正在做一个捉迷藏/迷宫游戏。我在迷宫中工作,但是当我移动时,即使有条件,有时也会跳过它。我还没有添加其他播放器,知道我正试图阻止第一个播放器穿过墙壁,但是我无法使其正常工作。我正在使用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],(VX,VY,85,85))
    wall1 = pygame.draw.rect(screen,[0,0],(W1X,W1Y,200,25))
    wall2 = pygame.draw.rect(screen,(W2X,W2Y,25,250))
    wall3 = pygame.draw.rect(screen,(W3X,W3Y,25))
    wall4 = pygame.draw.rect(screen,(W4X,W4Y,200))
    wall5 = pygame.draw.rect(screen,(W5X,W5Y,200))
    wall6 = pygame.draw.rect(screen,(W6X,W6Y,200))
    wall7 = pygame.draw.rect(screen,(W7X,W7Y,200))
    wall8 = pygame.draw.rect(screen,(W8X,W8Y,25))
    wall9 = pygame.draw.rect(screen,(W9X,W9Y,150,25))
    wall10 = pygame.draw.rect(screen,(W10X,W10Y,75,100))
    red = pygame.draw.rect(screen,(x,y,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()

解决方法

请了解有关listsloopspygame.Rect的信息。

例如:

import pygame
pygame.init()
screen = pygame.display.set_mode((500,500))

wrects = [(50,50,200,25),(200,25,250),(250,(50,200),100,(300,250,(350,225,(75,325,(25,150,(100,75,100)]
walls = [pygame.Rect(r) for r in wrects]
Svel = 1
red = pygame.Rect(225,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],wall)
    pygame.draw.rect(screen,[255,red)
    pygame.display.flip()

pygame.quit()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。