Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am trying to make my first game in pygame and I run into some problems.
I don't know how to spawn my enemies at random times. When I do it in my while loop they show all at once.
Every game loop add a chance to create an enemy. For example:
import random
# Main game loop
while True:
if random.randrange(0, 100) < 1: # 1% chance every frame
# Create a fly with specified properties
Fly(random.randrange(xmin, xmax), random.randrange(ymin, ymax), width, height, img_src)
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 days ago.
Improve this question
I am a novice. I don't know how to write the following code:
I have a rectangular box. There is a point in the box. How to get the new coordinates for the point after turning the image horizontally.
How to get the new position of the point after a horizontal flip
Their position has changed from (100, 150) to (100, 50)
Thank you.
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 2 years ago.
Improve this question
I want to make a game in the turtle module similar to "Lunar Lander" by Atari but I'm not sure if I'll be able to make a collision system if I draw the terrain using a turtle. Any help would be appreciated.
I don't think python turtle have the collision system, but you can make the limit for the ground. Such as if the ground surface is on -40y, then you can create a variable and prevent the object from going through the ground.
groundLimit = -40 #create the limit for the object
while True:
if turtle.ycor() < 40: #check if the object hit the ground
turtle.sety(40) #make the object on the ground again
#but this method might make the object a bit weird
But if you are making a game, I would suggest using the "pygame" module. It's great for making games and better than turtle. Turtle is just for drawing, but pygame is for making games. You can find tutorials online to help you.
Good Luck!
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm writing code in Python using turtle to visualise a bubble sort but it's very slow. Is there any way I can speed it up because speed(0) for Python turtle is not quick enough. Or are there any other ways of doing this sort of project using Python?
The first thing you should do is confirm that it's the turtle graphics that's slowing things down. For example, run your bubble sort with and without the graphics to see how much time is really lost.
One way to speed up turtle graphics, which you should approach carefully, is using tracer():
screen = turtle.Screen()
# ...
screen.tracer(False) # turn off graphic updates
# ...
# Whenever you make a change you want the user to see:
screen.update() # make screen current
# ...
# When you're completely finished:
screen.tracer(True) # turn graphics updates back on
# ...
screen.mainloop()
You don't need speed(0) (aka speed('fastest')) in this scenario though it won't hurt to leave it. Some graphic operations will force screen.update() independent of your calls to it -- don't be surprised. Make sure to tracer(True) at the end so things like hiding the turtle work properly.
Finally, add a minimal, working example of your code to your question above for a proper review.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have looked for a solution to my problem but the only one I could find was outdated. I know how to put text on the window but it doesn't work for variables.
It would be greatly appreciated if someone could provide an example code.
edit: this is what i tried using a variable with
myfont = pygame.font.SysFont('Arial', 30)
textsurface = myfont.render(score, True, (0, 0, 0))
windowSurface.blit(textsurface,(250, 250))
then python me to use a unicode or byte answer
It would be easier to help if we had more details.
However, if what you want to show is the change in a variable throughout the program, there's nothing wrong with just printing them as you go along. You can always take them out later if it all works and you don't want to see them.
for i in range(0, 10):
print("i is", i) #prints out what i is each time through the loop.
If what you want to do is print out the name instead of the value, then you should reconsider - it's possible but awkward, and probably not necessary.
EDIT:
If you mean that you want to display a variable's value to the player, and have that update, then this might be helpful: How to display text in pygame?
What you'll want to do is update the surface every time the value changes, or every time you update the rest of the display. Don't edit it, just redraw it with different text.
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 6 years ago.
Improve this question
I am making a python game and I am not exactly sure how to make the enemy's and the player have collisions. Could someone show me a easy way to add collisions to multiple things efficiently. I want the enemy in my python game to collide with the player. Here is the code I use to move the enemy to the player. This code moves the enemy but the enemy does not collide with the player.
if self.canvas.coords(self.man)[0] > self.canvas.coords(Man1.man)[0]:
self.canvas.move(self.man,-1,0)
if self.canvas.coords(self.man)[0] < self.canvas.coords(Man1.man)[0]:
self.canvas.move(self.man,1,0)
if self.canvas.coords(self.man)[1] < self.canvas.coords(Man1.man)[1]:
self.canvas.move(self.man,0,1)
if self.canvas.coords(self.man)[1] > self.canvas.coords(Man1.man)[1]:
self.canvas.move(self.man,0,-1)
You can use the canvas.overlapping() which returns all the items in a given rectangle. Just give it the x,y coordinates of one of your objects and see if the tuple returns more than one.
if you show us some code or give more details we might be able to better help you