Pygame moving multiple sprites not working as expected [closed] - python

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 1 year ago.
Improve this question
I am making space invaders in pygame, I am relatively new to python and this is my first project in pygame. I am trying to make my aliens move down when reaching the edge of the screen on either side. However, it is not quite working as expected. I am adding a link to a GitHub page so that anyone willing to help can view my code.
Basically what's happening is I have set the aliens to move down 1 pixel when touching the sides because when there are a lot of aliens, this moves them down quite a bit. Obviously, as the aliens start getting killed off, they move down less. However, that is not the strange part. The strange part is the fact that sometimes they will move down 1 px on the one side, but many on the other side. I'm not quite sure what I am doing wrong.
https://github.com/Kris-Stoltz/space_invaders

Add break statements so you don't call update multiple times (if an even number of aliens hit the wall, you end up traveling the same direction!)
You have to increase the y advance in update too:
for alien in aliens:
if alien.rect.right >= WIDTH:
aliens.update()
break
elif alien.rect.left <= 0:
aliens.update()
break
and:
def update(self):
self.direction *= -1
self.rect.y += 10
The code looks pretty cool though!

I changed that code to
for alien in aliens:
if alien.rect.right >= WIDTH-1:
aliens.update()
elif alien.rect.left <= 1:
aliens.update()
But I see if enemies too less they don't move down
def update(self):
print("x",self.rect.y)
self.rect.y += 1
print("y",self.rect.y)
self.direction *= -1
That peace of code looks like incorrect

Related

Making a Bullet move towards a Target without following the Target using Pgzhelper in Pygame Zero

Question: How can I make a bullet move towards a target without following the target using Pgzhelper in Pygame Zero?
I wanted to share this with a lot of people that may have the same trouble using Pygame Zero:
The problem may stem from people confusing how to move Actors using angle(), distance(), direction(), direction_to(), move_forward(), move_backward(), move_towards() and so on. I've watched and read a lot of videos and read many formulaic solutions but did not understand the key points of them because I suck hard at basic trigonometry.
So here's a simple solution, I realize after a few days:
ammo = []
def reload():
bullet = Actor('pic', center = (x,y))
bullet.angle = bullet.direction_to(target)
ammo.append(bullet)
def update():
for bullet in ammo:
bullet.move_forward(15)
if bullet.x < 0 or bullet.x > WIDTH or bullet.x < 0 or bullet.y > HEIGHT:
ammo.remove(bullet)
And after realizing this I LOL myself for how simple the solution was. Hahaha XD
I hope this would help others that suck at Trig or Math like me and are searching for simple ways to program games "as efficiently as possible".

Coding an Interactive Game in Python Using Turtle: Hopper [closed]

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 2 years ago.
Improve this question
I'd like to know how to code a certain portion of my game, Hopper. In this interactive python game using import turtle, I would like to create a turtle the user can control. I would like this turtle to jump when the spacebar is used, for the purpose of the user being able to jump over objects that should consistently move towards the player's turtle from the right side of the screen.
My question is, how to I make a circle shaped turtle jump when I hit the space bar and fall back to the ground, so that it can jump again when the next obstacle comes?
How do I create a code for obstacles, AKA, rectanglular shaped turtles, to move towards the player's circle turtle for the player's circle turtle to jump over? Thank you so so much for your time and help, will upvote.
If possible, I would love to know how to display a "Game Over" message if the player is unsuccessful in jumping over one of the rectangle obstacle turtles with the circle turtle.
Thank you again!
This is a common problem in Software Engineering. But let's take a step back. How do you cook breakfast? Well what's breakfast - let's say "boiled egg". What's an egg? What is it to "boil"? What sort of egg? Where does it come from? How big is it? Is it an Emu egg? How long do you boil it for? Etc. Etc. Etctera.
A big question like "How to Make Hopper" is really a huge list of tiny tiny problems. How do you draw on the screen, how to you read a key press, how do you move the turtle, how do I make that "sproinggg" sound when it jumps...
So. Start at the beginning, ignore the "noise" of what you can't do, and concentrate on what you can do. What's the simplest starting point? Write down some notes, draw some screen-layouts. Think about how it's all going to work. What are the first steps now?
Here's some simple code that opens a window and reads the keyboard. Take this code, and adapt it to handle the user-input according to your design.
import pygame
# Window size
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 400
WINDOW_SURFACE = pygame.HWSURFACE|pygame.DOUBLEBUF
WINDOW_MAX_FPS = 60
BACK_COLOUR = ( 3, 5, 54)
### initialisation
pygame.init()
pygame.mixer.init()
window = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ), WINDOW_SURFACE )
pygame.display.set_caption("Hopper")
### Main Loop
clock = pygame.time.Clock()
done = False
while not done:
# Handle Window & Keyboard Events
for event in pygame.event.get():
if ( event.type == pygame.QUIT ):
done = True
elif ( event.type == pygame.KEYDOWN ):
if ( event.key == pygame.K_SPACE ):
print( 'Space-key pushed' )
elif ( event.key == pygame.K_j ):
print( 'j pushed' )
# Handle Key-states
keys = pygame.key.get_pressed()
if ( keys[pygame.K_UP] ):
print("up")
elif ( keys[pygame.K_UP] ):
print("down")
# Update the window
window.fill( BACK_COLOUR )
pygame.display.flip()
clock.tick( WINDOW_MAX_FPS )
pygame.quit()
Next start with simple graphics - you don't need a graphic-artist drawn turtle-avatar at this stage, just a box. Once you have this drawing, work on making it move, then make it move according to user-input. Keep chipping away at the program, but heading towards your design.
Plenty of beginners here get stuck trying to implement a beautiful 50-frame player-walking animation as a first step, and get lost and disillusioned when it's not easy (or perfect). Keep it simple, and actually finish your project.

