Untitled
unknown
plain_text
9 months ago
6.8 kB
11
Indexable
import pygame
pygame.init()
# Screen dimensions
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Platformer Game")
# Loading Images
dirt = pygame.image.load("Game_2\\Images\\dirt.png")
grass = pygame.image.load("Game_2\\Images\\grass.png")
# Player class and functions
def check(rect,list_):
li = []
for i in list_:
if rect.colliderect(i):
li.append(i)
return li
def collisions(rect,list_):
collision_types = {'bottom': False, 'top': False, 'left': False, 'right': False}
list_ = check(rect, list_)
for tile in list_:
if abs(rect.bottom - tile.top) == 2:
if rect.right <= tile.left:
if rect.left >= tile.right:
collision_types['bottom'] = True
if abs(rect.top - tile.bottom) == 2:
if rect.right >= tile.left:
collision_types['bottom'] = True
if rect.left <= tile.right:
collision_types['bottom'] = True
if rect.right == tile.left:
if rect.bottom >= tile.top:
collision_types['right'] = True
if rect.top <= tile.bottom:
collision_types['right'] = True
if rect.left == tile.right:
if rect.bottom >= tile.top:
collision_types['left'] = True
if rect.top <= tile.bottom:
collision_types['left'] = True
return collision_types
class Player(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.image = pygame.transform.scale(pygame.image.load("Game_2\\Images\\test img2.png"), (24, 32))
self.image.set_colorkey((255, 255, 255))
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.speed = 5
self.gravity = 0.8
self.jump_speed = 10
self.is_jumping = False
self.vel_y = 0
self.falling = True
self.movement = [0,0]
self.vel_x = 0
def update(self, group, list_):
keys = pygame.key.get_pressed()
# Gravity and jumping
if self.falling:
self.vel_y += self.gravity
if keys[pygame.K_SPACE] and not self.is_jumping:
self.vel_y = -self.jump_speed
self.is_jumping = True
self.falling = True
# Collision detection with ground and platforms
if not self.is_jumping:
if self.rect.bottom >= screen_height:
self.rect.bottom = screen_height
self.vel_y = 0
self.is_jumping = False
self.falling = False
if self.rect.bottom >= screen_height:
self.is_jumping = False
collision = collisions(self.rect, list_)
if collision['bottom'] == True:
if self.vel_y != 0:
self.vel_y = 0
self.falling = False
self.is_jumping = False
if collision['top'] == True:
self.vel_y = 0
self.falling = True
self.is_jumping = True
if collision['left'] == True and self.vel_x < 0:
self.vel_x = 0
print(collision)
if keys[pygame.K_r]:
self.rect.x = 0
self.rect.y = 0
self.vel_y = 0
self.falling = True
if self.rect.top <= 0:
self.vel_y = 0
self.falling = True
self.rect.y += self.vel_y
# Platform class
class Platform(pygame.sprite.Sprite):
def __init__(self, x, y, image):
super().__init__()
self.image = image
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.vel_x = 0
def update(self, *args):
keys = pygame.key.get_pressed()
self.vel_x = player.vel_x
self.rect.x += self.vel_x
# Create game objects
game_map = [[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,2,2,2,2,2,2,2,0,0,0,0,2,2,2,2,2,2,2,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,2,2,2,2,2,2,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2],
[1,1,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,1,1],
[1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]]
all_sprites = pygame.sprite.Group()
plfs = pygame.sprite.Group()
player = Player(100, 350)
all_sprites.add(player)
clock = pygame.time.Clock()
p_d = False
tiles = []
# Game loop
running = True
while running:
movement = [0,0]
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if not p_d:
y = 0
for rows in game_map:
x = 0
for col in rows:
if col == 1:
pd = Platform(x*25, y*25, dirt)
plfs.add(pd)
tiles.append(pygame.Rect(x*25,y*25,25,25))
if col == 2:
pg = Platform(x*25, y*25, grass)
plfs.add(pg)
tiles.append(pygame.Rect(x*25,y*25,25,25))
x += 1
y += 1
p_d = True
all_sprites.update(plfs, tiles)
plfs.update(player)
movement = player.movement
screen.fill((0, 0, 0))
plfs.draw(screen)
all_sprites.draw(screen)
pygame.display.update()
clock.tick(30)
pygame.quit()Editor is loading...
Leave a Comment