本文介绍了我怎么才能撞上45度的斜坡呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在过去的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, 0), (255, 0, 0), (255, 255, 255), (114, 216, 242), (200, 200, 200), (255, 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, 50)
while True:
screen.fill(colour[6])
pygame.draw.polygon(screen, (255, 0, 255), [(300, 500), (300, 550), (250, 550)])
# ~~~~~~ List of rectangles
rectlist = []
small_rect = pygame.Rect(200, 200, 50, 50)
bottomrect_1 = pygame.Rect(0, screensize[1] - 50, screensize[0], 50)
bottomrect_2 = pygame.Rect(300, screensize[1] - 100, screensize[0], 50)
D_rect = pygame.Rect(250, screensize[1] - 100, 50, 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, 255, 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, colour[5], 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
# [....]
这篇关于我怎么才能撞上45度的斜坡呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!