import pygame
import sys
import random
from pygame.locals import*
if len(sys.argv) < 4:
n = 10
maxVel = 2
randDeath = 1
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)
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]
screen_width = 1680
screen_height = 1050
screen = pygame.display.set_mode((screen_width, screen_height))
background = pygame.image.load('background2.png')
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
while run:
if shrimp_group.left <= 20 or shrimp_group.right >= 580:
direction *= -1
speed_x = random(0, 8) * direction
speed_y = random(0, 8) * direction
if speed_x == 0 and speed_y == 0:
speed_x = random(2, 8) * direction
speed_y = random(2, 8) * direction
if shrimp_group.top <= 20 or shrimp_group.bottom >= 580:
direction *= -1
speed_x = random(0, 8) * direction
speed_y = random(0, 8) * direction
if speed_x == 0 and speed_y == 0:
speed_x = random(2, 8) * direction
speed_y = random(2, 8) * direction
shrimp_group.left += speed_x
shrimp_group.top += speed_y
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.