'how to make blit rotate toward mouse? [duplicate]
so im making a top down shooter game and i want to make the player face the mouse at all times. whenever i search it up, i cant figure out what the answer actually means. ive tried:
mouseY, mouseX = pygame.mouse.get_pos()
angle_to_pointer = math.degrees(math.atan2(T_rect.y - mouseY, T_rect.x - mouseX)) + 180
but that only makes it spin uncontrollably off the screen.
heres my main loop:
run = True
while run:
screen.blit(Tonk,(T_rect))
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
pr_l = True
if event.key == pygame.K_RIGHT:
pr_r = True
if event.key == pygame.K_UP:
pr_u = True
if event.key == pygame.K_DOWN:
pr_d = True
if event.key == pygame.K_SPACE:
pr_s = True
elif event.type == pygame.KEYUP: # check for key releases
if event.key == pygame.K_LEFT: # left arrow turns left
pr_l = False
elif event.key == pygame.K_RIGHT: # right arrow turns right
pr_r = False
elif event.key == pygame.K_UP: # up arrow goes up
pr_u = False
elif event.key == pygame.K_DOWN: # down arrow goes down
pr_d = False
elif event.key == pygame.K_SPACE:
pr_s = False
elif event.type == pygame.MOUSEMOTION:
m_m = True
if pr_l == True:
screen.fill(colour)
T_rect.x -= speed
screen.blit(Tonk,(T_rect))
if pr_r == True:
screen.fill(colour)
T_rect.x += speed
screen.blit(Tonk,(T_rect))
if pr_u == True:
screen.fill(colour)
T_rect.y -= speed
screen.blit(Tonk,(T_rect))
if pr_d == True:
screen.fill(colour)
T_rect.y += speed
screen.blit(Tonk,(T_rect))
if m_m == True:
screen.fill(colour)
screen.blit(Tonk,(T_rect))
pygame.display.flip()
clock.tick(60)
please help?
Edit: answered on my previous question: how can i make an image point towards the mouse in python
Solution 1:[1]
React is not batching state updates from fetch(). It is batched in case of event listeners. This is an async fetch call.
In this sandbox console, you can see that there is a render in between your state updates - setIsLoading(false) and setIsError(true).
So for one render cycle : isLoading is false and isError is also false. That will lead to the error condition.
You can use unstable_batchedUpdates to enforce batching.
import { useEffect, useState } from "react";
import { unstable_batchedUpdates } from "react-dom";
import "./styles.css";
const url = "https://api.github.com/users/QuincyLarsn";
const App = () => {
const [isLoading, setIsLoading] = useState(true);
const [isError, setIsError] = useState(false);
const [user, setUser] = useState(null);
useEffect(() => {
fetch(url)
.then((data) => {
if (data.status >= 200 && data.status <= 299) return data.json();
else {
console.log("here");
unstable_batchedUpdates(() => {
setIsLoading(false);
setIsError(true);
});
console.log("here 2");
}
})
.then((result) => {
setIsLoading(false);
setUser(result);
})
.catch((error) => {
console.log(error);
});
}, []);
console.log("isError", isError);
if (isLoading) return <h2>Loading...</h2>;
if (isError) return <h2>Error...</h2>;
return <h2>{user.login}</h2>;
};
export default App;
Solution 2:[2]
In a such case, order does matter.
While React may batch updates in this case, it's not guaranteed and even if it does, it may call the render function with the in-between state.
So, when isLoading is set to false, but user is not yet set, you get an error.
You can fix this by setting the user first, and then making isLoading false.
But the real solution would be to eliminate the unnecessary state variables: isLoading is true while isError is false and user is null, and false otherwise.
So, you can do it like this:
should not as I already have checks for the boolean before return.
import React, { useState, useEffect } from 'react';
const url = 'https://api.github.com/users/QuincyLarsn';
const MultipleReturns = () => {
const [isError, setIsError] = useState(false);
const [user, setUser] = useState(null);
useEffect(() => {
fetch(url)
.then(data => {
if (data.status >= 200 && data.status <= 299)
return data.json();
else {
console.log("here");
setIsError(true);
console.log("here 2");
}
})
.then(result => {
setUser(result);
})
.catch(error => console.log(error))
}, []);
console.log(isError);
if (isError) {
return <h2>Error...</h2>
}
if (user !== null) {
return <h2>{user.login}</h2>
}
return <h2>Loading...</h2>
};
export default MultipleReturns;
Solution 3:[3]
After you set isLoading as false, the code moves to the last return statement as the error is still false at the moment. So first setting the error blocks the code at the second return statement.
Similarly if you set isLoading as false then set the user, the code will move to the last return statement before the user is set and it will show error. Setting the user and then making isLoading as false shows the user perfectly.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | FZs |
| Solution 3 | Timsib Adnap |
