Customise Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorised as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyse the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customised advertisements based on the pages you visited previously and to analyse the effectiveness of the ad campaigns.

No cookies to display.

[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 *