My Character Sprite Doesn't Move Off Screen

I have been going over Paul Craven's Python and Pygame tutorial and lately I am trying to understand everything in Sprites. So, being inspired by Craven's multiple levels code, I've written my own code on it and it worked fine.
The logic is, if player sprite is off screen(for example screen width is equal to 640 and player's x value is greater than 640, than you are in the next level). Than, I decided to add menu to my game(?) and menu works fine as well.
The problem is, now my character can't get to second level. It seems it's x value is unable to pass 640, and I don't know why.
Since I am not able to use Stackoverflow editor good enough to share Python code(you know, intended), I will be using Pastebin to share my code.
http://pastebin.ubuntu.com/10392389/
Stackoverflow doesn't let me post the image files I've used, but they are classic pictures I got from the internet.
Any help is appreciated, thanks.
if current_level_num == 1 and player.rect.x > SCREEN_WIDTH:
current_level_num = 2
current_level = levels[current_level_num]
player.rect.x = 0
if current_level_num == 2 and player.rect.x < 1:
current_level_num = 1
current_level = levels[current_level_num]
player.rect.x = SCREEN_WIDTH
The character moves to the 2nd level, then immediately moves back to the 1st. The player should be moved farther to the right than the level 2 boundary.

minus lives when collide with enemy not working pygame

I am making a pacman style game in pygame using python and i am trying to make it so that when the player collides with an enemy sprite (monster) the score gets reduced by 1. The code for the monster and player are below and also the code i have tried to minus the score. Any help would be appreciated. I can post the whole game code if this will help.
The code i have tried to minus the player lives when colliding with a monster is below.
for monster in group:
if player.rect.colliderect(monster.rect):
player.lives -= 1
I suspect the problem is that you only create livestext at the beginning. The should be re-created each time you print it on the screen.
You seem to be drawing the text right at the end, after the game (why then?). So move the livestext=... line to before the blit. The lives may well be doing what you want, but perhaps you can't see it?
Also, I'd recommend making lives an instance member:
Do self.lives=5 in the __init__
and use self.lives instead of lives every time it occurs.

python 2.6 overhead dungeon crawl [closed]

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 8 years ago.
Improve this question
i am going to have multiple questions so i figure i would put it into one thread so i do not flood the website with my stupid questions.
i will list all questions as an edited question, and hope it will be answered. the first question is as follows (on my 2nd question):
this is my first "real" graphics based game i will create, but i know i need assistance.
def update(self):
if games.keyboard.is_pressed(games.K_w):
self.y -= 1
if games.keyboard.is_pressed(games.K_a):
self.x -= 1
if games.keyboard.is_pressed(games.K_s):
self.y += 1
if games.keyboard.is_pressed(games.K_d):
self.x += 1
if games.keyboard.is_pressed(games.K_i):
Inventory()
the above is in my update function of my player class, checking to see if i is pressed.
def Inventory():
global LEVEL, TOTAL_XP, MAX_HEALTH, MAX_MAGICKA, viewing_inv
viewing_inv = True
inventory_items = []
while viewing_inv == True:
print "yo"
score = games.Text(value = "Points", size = 25, color = color.green, top = 5,
left = games.screen.width/2 + 14)
games.screen.add(score)
if games.keyboard.is_pressed(games.K_i):
games.screen.remove(score)
viewing_inv = False
the above is a temp inventory function i am using to make sure things are working, which they are not.
i added the print statement so i can view what is going on behind the scenes. i see the word "yo" print roughly 2-4 times each time i hit i. how can i successfully get it so that if i press i i go into the inventory function w/o having the computer loop through 2+ times before i can remove my finger? this question is already alot even though i want to be able to press i again to exit. any advice would be appreciated!
If your room class is working correctly you shouldn't need a global, think about using instance attributes which are set in the constructor.
Also I don't think a separate class per room type is needed. It feels unnecessarily complex, as essentially each room behaves the same - it may look different - but you can handle to look of the room via instance attributes. What you need I think is to create separate instances of the single room class, and when you create the instance, pass in the walls, enemies, floors etc.

Categories

Resources