python 为什么mu square不在我制造的障碍前停下来?

kg7wmglp  于 2022-11-21  发布在  Python
关注(0)|答案(1)|浏览(82)
import pygame
import keyboard

screen = pygame.display.set_mode((800,600))
screen.fill((255,255,255))
blue = (20,40,200)
gray = (100,100,100)
x=200
y=200
w=60
h=60
p=0
OL=0
vel=0.1
#variables
def player():
    pygame.draw.rect(screen,blue,pygame.Rect(p+x,p+y,p+w,p+h))
def obst():
    pygame.draw.rect(screen,gray,pygame.Rect(OL+100,OL+100,OL+100,OL+10))
run=True
pygame.init()

while run:
    screen.fill((255,255,255))
    player()
    obst()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT] and x>0 and x>OL:
        x-= vel
    elif x==OL:
        vel=0
    if keys[pygame.K_RIGHT]and x<800-w and x>OL:
        x+= vel
    elif x==OL:
        vel=0
    if keys[pygame.K_UP]and y>0 and y>OL:
        y-= vel
    elif y==OL:
        vel=0
    if keys[pygame.K_DOWN]and y<600-h and y>OL:
        y+= vel
    elif y==OL:
        vel=0
    pygame.display.update()
pygame.quit()

我试着把变量放在我的obstalce的坐标,它只是不工作,我迷路了。有没有一个整体很多,我可以做我的一点或没有知识的python。我只是想我的广场停止时,它击中障碍物,这是很难让它做到这一点,我尝试了我所知道的一切,请帮助。把OL放在obstcle坐标似乎没有任何帮助,我只想让我做的所有形状都被屏蔽为“OL”,这样我就可以有复杂的障碍物或移动的障碍物来阻止玩家。但我也希望玩家能够远离障碍物。

68bkxrlz

68bkxrlz1#

读取 * How do I detect collision in pygame?,我建议使用pygame.Rect对象和colliderect进行碰撞检测,为玩家和障碍物创建pygame.Rect对象:

player_rect = pygame.Rect(200, 200, 60, 60)
obstacle_rect = pygame.Rect(100, 100, 100, 10)

当玩家移动并将玩家的边界限制在障碍物的边界时,测试碰撞:

if keys[pygame.K_LEFT] and player_rect.left > 0:
    player_rect.x -= vel
    if player_rect.colliderect(obstacle_rect):
        player_rect.left = obstacle_rect.right

完整示例:

import pygame

pygame.init()
screen = pygame.display.set_mode((800,600))
clock = pygame.time.Clock()
blue = (20,40,200)
gray = (100,100,100)
player_rect = pygame.Rect(200, 200, 60, 60)
obstacle_rect = pygame.Rect(100, 100, 100, 10)
vel = 5

def player():
    pygame.draw.rect(screen, blue, player_rect)
def obst():
    pygame.draw.rect(screen, gray, pygame.Rect(obstacle_rect))

run=True
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    keys = pygame.key.get_pressed()
    
    if keys[pygame.K_LEFT] and player_rect.left > 0:
        player_rect.x -= vel
        if player_rect.colliderect(obstacle_rect):
            player_rect.left = obstacle_rect.right
    if keys[pygame.K_RIGHT] and player_rect.right < 800:
        player_rect.x += vel
        if player_rect.colliderect(obstacle_rect):
            player_rect.right = obstacle_rect.left
    if keys[pygame.K_UP] and player_rect.top > 0:
        player_rect.y -= vel
        if player_rect.colliderect(obstacle_rect):
            player_rect.top = obstacle_rect.bottom
    if keys[pygame.K_DOWN] and player_rect.bottom < 600:
        player_rect.y += vel
        if player_rect.colliderect(obstacle_rect):
            player_rect.bottom = obstacle_rect.top

    screen.fill((255,255,255))
    player()
    obst()
    pygame.display.update()
pygame.quit()

相关问题