Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
2.7 kB
2
Indexable
Never
import pygame

class Player:
    def __init__(self):
        self.player_x = 400
        self.player_y = 355
        self.v_x = 0
        self.v_y = 25
        self.a = 2
        self.is_jump = False

    def check_jump(self):
        self.v_y += 5
        self.is_jump = True

    def move_x(self):
        global keys
        if keys[pygame.K_LEFT]:
            if self.player_x > 40:
                self.v_x = -5
                self.player_x += self.v_x
            else:
                self.v_x = 0
        else:
            self.v_x = 0
        if keys[pygame.K_RIGHT]:
            if self.player_x < 800:
                self.v_x = 5
                self.player_x += self.v_x
            else:
                self.v_x = 0
        else:
            self.v_x = 0
    def jump(self):
        global keys
        if keys[pygame.K_UP] and not self.is_jump:
            self.check_jump()
        if self.is_jump:
            self.v_y -= self.a
            y1 = self.player_y - self.v_y
            if y1 > 355:
                y1 = 355
                self.is_jump = False
                self.v_y = 25
            self.player_y = y1
    def color(self):
        global running
        global backC
        global index
        global events
        global event
        for event in events:
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_1:
                    index = (index + 1) % len(colors)
                    backC = colors[index]


pygame.init()
screen = pygame.display.set_mode((1000, 500))
pygame.display.set_caption("My pygame Project")
pygame.display.set_icon(pygame.image.load('images/icon game.png'))

player = pygame.image.load('images/player.png')
player = pygame.transform.scale(player, (150, 150))

backC = (112,112,214)
colors = [(112,112,214), (141, 67, 183), (65, 149, 121), (237,238, 47)]
index = 0

screen.fill(backC)

# myfont = pygame.font.Font("fonts/OpenSans.ttf", 40)
# text_surface = myfont.render('TEXT', False, 'yellow')  - шрифт

davic = Player()
clock = pygame.time.Clock()
running = True
while running:
    clock.tick(60)
    screen.fill(backC)
    screen.blit(player, (davic.player_x, davic.player_y))
    keys = pygame.key.get_pressed()
    events = pygame.event.get()
    #движение игрока
    davic.move_x()

    #прыжок
    davic.jump()

    #смена цвета
    davic.color()

    #смена цвета и выход из игры
    for event in events:
        if event.type == pygame.QUIT:
            running = False
    pygame.display.flip()
pygame.quit()
Leave a Comment