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

为什么在实现滚动时“移动 5 - 如果触摸墙壁 - 向后移动 5”逻辑不起作用?

如何解决为什么在实现滚动时“移动 5 - 如果触摸墙壁 - 向后移动 5”逻辑不起作用?

我知道这种墙壁检测逻辑在大多数游戏中都可以正常工作,但我想要更多的乐趣。所以,我实现了滚动,玩家总是在中间,而不是背景移动。

keys=pygame.key.get_pressed()
if keys[pygame.K_w]:
    y-=5
    if rect.colliderect(wall):
        y+=5

我的滚动方式是所有对象都是相对于变量 scrollxscrolly 绘制的。当按键被按下时,scrollxscrolly 会发生变化。然而,当碰撞真正发生时,玩家似乎卡在了物体中。这是完整的代码

import pygame
import random
pygame.init()
win_height=600
win_width=600
win=pygame.display.set_mode((win_width,win_height))
pygame.display.set_caption("YourCraft")
white=(255,255,255)
black=(0,0)
green=(0,0)
clock=pygame.time.Clock()
debris_list=[]

player=pygame.Rect(win_width/2,win_height/2,20,20)
scrolly=0
scrollx=0

size=1000
speed=5

class debris_class():
    def __init__(self):
        self.x=random.randrange(0,size-20)
        self.y=random.randrange(0,size-20)
        self.rect=pygame.Rect(self.x,self.y,20)
    def update(self):
        pygame.draw.rect(win,white,self.rect)
        self.rect.y=scrolly+self.y
        self.rect.x=scrollx+self.x

for i in range(10):
    debris=debris_class()
    debris_list.append(debris)

def collision():
    for i in debris_list:
        if i.rect.colliderect(player):
            return 1
    return 0

while True:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            pygame.quit()

    key=pygame.key.get_pressed()
    if key[pygame.K_s]:
        scrolly-=speed
        collide=collision()
        if collide==1:
            scrolly+=speed

    if key[pygame.K_w]:
        scrolly+=speed
        collide=collision()
        if collide==1:
            scrolly-=speed

    if key[pygame.K_d]:
        scrollx-=speed
        collide=collision()
        if collide==1:
            scrollx+=speed

    if key[pygame.K_a]:
        scrollx+=speed
        collide=collision()
        if collide==1:
            scrollx-=speed


    win.fill(black)
    pygame.draw.rect(win,green,(scrollx,scrolly,size,size)) #background
    pygame.draw.rect(win,player)

    for i in debris_list:
        i.update()

    pygame.display.update()

解决方法

您过早地执行碰撞测试。运行碰撞测试时,对象和地图尚未滚动。

在类 update 中将 drawdebris_class 分开:

class debris_class():
    def __init__(self):
        self.x=random.randrange(0,size-20)
        self.y=random.randrange(0,size-20)
        self.rect=pygame.Rect(self.x,self.y,20,20)
    def update(self):
        self.rect.y=scrolly+self.y
        self.rect.x=scrollx+self.x
    def draw(self):
        pygame.draw.rect(win,white,self.rect)

根据按下的键和速度获取运动:

key=pygame.key.get_pressed()
move_x = (key[pygame.K_d] - key[pygame.K_a]) * speed
move_y = (key[pygame.K_s] - key[pygame.K_w]) * speed

复制plyer并移动播放器:

player_copy = player.copy()
player.x += move_x
player.y += move_y

执行碰撞测试并撤销玩家的移动:

collide = collision()
player = player_copy

如果没有检测到碰撞,滚动地图和对象:

if collide == 0:
    scrollx -= move_x
    scrolly -= move_y
    for i in debris_list:
        i.update()     

在循环中绘制对象:

for i in debris_list:
    i.draw()

完整代码:

import pygame
import random

pygame.init()
win_height=600
win_width=600
win=pygame.display.set_mode((win_width,win_height))
pygame.display.set_caption("YourCraft")
white=(255,255,255)
black=(0,0)
green=(0,0)
clock=pygame.time.Clock()
debris_list=[]

player=pygame.Rect(win_width/2,win_height/2,20)
scrolly=0
scrollx=0

size=1000
speed=5

class debris_class():
    def __init__(self):
        self.x=random.randrange(0,self.rect)

for i in range(10):
    debris=debris_class()
    debris_list.append(debris)

def collision():
    for i in debris_list:
        if i.rect.colliderect(player):
            return 1
    return 0

while True:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            pygame.quit()

    key=pygame.key.get_pressed()
    move_x = (key[pygame.K_d] - key[pygame.K_a]) * speed
    move_y = (key[pygame.K_s] - key[pygame.K_w]) * speed
    
    player_copy = player.copy()
    player.x += move_x
    player.y += move_y

    collide = collision()
    player = player_copy
    if collide == 0:
        scrollx -= move_x
        scrolly -= move_y
        for i in debris_list:
            i.update()
           
    win.fill(black)
    pygame.draw.rect(win,green,(scrollx,scrolly,size,size)) #background
    pygame.draw.rect(win,player)
    for i in debris_list:
        i.draw()
    pygame.display.update()

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