Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pygame rect follower

import pygame
import random
import math

# --- constants --- (UPPER_CASE_NAMES)

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

FPS = 25 # for more than 220 it has no time to update screen

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

# --- classes --- (CamelCaseNames)

class Player(pygame.sprite.Sprite):

    def __init__(self, x=SCREEN_WIDTH//2, y=SCREEN_HEIGHT//2):
        super().__init__()
        self.image = pygame.image.load("image.png").convert()
        #self.rect = self.image.get_rect(x=x, y=y)
        self.rect = self.image.get_rect(centerx=x, centery=y)

    def update(self):
        #self.rect.centerx = random.randint(0, SCREEN_WIDTH)
        #self.rect.centery = random.randint(0, SCREEN_HEIGHT)
        move_x = random.randint(-15, 15)
        move_y = random.randint(-15, 15)
        self.rect.move_ip(move_x,move_y)

    def draw(self, surface):
        surface.blit(self.image, self.rect)

class Follower(Player):

    def update(self, player):
        distance = math.hypot(abs(player.rect.x - self.rect.x), abs(player.rect.y - self.rect.y))
        angle_radians = math.atan2((player.rect.y - self.rect.y), (player.rect.x - self.rect.x))

        if distance !=  0:
            self.rect.y += 5*math.sin(angle_radians)
            self.rect.x += 5*math.cos(angle_radians)        

# --- functions --- (lower_case_names)

# --- main ---

pygame.init()

screen = pygame.display.set_mode( (SCREEN_WIDTH, SCREEN_HEIGHT) )

player = Player()
follower = Follower(0, 0)

# --- mainloop ---

clock = pygame.time.Clock()

running = True
while running:

    # --- events ---
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_ESCAPE:
                running = False


    # --- changes/moves/updates ---

    if not pygame.key.get_pressed()[pygame.K_SPACE]:
        player.update()
        follower.update(player)
    # --- draws ---

    screen.fill(BLACK)

    player.draw(screen)
    follower.draw(screen)

    pygame.display.flip()

    # --- FPS ---

    ms = clock.tick(FPS)
    #pygame.display.set_caption('{}ms'.format(ms)) # 40ms for 25FPS, 16ms for 60FPS
    fps = clock.get_fps()
    pygame.display.set_caption('FPS: {}'.format(fps))

# --- end ---

pygame.quit()
Comment

PREVIOUS NEXT
Code Example
Python :: pyfcm image 
Python :: cdf empírica python 
Python :: pandas filter rows by fuzzy values 
Python :: data wrangling python 
Python :: scrapy get raw html content of selector innerhtml 
Python :: start of the american labor movement 
Python :: devu and friendship testing codechef solution 
Python :: how to flatten the image dataset 
Python :: vertica long running queries 
Python :: for loop does not work with open 
Python :: cambiar barra de etitulo tkinter 
Python :: Recursively find the factorial of a natural number. 
Python :: gtk entry focus python 
Python :: python numpy + opencv + overlay image 
Python :: Fish market linear regression implementattion 
Python :: Blender Python set center to center of mass 
Python :: introduction to sets python3 
Python :: add a new button in the index of the page wagtail 
Python :: block url selenium python 
Python :: tkinter tooltip 
Python :: sphix dont see .py file 
Python :: for loop pattern in python stack overflow 
Python :: norm 2 or ocklidos of matrix in python 
Python :: select values for row matching condition 
Python :: how to use event of Button in python 
Python :: convert_hex_to_ASCII_3.py 
Python :: print 2 letter python 
Python :: 3x3 gaussian kernel 
Python :: python open multiple .py windows 
Python :: programme phyton pour realiser un programme qui transforme une image en niveau de gris 
ADD CONTENT
Topic
Content
Source link
Name
2+2 =