Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge Dev to Main #5

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 37 additions & 20 deletions code/level.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pygame
from tiles import Tile
from settings import tile_size, screen_width, level_map2
from settings import tile_size, screen_width, level_map2, screen_height
from player import Player
from enemies import Enemy
from Door import Door
@@ -112,19 +112,20 @@ def change_level(self):
self.setup_level(level_map2)

def vertical_enemy_collision(self):
for enemy in self.enemies.sprites():
temp_rect = enemy.rect.copy()
temp_rect.y += 1
flag = False
for sprite in self.tiles.sprites():
if sprite.rect.colliderect(temp_rect):
enemy.on_ground = True
flag = True
if not flag:
if enemy.direction.x < 0:
enemy.direction.x = 1
elif enemy.direction.x > 0:
enemy.direction.x = -1
pass
# for enemy in self.enemies.sprites():
# temp_rect = enemy.rect.copy()
# temp_rect.y += 1
# flag = False
# for sprite in self.tiles.sprites():
# if sprite.rect.colliderect(temp_rect):
# enemy.on_ground = True
# flag = True
# if not flag:
# if enemy.direction.x < 0:
# enemy.direction.x = 1
# elif enemy.direction.x > 0:
# enemy.direction.x = -1


def horizontal_movement_collision(self):
@@ -134,7 +135,7 @@ def horizontal_movement_collision(self):
#merging knock back, wall collision and invisble frames
for enemy in self.enemies.sprites():
for sprite in self.tiles.sprites():
if enemy.rect.colliderect(player.rect) and not player.is_invincible:
if enemy.rect.colliderect(player.rect) and not player.is_invincible and player.state != 'attack':


# check if player is on the left of enemy or right and knock in that direction
@@ -166,6 +167,8 @@ def horizontal_movement_collision(self):
player.jump()
player.on_ground = False
# check collision with the wall
if enemy.rect.colliderect(player.rect) and not player.is_invincible and player.state == 'attack' and player.direction.x != enemy.direction.x:
enemy.kill()
if sprite.rect.colliderect(player.rect) and sprite != self.doors.sprite:
if player.direction.x < 0:
player.rect.left = sprite.rect.right
@@ -174,31 +177,40 @@ def horizontal_movement_collision(self):
player.rect.right = sprite.rect.left
self.world_shift = 0


def vertical_movement_collision(self):
player = self.player.sprite
player.apply_gravity()

#merging vertical knock back, wall collision and invislbe frames
for enemy in self.enemies.sprites():
for sprite in self.tiles.sprites():
if enemy.rect.colliderect(player.rect) and not player.is_invincible:
if player.rect.left <= enemy.rect.left:
if enemy.rect.colliderect(player.rect) and not player.is_invincible and player.state != 'jumpAttack':
if player.rect.left < enemy.rect.left:
#player.direction.y = -15
player.is_attacked(-1)
if enemy.direction.x < 0:
enemy.direction.x = 1
elif enemy.direction.x > 0:
enemy.direction.x = -1
elif player.rect.left >= enemy.rect.left:
elif player.rect.left > enemy.rect.left:
#player.direction.y = -15
player.is_attacked(1)
if enemy.direction.x < 0:
enemy.direction.x = 1
elif enemy.direction.x > 0:
enemy.direction.x = -1
else:
player.is_attacked(player.direction.x)
if enemy.direction.x < 0:
enemy.direction.x = 1
elif enemy.direction.x > 0:
enemy.direction.x = -1

player.jump()
player.on_ground = False
player.on_ground = False
if enemy.rect.colliderect(player.rect) and not player.is_invincible and player.state == 'jumpAttack':
enemy.kill()
if sprite.rect.colliderect(player.rect):
if player.direction.y > 0:
player.rect.bottom = sprite.rect.top
@@ -234,3 +246,8 @@ def run(self):
self.horizontal_enemy_collision()
self.enemies.update(self.world_shift)
self.enemies.draw(self.display_surface)

def check_player(self):
if self.player.sprite.current_health == 0 or (self.player.sprite.rect.left < 0 or self.player.sprite.rect.right > screen_width) or (self.player.sprite.rect.bottom > screen_height or self.player.sprite.rect.bottom < -64):
return 0
return 1
79 changes: 63 additions & 16 deletions code/main.py
Original file line number Diff line number Diff line change
@@ -5,23 +5,30 @@
from button import Button
from os import path

class Main:

