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

为什么我的堆叠游戏数学无法正常工作?

如何解决为什么我的堆叠游戏数学无法正常工作?

这是我的第一个pygame项目,它是一个堆叠游戏。我制作游戏时的想法是将任何一块不接触底座的东西都切掉,并且一直进行到您的块变得太小或到达屏幕顶部为止。我在这里的游戏的问题是,如果您只在右侧堆叠筹码,它就可以工作。但是,如果您开始向左堆叠,它就会分崩离析。我已经多次检查过数学,但仍然无法正常工作。它通过以下方式计算出要切掉的部分:在底部获得工件的基本x位置,然后在顶部减去或减去该工件的x位置。

import pygame
import time
pygame.init()
win=pygame.display.set_mode((800,800))
pygame.display.set_caption("Stacking Game")
length=200 #The length of you CURRENT piece,the one that is moving
base=0 #X pos of the PREVIoUS stacked piece
x=0 #X pos of current piece
y=750 #y pos of current piece
blocks=0 #How many blocks have been stacked
difference=0
speed=5
direction=1
running=1
lengthbase=0 #Length of the PREVIoUS stacked piece
while (running==1):
    pygame.time.delay(10)
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            pygame.quit()
    if direction==1: 
        if x>800-length:
            direction=0
        else:
            x+=speed
    else:
        if direction==0:
            if x<0:
                direction=1
            else:
                x-=speed
    pygame.draw.rect(win,(0),(0,800,y+50)) #Blacks out the screen from the current piece to the top,leaving prevIoUsly stacked pieces
    pygame.draw.rect(win,(255,0),(x,y,length,50))
    if blocks==16: #16 is the maximum number of blocks on the screen
        pygame.quit()
        print("You win!")
    if event.type==pygame.KEYDOWN and event.key==pygame.K_SPACE:
        if blocks==0: #Since the math is all based off the prevIoUs piece's length and position,this inserts the information of the first piece. We Now kNow the position and length of the first stacked piece
            base=x
            lengthbase=200
        else:
            if x>base and x<(base+lengthbase): #The current piece should always be the same size and the prevIoUs piece,so it can either be stacked perfectly,to the right,or to the left. This calulates data when it is stacked to the right.
                difference=(x+length)-(base+lengthbase)
                length=length-difference
                pygame.draw.rect(win,y+50))
                pygame.draw.rect(win,50)) #Draws the new piece at the same position as the current piece,but with the new length
            else:
                if (x+length)>base and (x+length)<(base+lengthbase): #Calculates if the piece is stacked to the left.
                    difference=base-x
                    length=length-difference
                    pygame.draw.rect(win,y+50))
                    pygame.draw.rect(win,(x+difference,50)) #If it was drawn on x,the stacked piece and the one below it would not be aligned. It has to move a distance of difference
                else:
                    pygame.quit()
                    print("You lost.")
        base=x #The stacked piece then becomes the bases for the next piece
        lengthbase=length
        speed+=1 
        blocks+=1
        y-=50
        time.sleep(0.5)
    pygame.display.update()

解决方法

您必须评估[x1,x1 + w1]和[x2,x2 + w2]范围是否重叠。

不重叠:

x1      x1+w1
  +----+
            +----+
          x2      x2+w2
           x1      x1+w1
             +----+
  +----+
x2      x2+w2

重叠

x1                x1+w1
  +--------------+
       +----+
     x2      x2+w2
     x1      x1+w1
       +----+
  +---------------+
x2                 x2+w2
x1           x1+w1
  +---------+
       +----------+
     x2            x2+w2
     x1            x1+w1
       +----------+
  +----------+
x2            x2+w2

这意味着,如果范围重叠,则

x1 < x2+w2 and x2 < x1+w1

您不需要左右两个单独的箱子。只需评估区域是否重叠即可。用minmax计算公共区域的开始和结束:

if x < base + lengthbase and base < x + length:
    start = max(x,base)
    end = min(x+length,base+lengthbase)
    x = start
    length = end - start 
    pygame.draw.rect(win,(0),(0,800,y+50))
    pygame.draw.rect(win,(255,0),(x,y,length,50))
else:
    pygame.quit()
    print("You lost.")

这可以进一步简化:

start = max(x,base)
end = min(x+length,base+lengthbase)
if start < end:  
    x,length = start,end - start
    pygame.draw.rect(win,50))
else:
    # [...]

侧面说明:您必须在事件循环中处理事件,而不是在事件循环后处理:

while running==1:
    pygame.time.delay(10)
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            pygame.quit()

        if event.type==pygame.KEYDOWN and event.key==pygame.K_SPACE:
            if blocks==0:
                base=x
                lengthbase=200
            else:
                start = max(x,base)
                end = min(x+length,base+lengthbase)
                if start < end:  
                    x,end - start
                    pygame.draw.rect(win,y+50))
                    pygame.draw.rect(win,50))
                else:
                    pygame.quit()
                    print("You lost.")
            base,lengthbase = x,length 
            speed += 1 
            blocks += 1
            y -= 50
            time.sleep(0.5)

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