Untitled

mail@pastecode.io avatar
unknown
python
2 years ago
4.6 kB
2
Indexable
Never
import pygame

pygame.init()
SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 800
FPS = 30
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
SKY = (77,255,255)
RUNNING = True
GRAVITY = 4
WORLD_FLOOR = 700

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

clock = pygame.time.Clock()
pygame.display.set_caption('GAME')


class Player(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.velocity = 7
        self.x = 50
        self.y = WORLD_FLOOR
        self.image = pygame.image.load('player.png')
        self.rect = self.image.get_rect(center=(self.x, self.y))
        self.width = 32
        self.height = 32
        self.jumping = False
        self.xVel = self.velocity
        self.yVel = 10
        self.movingLeft = False
        self.movingRight = False
        self.inAir = False


    def jump(self):
        if self.yVel >= -10:
            neg = 1
            if self.yVel < 0:
                neg = -1
            self.rect.y -= (self.yVel ** 2) * 0.5 * neg
            self.yVel -= 1
            return True
        else:
            self.jumping = False
            self.yVel = 10
            self.rect.y = WORLD_FLOOR

    def move(self):
        keys = pygame.key.get_pressed()
        if keys[pygame.K_a] and self.rect.x > self.yVel:
            self.rect.x -= self.xVel

        if keys[pygame.K_d] and self.rect.x < SCREEN_WIDTH - self.width - self.yVel - 10:
            self.rect.x = self.rect.x + self.xVel

        if not self.jumping:
            if keys[pygame.K_w]:
                self.jumping = True
        else:
            self.jump()


class Level():
    def __init__(self):
        level1_data = [[1, 1, 1, 1, 1,1],
                       [1, 0, 0, 0, 1,1],
                       [0, 0, 1, 0, 1,1],
                       [0, 0, 1, 1, 1,1]]
        self.tile_size = 200
        self.tile_height = 30
        self.tiles = []
        self.grass_img = pygame.image.load('grass.png')

        for y, row in enumerate(level1_data):
            for x, tile in enumerate(row):
                if tile > 0:
                    img = pygame.transform.scale(self.grass_img,(self.tile_size, self.tile_height))
                    img_rect = img.get_rect()
                    img_rect.x = x * self.tile_size
                    img_rect.y = y * self.tile_size
                    tile_data = (img, img_rect)
                    self.tiles.append(tile_data)

    def draw(self):
        for platform in self.tiles:
            display_surface.blit(platform[0], platform[1])
            pygame.draw.rect(display_surface, (0, 0, 0), platform[1], 2)


class Game(object):
    SCREEN_WIDTH = 1800
    SCREEN_HEIGHT = 900

    def __init__(self):
        self.running = True
        self.player = Player()
        self.level = Level()
        self.xpos, self.ypos = 400, 300
        self.display_surface = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
        self.image = None

    def on_initialisation(self):
        pygame.init()

        icon = pygame.image.load('swords.png').convert()
        pygame.display.set_icon(icon)
        pygame.display.set_caption('Marks Maths Adventure')

    def quit(self):
        pygame.quit()

    def screen(self):
        self.display_surface.fill((SKY))
        self.level.draw()
        self.display_surface.blit(self.player.image, self.player.rect)
        pygame.draw.rect(self.display_surface, (0, 0, 0), self.player.rect, 2)
        pygame.display.flip()

    def collision(self):
        # Collision detection
        for tile in self.level.tiles:
            if tile[1].colliderect(self.player.rect.x -10, self.player.rect.y + 30, self.player.width, self.player.height):
                if self.player.yVel <= 0:
                    self.player.yVel = 0
                    #bottom collision
                elif self.player.yVel >= 0:
                    self.player.yVel = 0





    def check(self):
        while self.running:
            clock.tick(FPS)
            self.screen()
            self.player.move()
            self.collision()

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.running = False

            if self.player.rect.y > WORLD_FLOOR:
                self.player.rect.y = WORLD_FLOOR

            self.on_initialisation()
            self.screen()
            pygame.event.pump()
            print(self.player.rect.x, self.player.rect.y)


if __name__ == '__main__':
    game = Game()
    game.check()