Untitled

 avatar
unknown
plain_text
4 months ago
6.3 kB
27
Indexable
import pygame 
from sys import exit 
from random import randint, choice
#randint - random integer
#choice - random selection from a list 

pygame.init()
screen = pygame.display.set_mode((800,400))
#set_mode - creates the window
pygame.display.set_caption("Runner")
clock = pygame.time.Clock()
test_font = pygame.font.Font('font/Pixeltype.ttf', 50)

bg_music = pygame.mixer.Sound('audio/music.wav')
bg_music.play( loops = -1)

#loops = 0 (original)
#loops = 1 ( 2 times )
#loops = -1 (forever)

class Player(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        #walk
        player_walk_1 = pygame.image.load('graphics/player/player_walk_1.png').convert_alpha()
        #convert_alpha() - allows images to be transparent
        player_walk_2 = pygame.image.load('graphics/player/player_walk_2.png').convert_alpha()
        self.player_walk = [player_walk_1, player_walk_2 ]
        # list [ ]
        self.player_index = 0
        self.image = self.player_walk[self.player_index]

        #jump
        self.player_jump = pygame.image.load('graphics/player/jump.png').convert_alpha()
        self.rect = self.image.get_rect(midbottom = (80, 300))
        self.gravity = 0 #gravity affect vertical movement 

        self.jump_sound = pygame.mixer.Sound('audio/jump.wav')
        self.jump_sound.set_volume(0.5)
    

    def player_input(self):
        keys = pygame.key.get_pressed()

        if keys[pygame.K_SPACE] and self.rect.bottom >= 300:
            self.gravity = -20
            self.jump_sound.play()
    

    def apply_gravity(self):
        self.gravity +=1
        self.rect.y += self.gravity 
        if self.rect.bottom >= 300:
            self.rect.bottom = 300

    
    def animation_state(self):
        if self.rect.bottom < 300:
            self.image = self.player_jump
        else:
            self.player_index += 0.1  #slower movement through the list 
            if self.player_index >= len(self.player_walk):
                self.player_index = 0
            self.image = self.player_walk[int(self.player_index)]
                #int() - integer
    
    def update(self):
        self.player_input()
        self.apply_gravity()
        self.animation_state()

            #len() - finds the length of the list
            #() - put the list name
            #list_name[]

class Obstacle(pygame.sprite.Sprite):
    def __init__(self, type):
        super().__init__() 
        #super() - lets child class inherit information from the parennt class
# >= ,<= , == 
        if type == 'fly':
            fly_1 = pygame.image.load('graphics/fly/fly1.png').convert_alpha()
            fly_2 = pygame.image.load('graphics/fly/fly2.png').convert_alpha()
            self.frames = [fly_1, fly_2] #class variable 
            y_pos = 210

        else: #snail
            snail_1 = pygame.image.load('graphics/snail/snail1.png').convert_alpha()
            snail_2 = pygame.image.load('graphics/snail/snail2.png').convert_alpha()
            self.frames = [ snail_1, snail_2] #list 
            y_pos = 300
    
        self.animation_index = 0
        self.image = self.frames[self.animation_index]
        self.rect = self.image.get_rect(midbottom = (randint(900,1100), y_pos))
    
# to access an item in a list - listname[index]
    





def player_animation():
    global player_surf, player_index 
    #global variable acess it anywhere throughout your code 

    if player_rect.bottom < 300:
        player_surf = player_jump
    else:
        player_index += 0.1
        if player_index >= len(player_walk):
            player_index = 0
        player_surf = player_walk[int(player_index)]
#logicl operator - and or 

def collisions(player, obstacles):
    #obstacles - list 
    if obstacles:
        for obstacle in obstacles:
            if player.colliderect(obstacle):
                return False 
    return True    

#for i in range(1, 11)
# 1,2,3,4,5,6,
#for i in range(5)
#0,1,2,3,4 

def collision_sprite():
    if pygame.sprite.spritecollide(player.sprite, obstacle_group, False):
        obstacle_group.empty() #clears all the sprites from the screen
        return False
    else: #no collisions
        return True # no collisions so game continues 

def obstacle_movement(obstacle_list):
    #obstacle_list - stores positions the obstacles can be in 
    if obstacle_list: #if len(obstacle_list) >0: is the list empty ?
        for obstacle_rect in obstacle_list:
            obstacle_rect.x -= 5 # move forward

            if obstacle_rect.bottom == 300:
                screen.blit(snail_surf, obstacle_rect)
            else:
                screen.blit(fly_surf, obstacle_rect)
            
        # List comprehension
        # [result for.........  if...] - to update it a list
        obstacle_list = [obstacle for obstacle in obstacle_list if obstacle_rect.x > -100]
        return obstacle_list
    else:
        return []

#new_obstacle_list = []
#for obstacle in obstacle_list:
    #if obstacle_rect.x > -100:
    #new_obstacle_list.append()

#obstacle_list = new_obstacle_list


#append lets you add to the list



player =  pygame.sprite.GroupSingle()
#GroupSingle - container that holds one sprite 
player.add(Player())
game_active = True 

sky_surface = pygame.image.load('graphics/Sky.png').convert()
ground_surface = pygame.image.load('graphics/ground.png').convert()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()

    if game_active: #True   #nested if loop - more than one if loop inside of another 
        if event.type == pygame.KEYDOWN:  #KEYDOWN checks for any that has been pressed 
            #K_DOWN is the downwards arrow key 
            if event.key == pygame.K_SPACE and player_rect.bottom >= 300:
                player_gravity = -20

    else: #game_active = False
        if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
            game_active = True

    if game_active:
        screen.blit(sky_surface, (0,0))
        #blit() brings the image to the screen at a specific position
        screen.blit(ground_surface, (0, 300))

        player.draw(screen)
        player.update()
       


    #screen.fill((94, 129,162))  #blue

    pygame.display.update()
    clock.tick(60)
Editor is loading...
Leave a Comment