Window opening procedure [duplicate] - python

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.

Related

Pygame "Not Responding" [duplicate]

This question already has answers here:
Pygame window not responding after a few seconds
(3 answers)
Why is my PyGame application not running at all?
(2 answers)
How to wait some time in pygame?
(3 answers)
Closed 2 days ago.
I'm trying to add a score to my program, But when I run my code a black screen appears and it says that the app isn't responding. Without the score function it runs properly.
Here is my code:
def score_add():
global score
wait(1)
score = score + 1
while not Game_Over:
score_add()
text = font.render("Score: " + str(score), True, (0, 0, 0))
screen.blit(text, (10, 10))
pygame.display.flip()
I've tried removing pygame.display.flip() Same result.

How to make inputs not strings (python) [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 3 months ago.
I am making a quick problem solver, it takes the speed (input) and time (also input) than multiplies them to get the distance... the problem is that python thinks the inputs are strings, how do i make them numbers???
my code so far is:
py
import random
time = input("What is the time it took? (no label :: ")
speed = input("What was the speed?(no label :: ")
s = speed
t = time
distance = s * t
print(time)
print(speed)
print(distance)
You should be able to simply cast the String input to an Integer. Doing as follows will give you the desired output.
s = int(speed)
t = int(time)

Pygame colliderect() function returning 0 (zeros) and 1 (ones) instead of True and False? [duplicate]

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

How to jump on platforms in pygame [duplicate]

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.

how to get the most recent variable added to a list in Python [duplicate]

This question already has answers here:
How do I get the last element of a list?
(25 answers)
Closed 7 years ago.
I made a list in python named "worldPos" and it constantly adds the position of the mouse cursor to the list as x and y coordinates. How can I get the most recent x and y coordinates added to the list?
from Tkinter import *
root = Tk()
root.geometry('800x800')
cnv = Canvas(root, width = 800, height = 800)
cnv.pack()
worldPos = []
def motion(event):
x, y = event.x, event.y
worldPos.append((x,y))
root.bind('<Motion>', motion)
mainloop()
The .append() method adds to the very end of the list.
This location can be addressed by list_name[-1]. Where the minus sign indicates indexing from the back of the list.
However you are adding two items as a tuple, so you'll want to pull:
x,y = worldPos[-1]
If you instead added the items to the front of the list
worldPos[0:0] = (x,y)
You would then index from the front of the list
x,y = worldPos[0]
Enjoy and good luck!
p.s. Be sure to accept this as an answer if it answers your question.

Categories