问题描述
过去 3 天我一直在试图理解这一点,但是对于我阅读的每一篇文章和我观看的每一个 YouTube 视频,我都未能理解这个概念。在问这里之前,我已经尽力听取和阅读我在网上看到的内容。
我只想让一个矩形上一个斜坡。我不是为了这个功能。我观看的每个教程/文章/视频都解释了它们的函数/类是如何工作的。有些使用类和函数的列表来调用其中的其他函数。它变得非常抽象,现在对我来说太多了。
看来,我需要有人真正为我安下心来。没有函数和类。我只想将一个矩形向上移动一个 45 度的斜坡。
一个 YouTube 视频解释说我需要 y = mx + b
。但是没有说明如何使用这个功能。因此,如果有人有一个基本 45 度斜率的公式,并且可以在我自己的代码中向我展示如何在没有函数或类的情况下使用它,我将不胜感激。
在我理解之后,我可以制作自己的函数和类来实现它。我对此的尝试是在斜坡图像后面制作一个矩形。这是错误的吗?我应该使用一行吗?
我对代码的尝试:
import pygame
import sys
pygame.init()
clock = pygame.time.Clock()
screensize = (800,600)
screen = pygame.display.set_mode(screensize)
colour = [(0,0),(255,255,255),(114,216,242),(200,200,200),199,241),(50,50,50)]
# black 0 #red 1 #white 2 #light_blue 3 #gray 4 #light_pink 5 #dark_gray 6
moving_up = False
moving_down = False
moving_left = False
moving_right = False
gravity = 5
def collide(rect,rectangle_list):
hit_list = []
for rectangle in rectangle_list:
if rect.colliderect(rectangle):
hit_list.append(rectangle)
return hit_list
def move(rect,movement,tiles):
collision_types = {'top': False,'bottom': False,'right': False,'left': False}
rect.x += movement[0]
hit_list = collide(rect,tiles)
for tile in hit_list:
if movement[0] > 0:
rect.right = tile.left
collision_types['right'] = True
elif movement[0] < 0:
rect.left = tile.right
collision_types['left'] = True
rect.y += movement[1]
hit_list = collide(rect,tiles)
for tile in hit_list:
if movement[1] > 0:
rect.bottom = tile.top
collision_types['bottom'] = True
elif movement[1] < 0:
rect.top = tile.bottom
collision_types['top'] = True
return rect,collision_types
player_rect = pygame.Rect(100,51,50)
while True:
screen.fill(colour[6])
pygame.draw.polygon(screen,[(300,500),(300,550),(250,550)])
# ~~~~~~ List of rectangles
rectlist = []
small_rect = pygame.Rect(200,50)
bottomrect_1 = pygame.Rect(0,screensize[1] - 50,screensize[0],50)
bottomrect_2 = pygame.Rect(300,screensize[1] - 100,50)
D_rect = pygame.Rect(250,50)
rectlist.append(small_rect)
rectlist.append(bottomrect_1)
rectlist.append(bottomrect_2)
# ~~~~~~ Player Movement
player_movement = [0,0]
if moving_up:
player_movement[1] -= 2
if moving_down:
player_movement[1] += 2
if moving_left:
player_movement[0] -= 2
if moving_right:
player_movement[0] += 2
player_movement[1] += gravity
# ~~~~~~ Player Collision
my_rect,collision = move(player_rect,player_movement,rectlist)
# my attempt at this diagonal humbug. D_rect is the diagonal rect. (the slope)
if my_rect.colliderect(D_rect):
yy = D_rect.height + (my_rect.x - D_rect.x) * 1
my_rect.y -= yy/3.6
# ~~~~~~ Event Loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
if event.key == pygame.K_w:
moving_up = True
if event.key == pygame.K_s:
moving_down = True
if event.key == pygame.K_a:
moving_left = True
if event.key == pygame.K_d:
moving_right = True
if event.key == pygame.K_SPACE:
gravity = -gravity
if event.type == pygame.KEYUP:
if event.key == pygame.K_w:
moving_up = False
if event.key == pygame.K_s:
moving_down = False
if event.key == pygame.K_a:
moving_left = False
if event.key == pygame.K_d:
moving_right = False
# ~~~~~~ Draw.
pygame.draw.rect(screen,(0,125),D_rect,1)
pygame.draw.rect(screen,colour[3],small_rect)
pygame.draw.rect(screen,colour[4],my_rect)
pygame.draw.rect(screen,colour[5],bottomrect_1)
pygame.draw.rect(screen,bottomrect_2)
pygame.display.update()
clock.tick(60)
解决方法
计算移动矩形(my_rect.right
)右边缘对角矩形的高度:
dia_height = D_rect.height * (my_rect.right-D_rect.left) / D_rect.width
计算移动矩形右边缘的对角矩形的顶部:
D_rect_top = D_rect.bottom - round(dia_height)
移动矩形右边缘的对角矩形的顶部是移动矩形的底部:
my_rect.bottom = D_rect_top
如果对角线方向相反,则在左边缘 (my_rect.left
) 而不是右边缘 (my_rect.right
) 上找到对角线的高度。
公式 y = mx + b 隐藏在这段代码中:
x 是 my_rect.right - D_rect.left
m 是 -D_rect.height / D_rect.width
b 是 D_rect.bottom
y 是 my_rect.bottom
while True:
# [...]
if my_rect.colliderect(D_rect):
dia_height = D_rect.height * (my_rect.right-D_rect.left) / D_rect.width
D_rect_top = D_rect.bottom - round(dia_height)
my_rect.bottom = D_rect_top
# [....]