- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
在 pygame 中计算重力的最佳方法是什么?我基本上只需要它,所以当玩家按下“向上”时,角色会跳跃。到目前为止,这是我的代码(只是一个带有移动的红色 block 的白色屏幕)
import pygame
import random
# Define colors
black = ( 0, 0, 0)
white = ( 255, 255, 255)
green = ( 0, 255, 0)
red = ( 255, 0, 0)
#Classes
class Player(pygame.sprite.Sprite):
def __init__(self, color, width, height):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([width, height])
self.image.fill(color)
self.rect = self.image.get_rect()
def move(self, x_change, y_change):
self.rect.x += x_change
self.rect.y += y_change
#Lists
all_sprites_list = pygame.sprite.Group()
#Spawn player
player = Player(red,16,16)
all_sprites_list.add(player)
player.rect.x = 0
player.rect.y = 484
#Initalize
pygame.init()
#Set the width and height of the screen [width,height]
screen_height = 700
screen_width = 500
size=[screen_height,screen_width]
screen=pygame.display.set_mode(size)
#Name on top tab
pygame.display.set_caption("My Game")
#DONT CHANGE
done = False
clock=pygame.time.Clock()
#MAIN LOOP
while done == False:
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done = True # Quit
if event.type == pygame.KEYUP:
# If it is an arrow key, reset vector back to zero
if event.key == pygame.K_LEFT:
None
keyDown = pygame.key.get_pressed()
if keyDown[pygame.K_RIGHT]:
player.move(3, 0)
if keyDown[pygame.K_LEFT]:
player.move(-3, 0)
if keyDown[pygame.K_DOWN]:
player.move(0, 3)
if keyDown[pygame.K_UP]:
player.move(0,-3)
#If player hits side of screen, do this
if player.rect.x < 0:
player.rect.x = 0
if player.rect.x > 684:
player.rect.x = 684
if player.rect.y < 0:
player.rect.y = 0
if player.rect.y > 484:
player.rect.y = 484
#Clear screen
screen.fill(white)
#Drawing
all_sprites_list.draw(screen)
#FPS Lock
clock.tick(60)
#Update screen
pygame.display.flip()
# Close the window and quit.
pygame.quit()
最佳答案
super 马里奥克隆体通常时间向上移动特定的时间,然后向下移动,直到他撞到固体(地板,或者可能是一只乌龟)。您可能知道,这看起来很不现实。
“跳跃和坠落”的物理公式为:d = (at²)/2
,其中d
为跳跃距离,a
是重力,t
是时间。但是,对于可以调整的简单游戏:)
1
。t
的时间。这让您有时间达到 32 像素:
32 = (t²)/2
t² = 64
t = 8
跳跃的“速度”是v = at
。由于您假设上面的 a=1
,v = t
,即 v = 8
。看起来,您的初始 ypos
需要设置为 speed/2
(可能是因为这是一个近似值..)
speed
添加到您的 y 位置并从 speed
中减去 1现在,从您按下“向上”的那一刻起,在每个游戏刻度中都会发生以下情况(假设 y=0
在开始时):
speed = 8
-> y=4
(因为这是“跳跃 0”)speed = 7
-> y=11
speed = 6
-> y=17
speed = 5
-> y=22
speed = 4
-> y=26
speed = 3
-> y=29
speed = 2
-> y=31
speed = 1
-> y=32
speed = 0
-> y=32
speed = -1
-> y=31
speed = -2
-> y=29
...再次下降到 0(但检查溢出是否低于零)。关于“半速”修复:奇怪.. 数学是这样计算的(你在正确的高度上悬停片刻)但我无法立即理解为什么你不应该从实际速度。
只是想到我的数学成绩不佳的原因。
问题出在我的陈述中,如果您的初始速度——在跳跃开始时——是8
(每个游戏刻度的像素),您最终正好是 8在那个刻度线的“末端”更高的像素。你不会,因为一旦你离开地面,“重力”就会立即起作用。在游戏刻度“结束”时,您的速度已降至 7
;因此,您的平均速度从开始到结束都不是8
,它只是7.5
,并且您最终在 y-pos 上7.5 像素。下一个滴答声也是如此;你的速度仍然以每滴答 1
的速度下降。
所以,在(正确计算!)8 个刻度的总跳跃时间之后,您旅行了
7.5 + 6.5 + 5.5 + 4.5 + 3.5 + 2.5 + 1.5 + 0.5 = 32 pixels
“正确”实现它是可能的,但它在当前仅整数计算中引入了浮点运算,并且需要您在“7.5 像素”的 y 位置绘制 Sprite 。浮点计算可能会遇到舍入和“精确”比较问题,所以这就是为什么最好尽可能避免它的原因——对于这种简单的物理学来说当然如此。
关于python - 在pygame中计算重力/跳跃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20940852/
滚动时,我的移动设备底部的固定元素出现问题。看起来每次滚动时都会重新计算高度,因为移动设备上的文档高度增加了。 我认为原因是地址栏淡出并且文档视口(viewport)变大了。我进行了很多搜索并尝试了不
以下是插入到我自己的哈希表中时进行冲突检测的方法的内部。我正在使用小的测试数字并尝试使我的逻辑正确,变量哈希设置为 0,table.length 为 10。 else {
我正在做一个大元素,之前只用过几次转换,所以我没有太多经验。这次客户对效果有很多要求。 我有一个#item,里面有一个按钮和一个透明的div。将鼠标悬停在透明 div 上的 opacity 更改为 0
我是一个十足的 Java 新手。上周一开始,之前从未用任何语言进行过任何编程。因此,如果我发现简单的事情变得复杂,请耐心等待。 我收到了一个文本文件。如下图: 第一个数据是时间(午夜过后的秒数),第二
在 pygame 中计算重力的最佳方法是什么?我基本上只需要它,所以当玩家按下“向上”时,角色会跳跃。到目前为止,这是我的代码(只是一个带有移动的红色 block 的白色屏幕) import pyga
我用一个简单的渐变扩展了 JComponent 并调整了paintComponent()方法来制作我自己的BottomBar。 然后我将它添加到使用 BorderLayout 的 JFrame 的 S
我使用 Chart.js 渲染折线图。我不想重新调整 x 轴以获得更多样本点。 如果 y 轴值不连续变化,渲染跳跃的最佳方法是什么? 理想情况下,我能够为每个 x 值定义两个 y 值。因此,假设我传入
我有用于左、右、上、下移动的 Sprite 的 KeyEvents。我只是在闲逛,正在考虑另一个我希望 Sprite 跳跃的项目。它不必完全现实,因为我才刚刚开始。我所拥有的是,当按下空格键时,它将导
我运行双显示器设置。 从显示器 1 到显示器 2(或反之亦然)需要大量不必要的鼠标移动。 我的想法是利用一个额外的鼠标按钮(我有两个)并让鼠标从显示器 1 上的 XY 坐标超跳(向星际迷航道歉)到显示
我有一个 slider 可以实时更新其右侧标签上的值。当值从 1 位值变为 2 位值时,我应该怎么做才能防止它“跳跃”?我怎样才能给标签一个固定的位置,这样它就不会改变布局: var range =
我在页面上使用光滑的 slider 。一切都很好,除了一件事:当我拖动幻灯片时,有时图像或文本会弹起,这看起来非常糟糕。我该怎么做才能避免这个问题? 这是我的 code
const int jumpHeight = 10; //Non-Const variables bool running = true
我有一个 bootstrap Accordion 崩溃,它工作得很好,虽然有一件事困扰着我。当正在展开的元素很长并且做一个垂直滚动条时,它也会使整个页面向左小跳,但只要内容不大,一切都是流畅的。 有谁
我开始学习一些 SVG(我想还有 javascript),并且我很难理解为什么这不顺利。 0 移动了少量(大概是 x 轴上水平的“1”),但开始大幅跳跃。这是因为我使用的浏览器(Chrome)刷新/重
我想创建像飞翔的小鸟一样的游戏。我希望玩家在屏幕上连续跳跃。我创建了这段代码,它不像一只飞扬的小鸟跳跃 代码: float jump = 100; // Just example if(Gdx.inp
如下图所示,.hero div 的高度在滚动经过某个点时发生变化(Chrome、Android 6.0.1)。 这是相关的CSS .hero { height: 100%; width
当我将鼠标悬停在 li 上时,我有菜单有点摆动。如何停止摆动。 代码在这里 JSFiddle 最佳答案 摆脱它的最简单方法是像这样修改 css: .inner LI { border: 1px s
我试图让 dino 跳跃,但是当我在 dino 跳跃和下落之间使用 time.sleep(0.1) 时,整个游戏停止0.1秒。 我试过使用 time.sleep,仅此而已,因为我在网上找不到任何其他有
我正在使用 andengine 来实现一个可以使用它在屏幕上拖动的 Sprite 。 所以我想做的是当用户点击屏幕上的任何位置时使 Sprite 跳跃。 或者向上移动然后向下移动。 使用 andeng
我有一个包含 webview 的 viewflipper。在某些情况下(看似随机),webview 的底部会出现抖动/跳跃。这可以持续一秒到几秒之间的任何时间。这是一个视频来说明我在说什么 http:
我是一名优秀的程序员,十分优秀!