# Pygame setup
pygame.init()
screen = pygame.display.set_mode((screen_width,screen_height))
clock = pygame.time.Clock()
level = Level(level_map, screen)
main_menu = True
start_img = pygame.image.load(path.join(base_path , 'buttons', 'start-1.png')).convert_alpha()
exit_img= pygame.image.load(path.join(base_path, 'buttons','exit-1.png')).convert_alpha()
start_button = Button(screen_width//2 - start_img.get_width()//2, 200, start_img)
exit_button = Button(screen_width//2 - exit_img.get_width()//2, 400, exit_img)
pygame.display.set_caption("2D Platformer Game")

while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
def __init__(self) -> None:
pygame.init()
self.screen = pygame.display.set_mode((screen_width,screen_height))
self.clock = pygame.time.Clock()
self.level = Level(level_map, self.screen)
self.main_menu = True
self.start_img = pygame.image.load(base_path + '\\buttons\\start-1.png').convert_alpha()
self.exit_img= pygame.image.load(base_path + '\\buttons\\exit-1.png').convert_alpha()
self.menu_img = pygame.image.load(path.join(base_path, 'title.png')).convert_alpha()

self.start_button = Button(screen_width//2 - self.start_img.get_width()//2, 500, self.start_img)
self.exit_button = Button(screen_width//2 - self.exit_img.get_width()//2, 600, self.exit_img)
pygame.display.set_caption("2D Platformer Game")


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

background = pygame.image.load(path.join(base_path, 'bg.png')).convert()
background = pygame.transform.smoothscale(background, screen.get_size())
@@ -43,3 +50,43 @@

pygame.display.update()
clock.tick(clock_tick)
# Check if the game is in the main menu state
if self.main_menu:
# Load the main menu background image
background_img = pygame.image.load(path.join(base_path, 'sky1.png')).convert()
else:
# Load the level background image
background_img = pygame.image.load(path.join(base_path, 'bg.png')).convert()

# Resize the background image to match the screen size
background_img = pygame.transform.smoothscale(background_img, self.screen.get_size())

# Blit the background image onto the screen
self.screen.blit(background_img, (0, 0))

# Handle game logic based on the current state
if not self.main_menu:
self.level.run()
if not self.level.check_player():
return 1

if self.main_menu == True:
# Draw the main menu buttons
self.screen.blit(self.menu_img, (screen_width//2 - self.menu_img.get_width()//2, 50))
self.start_button.draw(self.screen)
self.exit_button.draw(self.screen)

if self.start_button.clicked:
self.main_menu = False

if self.exit_button.clicked:
return 2

pygame.display.update()
self.clock.tick(clock_tick)

if __name__ == '__main__':
main = Main()
while main.run() != 2:
main = Main()
main.run()
44 changes: 36 additions & 8 deletions code/player.py
Original file line number Diff line number Diff line change
@@ -85,14 +85,16 @@ def animate(self):
if self.state == 'dead':
self.frame_index = len(animation) - 1
elif self.state == 'attack' or self.state == 'jumpAttack':
self.is_attacking = False
self.is_attacking = True
self.state = 'idle'
else:
self.is_attacking = False
self.frame_index = 0


# Flip the image based on the direction
image = animation[int(self.frame_index)%len(animation)]
if self.is_invincible:
if self.is_invincible: # if player is invincible then make every alternate frame blank
if (int(self.frame_index)%len(animation)) % 2 == 0:
image = pygame.mask.from_surface(image).to_surface()
image.set_colorkey((0,0,0))
@@ -107,6 +109,7 @@ def update_state(self):
self.attacked_c = 0
# checking if the player has already been knocked back then no need to knock back further.
if self.is_attacking:
#self.direction.x = 0
return
elif self.direction.x > 0:
self.facing_right = True
@@ -149,18 +152,34 @@ def is_attacked(self,dir=0):
#self.jump()

def get_input(self):
if self.is_attacking:
self.direction.x = 0
return
keys = pygame.key.get_pressed()
mouse_buttons = pygame.mouse.get_pressed()

if self.is_attacking:
# if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
# self.direction.x = 1
# elif keys[pygame.K_LEFT] or keys[pygame.K_a]:
# self.direction.x = -1
# #self.direction.x = 0
# else:
#self.direction.x = 0
if keys[pygame.K_UP] or keys[pygame.K_w] or keys[pygame.K_SPACE]:
if not self.jump_key_pressed:
self.jump()
self.jump_key_pressed = True
else:
self.jump_key_pressed = False
else:
self.direction.x = 0
return

if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
self.direction.x = 1
elif keys[pygame.K_LEFT] or keys[pygame.K_a]:
self.direction.x = -1
else:
self.direction.x = 0


# force the player to move in the dirction of the kock back.
if self.is_invincible and not self.on_ground and self.attacked_c:
@@ -175,11 +194,20 @@ def get_input(self):

# Check for attack input (left mouse click or 'F' key)
if mouse_buttons[0] or keys[pygame.K_f]:
self.attack()
#self.attack()
if self.on_ground:
self.state = 'attack'
else:
self.state = 'jumpAttack'
self.frame_index = 0
self.is_attacking = True

else:
self.is_attacking = False

def attack(self):
if not self.is_attacking:
self.is_attacking = True
#self.is_attacking = True
if self.on_ground:
self.state = 'attack'
else:
@@ -203,4 +231,4 @@ def jump(self,wind=0):
def update(self):
self.get_input()
self.update_state()
self.animate()
self.animate()
3 changes: 2 additions & 1 deletion code/settings.py
Original file line number Diff line number Diff line change
@@ -44,6 +44,7 @@
]

base_path = path.join(path.dirname(__file__), '..', 'graphics')
#base_path = "C:\\Users\\Dhruv\\Desktop\\workshop\\Platformer_Game_DeCoded\\graphics\\"

tile_types = {
'T': path.join(base_path, 'Tiles', 'grassMid.png'),
@@ -60,7 +61,7 @@
def import_folder(path_to_folder):
surf_lst = []
for _,_,img_files in walk(path_to_folder):
for img in img_files:
for img in img_files:
full_path = path.join(path_to_folder, img)
img_surf = pygame.image.load(full_path).convert_alpha()
surf_lst.append(img_surf)
Binary file modified graphics/bg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added graphics/bg1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added graphics/sky1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.