[ad_1]

I am attempting to get a turtle sprite to chase a falling algae sprite utilising Pygame’s inbuilt performance. I’ve spent many hours reviewing questions and solutions and suspect I am lacking some foundational logic as a newbie.

Beneath is the total code pared right down to important strains solely.

No errors are displayed, but the turtle sprite doesn’t transfer.

import pygame
import random
from random import randint

#GLOBAL VARIABLES
BACKGROUND_COLOR = pygame.Coloration('black')
screen_width = 1400
screen_height = 1000
win = pygame.show.set_mode((screen_width, screen_height))
display_surface = pygame.show.get_surface()
display_rect = display_surface.get_rect()
clock = pygame.time.Clock()

class Turtle(pygame.sprite.Sprite):
    def __init__(self):
        tremendous(Turtle, self).__init__()
        self.picture = pygame.picture.load('T10.png').convert_alpha()
        self.x = random.randrange(60, (screen_width - 60), 1)                                                
        self.y = random.randrange(60, (screen_height - 60), 1) 
        self.pos = pygame.Vector2(self.x, self.y)
        self.rect = self.picture.get_rect()
        self.rect.x = self.x
        self.rect.y = self.y
        self.velocity = 5

    def transfer(self):
        self.pos.move_towards_ip(algae.pos, self.velocity)

    def replace(self):
        self.transfer()

class Algae(pygame.sprite.Sprite):
    def __init__(self):
        tremendous(Algae, self).__init__()
        self.width = 20
        self.top = 20
        self.surf = pygame.Floor((self.width, self.top), pygame.SRCALPHA)
        self.rect = self.surf.get_rect()
        self.x = -5
        self.y = -5
        self.pos = pygame.Vector2(self.x, self.y)
        pygame.draw.circle(self.surf, pygame.Coloration('inexperienced'), (10, 10), 4)
        self.record = []
        

pygame.init()
pygame.show.set_caption("EvoSim")

turtle = Turtle()
algae = Algae()
turtle_group = pygame.sprite.Group(turtle)
algae_group = pygame.sprite.Group(algae)
all_sprites = pygame.sprite.Group()
collide_list = pygame.sprite.Group(algae)                       #not presently in use

algae_spawn = pygame.USEREVENT + 1                              #algae spawn occasion
pygame.time.set_timer(algae_spawn, randint(0, 10000))           #algae spawn timer

def principal():
    whereas True:
        clock.tick(15)
        bg = pygame.picture.load('bg.png')
        win.blit(bg, (0, 0))
        turtle.rect.clamp_ip(pygame.show.get_surface().get_rect())
        turtle_group.draw(win)
        turtle_group.replace()

        for occasion in pygame.occasion.get():                 
            if occasion.sort == pygame.QUIT:
                pygame.give up()
                give up()
            if occasion.sort == algae_spawn:      #falling algae spawn on random timer
                algae.x = random.randrange(10, win.get_width()-10)
                algae.record.append(pygame.Rect(algae.x, -20, 20, 20))
                collide_list.add(algae)

        for algae.rect in algae.record[:]:
            algae.rect.y += 1                                      
            if algae.rect.prime > win.get_height():                   
                algae.record.take away(algae.rect)

        for algae.rect in algae.record:
            win.blit(algae.surf, algae.rect)
    

        pygame.show.replace()

if __name__ == '__main__':
    principal()

[ad_2]

Leave a Reply

Your email address will not be published. Required fields are marked *