This question already has answers here:
How do I detect collision in pygame?
(5 answers)
How to detect collisions between two rectangular objects or images in pygame
(1 answer)
Closed 2 years ago.
I'm currently using this code for jumping:
if not Jump:
if keys[pygame.K_UP]:
Jump=True
right=False #right, left and standing are for character's facing
left=False # and they serve no purpose for jumping
standing=False
steps=0
else:
if jumpCount >= -8.5:
standing=False
neg=1
if JumpCount < 0:
neg=-1
y-=(JumpCount**2)/2 *neg
jumpCount-=1
else:
Jump=False
jumpCount=8.5 # I've set it to 8.5 bc that value works for me best
Anyway, this code works for basic jumping, but a problem occurs if I want to jump, let's say, on a box.
I tried to set it up like this:
self.line1=[self.x,self.y+60] #this is the bottom left coordinate of the
#character
self.line2=[self.x+28,self.y+60] #this is the bottom right coordinate
def on(self,a):
if (a.line1[0]+12)>self.x and (a.line1[0]+12)<(self.x+40): # self.x+40
#is the top right coordinate of the box
if a.line1[1]<=self.y and (a.line1[1]+a.v)>=self.y:
return True
return False
def collision(self,a):
if self.on(a):
a.y=self.y-60
a.Jump=False
return
def all(self,a):
self.on(a)
self.collision(a)
return
But this doesn't work. I've managed to set up that when the character hits sides of the box it can't go further, but I couldn't do the jumping part.
Is it possible to jump on something with this code, or should I change the jumping code completely, use gravity-style jumping or something? Any kind of help is welcome.
Related
This question already has answers here:
How do I detect collision in pygame?
(5 answers)
Closed last year.
colliderect() function won't return True and False; instead, it is returning 0 and 1. When I use the function type it says int instead of bool. If I code using 0 and 1 instead of the boolean value the code works, but why is it happening? Pygame documentation doesn't mention anything about it.
def collisions():
tileHitsList = []
for rect in tileList:
print(testcube.colliderect(rect)) #it weirdly prints 0 and 1 instead of False and True
#tileList is a list which contains the rects of the tiles being rendering on the display
#testcube is a rect that hits the tiles
if testcube.colliderect(rect) == True:
tileHitsList.append(rect)
return tileHitsList
It's pretty normal if the pygame documentation didn't say anything about it, as 1 and 0 are very commonly used to replace True and False.
You can just do
if testcube.colliderect(rect):
# Your code
without the ==.
Here is a documentation on the matter: https://docs.python.org/3/reference/datamodel.html#index-10
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have to make a game where n people play hangman and the game keeps their results and if they win the round, then they go on until there is only one player left(that player wins) or none left(nobody wins). My hangman code is ready, so I do not need anything concerning the hangman function. However, I need some help to make a function that does the following:
1)Asks how many players will play
2)Asks their names and stores them
3)Plays the first round and if hangman()==True (meaning the player won) for a player, then this player goes on to the next round, otherwise not
4)If somebody wins, then we have a winner and the game ends
I have already made the part that the game asks the number of player, asks their names and makes them play. My hangman() function returns either True or False. However, it seems that I have a problem. Every time a player plays the game, the hangman() function runs twice. I don't know why this happens. I would like some help fixing that and also to write the part where each round is played.
def game():
players_dict={}
results=[]
num_of_players=int(input('How many players will play? '))
for i in range(1,num_of_players+1):
a=input('Give the name of Player {}: '.format(i))
players_dict['Player {}'.format(i)]=a
for i in range(1,num_of_players+1):
print(players_dict['Player {}'.format(i)])
hangman()
if hangman()==False:
results+=False
else:
results+=True
All you need to do is get rid of the first hangman() call:
...
for i in range(1, num_of_players + 1):
print(players_dict['Player {}'.format(i)]
if hangman() is False:
results += False
else:
results += True
If you need to keep hold of the value returned, assign it to a variable beforehand:
...
r = hangman()
if r is False:
results += False
else:
results += True
Furthermore, you can shorten this code simply by writing the following (assuming you don’t keep the result):
results += hangman()
This question already has answers here:
What exactly does += do?
(17 answers)
Why does += behave unexpectedly on lists?
(9 answers)
Closed 5 years ago.
I am trying to follow the example in this post, and it was going smoothly until I ran into the lines of code with -= and += calls.
I believe this might be some sort of misprint in expressions that are supposed to update the object on the RHS of = according to some condition.
Unfortunately, interpreting this expression is key in getting what the code is doing, and guessing defeats the purpose.
Do these fragments of code or operators make any sense?
What was intended by the author?
x -= y, x += y and so forth are a shorthand way of writing x = x - y and x = x + y respectively.
These are shorthands.
a -= b
means
a = a -b
The meaning of += should be fathomable now.
But, it seems that you might have gotten some Exception. In that case, feel free to post it, and your code.
This question already has answers here:
Why is my pygame display not responding while waiting for input?
(1 answer)
Pygame Window not Responding after few seconds
(3 answers)
Why does pygame.display.update() not work if an input is directly followed after it?
(1 answer)
Closed 1 year ago.
I have an instruction I need to do:
a procedure taking into parameter two integers 'l' and 'h' (length and width)assumed positive and a filename with the corresponding extension of an image. it will create a window of 'l' length and 'h' width and will affect a random value (R,G,B form) to each of it's pixels, finally it will save this image in the current directory under the name passed into parameter
Here's my (messy) code:
def surf (l, h, imagech):
pygame.init()
ecran = pygame.display.set_mode("RGB",(l, h))
fond = pygame.image.load("MPchiffre.bmp").convert()
surface.blit(fond, (0,0))
pygame.display.flip()
continue = 1
while continue:
continue = int(input())
pixels = getPixelArray('MPchiffre.bmp')
nu.random.randint(0, 255)
pygame.image.save(surface, "imagech.bmp")
my main issue is syntax basics and basic understanding of python, I don't have time to review all of it, if someone could tell me what's wrong with this code it would be nice.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
def update(self):
# Get the current mouse position. This returns the position
# as a list of two numbers
pos = pygame.mouse.get_pos()
# Fetch the x and y out of the list,
# just like we'd fetch letters out of a string.
# Set the player object to the mouse location
self.circ.x=pos[0]
self.circ.y=pos[1]
Initialize Pygame
right where the self.circ.x=pos[0] is there seems to be a unident/indentation error, i am new to python and was wondering what was wrong?
You have a whitespace before pos variable delete it.
def update(self):
# Get the current mouse position. This returns the position
# as a list of two numbers
pos = pygame.mouse.get_pos()
# Fetch the x and y out of the list,
# just like we'd fetch letters out of a string.
# Set the player object to the mouse location
self.circ.x=pos[0]
self.circ.y=pos[1]
Pos is located one far too right.
The line
pos = pygame.mouse.get_pos()
has an extra space. To see that clearly, removing the comments helps.
pos = pygame.mouse.get_pos()
self.circ.x=pos[0]
self.circ.y=pos[1]