jumping too fast?
You've to turn the while
loops to if
conditions. You don't want to do the complete jump in a single frame.
You've to do a single "step" of the jump per frame. Use the main application loop to perform the jump.
See the example:
import pygame
pygame.init()
win = pygame.display.set_mode((400, 400))
clock = pygame.time.Clock()
isJump = False
jumpCount, fallCount = 10, 10
x, y, width, height = 200, 300, 20, 20
run = True
while run:
clock.tick(20)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
isJump = True
if isJump:
if jumpCount > 0:
y -= (jumpCount**1.5) / 3
jumpCount -= 1
print(jumpCount)
elif fallCount > 0:
y += (fallCount**1.5) / 3
fallCount -= 1
print(fallCount)
else:
isJump = False
jumpCount, fallCount = 10, 10
print(jumpCount, fallCount)
win.fill((53, 81, 92))
pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
pygame.display.flip()
Promises jumping too fast to then() (vanilla JS)
I assume that you assumed that we assume that you resolved the function like
function promise() {
return new Promise((resolve, rejected) => {
load.site('pages/main/main.html', content);
resolve(); // Like this.
})
}
The Problem
When you call promise
. It calls load.site
and then resolves. So why isn't it giving the expected behavior.
Well, the problem is that load.site
is a synchronous function, so it does not wait for the fetch
call and returns which resolves promise
before fetch
is resolved. So, the code in promise.then()
runs before the code in fetch.then()
The Solution
Make load.site
an asynchronous function so that it returns a promise that can be awaited. i.e.
const site = async function (url) {
return fetch('a.js')
.then(response =>{
return response.text()
})
.then(data =>{
console.log("Loaded");
})
};
Then make the promise
's executor async so that you await load.site
. i.e.
function promise() {
return new Promise(async (resolve, rejected) => {
await load.site('pages/main/main.html'); // This line does the magic
resolve();
});
};
This will make sure that your promise
resolves after fetch
has resolved. So that you can get the expected behavior.
Hope it helps. Keep on coding
Unity - character falling faster than jumping
I think I figured out what was wrong with my idea.ySpeed += gravity*jumpTime
So every frame I'm adding more and more acceleration downwards. This should just be: ySpeed += gravity *Time.deltaTime
(Acceleration due to gravity is then constant, not getting greater as time passes)
It is being integrated over many steps, so each Update() cycle is a slice of time that adds some velocity based on acceleration due to gravity, multiplied by the amount of time taken by that little slice.
In another words...
Gravity is a constant acceleration. I've made it a linear function of the time spent in the air. So, I start the jump with no gravity and end the jump with very high gravity.
How do I make my Pygame Sprite jump higher and farther?
The height of the jump depends on v
. Define a variabel (jump_v
) for the initial value of v
. Use a higher value than 5, for a higher jump:
jump_v = 7 # try different values
keys = pygame.key.get_pressed()
if isjump == False:
#Up arrow key
if keys[pygame.K_UP]:
isjump = True
v = jump_v # <--- jump_v
else:
m = 1 if v >= 0 else -1
F = m * (v**2)
player.rect.y -= F
v -= 1
if v < -jump_v: # <--- jump_v
isjump = False
Related Topics
What Is Python Whitespace and How Does It Work
How to Print Bold Text in Python
How to Override the [] Operator in Python
Pandas: To_Numeric for Multiple Columns
Increment a Python Floating Point Value by the Smallest Possible Amount
Reimport a Module While Interactive
How to Do/Workaround a Conditional Join in Python Pandas
Matching Nested Structures with Regular Expressions in Python
Get a Function Argument's Default Value
Compute a Confidence Interval from Sample Data
How Does Condensed Distance Matrix Work? (Pdist)
Can Python Pickle Lambda Functions
Want to Find Contours -> Valueerror: Not Enough Values to Unpack (Expected 3, Got 2), This Appears
Python Dictionary from an Object's Fields