Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Python
import pygame
import sys
import random 
from pygame.locals import*



# Parameter sweep variables
if len(sys.argv) < 4:
    n = 10  # Number of shrimps
    maxVel = 2  # Maximum velocity
    randDeath = 1  # Random chance of death
else:
    n = int(sys.argv[1])
    maxVel = int(sys.argv[2])
    randDeath = int(sys.argv[3])

print("\nPARAMETERS\n")
print("Number of shrimp", n)
print("Maximum velocity", maxVel)
print("Possibility of death", randDeath)
            


        
# General setup
pygame.init()
clock = pygame.time.Clock()

class Shrimp(pygame.sprite.Sprite):
    def __init__(self, picture_path, pos_x, pos_y):
        super().__init__()
        self.image = pygame.image.load(picture_path)
        self.rect = self.image.get_rect()
        self.rect.center = [pos_x, pos_y]
     
#Game Screen
screen_width = 1680
screen_height = 1050
screen = pygame.display.set_mode((screen_width, screen_height))
background = pygame.image.load('background2.png')

#the shrimp 
shrimp_group = pygame.sprite.Group()
for shrimp in range (n) :
    new_shrimp =  Shrimp("shrimp-anemone.png", random.randrange(0, screen_width),random.randrange(0, screen_height))    
    shrimp_group.add(new_shrimp)

direction = 1 

speed_x = 5
speed_y = 4

run = True
 
# Creating an infinite loop
# to run our game
while run:

 
    # Changing the direction and x,y coordinate
    # of the object if the coordinate of left
    # side is less than equal to 20 or right side coordinate
    # is greater than equal to 580
    if shrimp_group.left <= 20 or shrimp_group.right >= 580:
        direction *= -1
        speed_x = random(0, 8) * direction
        speed_y = random(0, 8) * direction
 
        # Changing the value if speed_x
        # and speed_y both are zero
        if speed_x == 0 and speed_y == 0:
            speed_x = random(2, 8) * direction
            speed_y = random(2, 8) * direction
 
    # Changing the direction and x,y coordinate
    # of the object if the coordinate of top
    # side is less than equal to 20 or bottom side coordinate
    # is greater than equal to 580
    if shrimp_group.top <= 20 or shrimp_group.bottom >= 580:
        direction *= -1
        speed_x = random(0, 8) * direction
        speed_y = random(0, 8) * direction
 
        # Changing the value if speed_x
        # and speed_y both are zero
        if speed_x == 0 and speed_y == 0:
            speed_x = random(2, 8) * direction
            speed_y = random(2, 8) * direction
 
    # Adding speed_x and speed_y
    # in left and top coordinates of object
    shrimp_group.left += speed_x
    shrimp_group.top += speed_y
    
    
#prevent the screen from crashing and displaying the screen
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()  
   
        
        pygame.display.flip()
        screen.blit(background,(0,0))
        shrimp_group.draw(screen)
        clock.tick(60)


What I have tried:

I want to make my sprites move randomly, but I keep receiving an attribute error not knowing why. And I am unsure of how to fix it. This is my first time working with pygame as it's part of a university project.
Posted
Updated 22-Nov-22 4:19am

1 solution

You need to study the documentation to see which attributes exist: pygame.sprite — pygame v2.1.4 documentation[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900