Python Pygame kazanma ekranı nasıl yapılır?

PCsoruları

Femtopat
Katılım
11 Haziran 2024
Mesajlar
37
Daha fazla  
Cinsiyet
Erkek
Kod:
İmport pygame.
İmport random.
From pygame. Locals import *
İmport time.

pygame.init()

# Screen dimensions.
Screen_width = 800.
Screen_height = 600.

# Initialize pygame screen.
Screen = pygame. Display. Set_mode((screen_width, screen_height))
Pygame. Display. Set_caption('simple Game')

# Game variables for the first Game.
Player_width = 50.
Player_height = 50.
Player_pos2 = [screen_width - 100, 270]
Player_pos1 = [screen_width - 100, 430]
Player_speed = 5
Enemy1_width = 50.
Enemy1_height = 50.
Enemy1_speed = 3
Enemies1 = []
score1 = 0
game_over1 = False.
win1 = False.
game_active = True.

# Game variables for the second game.
player2_width = 50.
normal_height = 50.
crouch_height = 25.
player_pos = [0, screen_height - normal_height]
Player_speed = 5
Player_current_height = normal_height.
Enemy2_width = 40.
Enemy2_height = 25.
İnitial_enemy2_speed = 3
Enemy2_speed = initial_enemy2_speed.
Enemies2 = []
score2 = 0
game_over2 = False.
win2 = False.
start_ticks = pygame.time.get_ticks()
boom = 1000.
start2_ticks = pygame.time.get_ticks()

# Function to spawn enemy for the first game.
def spawn_enemy_game1():
x = 0
y = random.randint(60, screen_height - 110)
enemies1.append(pygame.Rect(x, y, enemy1_width, enemy1_height))

# Function to spawn enemy for the second game.
def spawn_enemy_game2():
y = random.choice([screen_height - 50, screen_height - 25])
Enemy = pygame. Rect(screen_width, Y, enemy2_width, enemy2_height)
Enemies2.append(enemy)

# Function to check collision with Player'in the first Game.
Def check_collision_game1(player_rect1, player_rect2):
For enemy in enemies1:
İf player_rect1.colliderect(enemy) or player_rect2.colliderect(enemy):
Return true.
Return false.

# Function to check collision with Player'in the second Game.
Def check_collision_game2(player_rect):
For enemy in enemies2:
İf player_rect. Colliderect(enemy):
Return true.
Return false.

# Function to restart Game for the first Game.
Def restart_game1():
Global player_pos1, player_pos2, enemies1, game_over1, Win1, score1.
Player_pos1 = [screen_width - 100, 430]
Player_pos2 = [screen_width - 100, 270]
enemies1.clear()
Game_over1 = false.
Win1 = false.
Score1 = 0

# Function to restart Game for the second Game.
Def restart_game2():
Global player_pos, enemies2, game_over2, Win2, enemy2_speed, start_ticks, player_current_height, score2, Player2_width, boom, start2_ticks.
Player_current_height = normal_height.
Player_pos = [0, screen_height - normal_height]
enemies2.clear()
Game_over2 = false.
Win2 = false.
Enemy2_speed = initial_enemy2_speed.
Start_ticks = pygame.time.get_ticks()
Start2_ticks = pygame.time.get_ticks()
Score2 = 0
Player2_width = 50.
Boom = 1000.

# Menu function.
Def show_menu():
Screen. Fill((0, 0, 0))
Menu_font = pygame. Font. Sysfont(none, 60)
Menu_text1 = menu_font. Render('press 1 to open first Game', true, (255, 255, 255))
Menu_text2 = menu_font. Render('press 2 to open the other Game', true, (255, 255, 255))
Screen. Blit(menu_text1, (screen_width // 2 - menu_text1.get_width() // 2, screen_height // 2 - 30))
Screen. Blit(menu_text2, (screen_width // 2 - menu_text2.get_width() // 2, screen_height // 2 + 30))
pygame.display.flip()

# Function to show pause menu.
Def show_pause_menu():
Screen. Fill((0, 0, 0))
Pause_font = pygame. Font. Sysfont(none, 60)
Pause_text = pause_font. Render('press space to continue', true, (255, 255, 255))
Screen. Blit(pause_text, (screen_width // 2 - pause_text.get_width() // 2, screen_height // 2 - 30))
pygame.display.flip()

# Function to show Game over menu.
Def show_game_over_menu():
Screen. Fill((0, 0, 0))
Game_over_font = pygame. Font. Sysfont(none, 60)
Game_over_text = game_over_font. Render("Game over!", true, (255, 0, 0))
Screen. Blit(game_over_text, (screen_width // 2 - game_over_text.get_width() // 2, screen_height // 2 - 30))
pygame.display.flip()

# Function to show Win menu.
Def show_win_menu():
Global game_active.
Screen. Fill((0, 0, 0))
Win_font = pygame. Font. Sysfont(none, 60)
Win_text = win_font. Render("you Win!", true, (0, 255, 0))
Screen. Blit(win_text, (screen_width // 2 - win_text.get_width() // 2, screen_height // 2 - 30))
pygame.display.flip()
Game_active = false.

# Game loop.
Clock = pygame.time.Clock()
Running = true.
İn_menu = true.
Game1_active = false.
Game2_active = false.
Paused = false.
Show_pause = false.

While running:
Dt = clock. Tick(60) / 1000.0

For Event'in pygame.event.get():
İf Event. Type == quıt:
Running = false.
İf Event. Type == pygame. Keydown:
İf Win2:
İf Event. Key == pygame. K_r:
Paused = false.
Win2 = false # reset Win2 to avoid re-entering this block immediately.
restart_game2()
Elif Event. Type == keydown:
İf Event. Key == k_escape:
İf not in_menu:
İf not paused:
Paused = true.
Show_pause = true.
Else:
Paused = false.
Show_pause = false.
Elif Event. Key == k_space and show_pause:
Paused = false.
Show_pause = false.
Elif Event. Key == k_m:
İf not in_menu:
İn_menu = true.
Game1_active = false.
Game2_active = false.
Elif Event. Key == K_1:
İf in_menu:
İn_menu = false.
Game1_active = true.
Game2_active = false.
restart_game1()
Elif Event. Key == K_2:
İf in_menu:
İn_menu = false.
Game1_active = false.
Game2_active = true.
restart_game2()
Elif Event. Key == k_r:
İf Game1_active and game_over1:
restart_game1()
İf Game2_active and game_over2:
restart_game2()
Elif Event. Key == k_down:
İf Game2_active:
Player_current_height = crouch_height.
Player_pos[1] = screen_height - crouch_height.
Elif Event. Key == k_up:
İf Game2_active:
Player_current_height = crouch_height.
Player_pos[1] = 550.

Current_time = pygame.time.get_ticks()

# Calculate the elapsed time.
Elapsed_time = (current_time - start2_ticks) / 1000 # convert milliseconds to seconds.

# Adjust boom and Player width based on elapsed time.
İf 10 <= score2 < 20:
Boom = 500.
Player2_width = 25.
İf 20 <= score2 < 30:
Boom = 400.
Player2_width = 12.5
İf 30 <= elapsed_time <= 40:
Boom = 300.
İf 40 <= score2:
Boom = 250.

İf in_menu:
show_menu()
Elif show_pause:
show_pause_menu()
Elif Game1_active:
İf not paused:
Keys = pygame.key.get_pressed()
İf keys[K_DOWN] and player_pos2[1] + player_height < screen_height:
Player_pos2[1] += player_speed.
Player_pos1[1] += player_speed.
İf keys[K_UP] and player_pos2[1] > 0:
Player_pos2[1] -= player_speed.
Player_pos1[1] -= player_speed.

Player_pos1[1] = Max(0, min(player_pos1[1], screen_height - player_height))
Player_pos2[1] = Max(0, min(player_pos2[1], 390))

Player_rect1 = pygame. Rect(player_pos1[0], player_pos1[1], player_width, player_height)
Player_rect2 = pygame. Rect(player_pos2[0], player_pos2[1], player_width, player_height)

Middle_top = min(player_pos1[1], player_pos2[1]) + player_height.
Middle_bottom = Max(player_pos1[1], player_pos2[1])
Middle_rect = pygame. Rect(player_pos1[0], middle_top, 60, middle_bottom - middle_top)

İf random. Randint(1, 100) == 1:
spawn_enemy_game1()

For enemy in enemies1[:]:
Enemy. X += enemy1_speed.
İf enemy. X > screen_width:
Enemies1.remove(enemy)

İf check_collision_game1(player_rect1, player_rect2):
Game_over1 = true.

Screen. Fill((0, 0, 0))

Pygame. Draw. Rect(screen, (255, 255, 255), player_rect1)
Pygame. Draw. Rect(screen, (255, 255, 255), player_rect2)

For enemy in enemies1[:]:
Pygame. Draw. Rect(screen, (255, 0, 0), enemy)
İf enemy. Colliderect(middle_rect):
Score1 += 1
Enemies1.remove(enemy)

Score_font = pygame. Font. Sysfont(none, 36)
Score_text = score_font. Render(F"score: {score1}", true, (255, 255, 255))
Screen. Blit(score_text, (10, 10))

İf game_over1:
show_game_over_menu()

Elif Game2_active:
İf not paused:
Keys = pygame.key.get_pressed()
İf keys[K_UP]:
Player_current_height = crouch_height.
Player_pos[1] = 550.
Elif keys[K_DOWN]:
Player_current_height = crouch_height.
Player_pos[1] = screen_height - crouch_height.
Else:
Player_current_height = 50.
Player_pos[1] = 550.

İf pygame.time.get_ticks() - start_ticks > boom:
spawn_enemy_game2()
Start_ticks = pygame.time.get_ticks()

For enemy in enemies2[:]:
Enemy. X -= enemy2_speed.
İf enemy. X < 0:
Score2 += 1
Enemies2.remove(enemy)
İf score2 >= 1:
Win2 = true.

İf check_collision_game2(pygame. Rect(player_pos[0], player_pos[1], Player2_width, player_current_height)):
Game_over2 = true.

Screen. Fill((0, 0, 0))

Pygame. Draw. Rect(screen, (255, 255, 255),
Pygame. Rect(player_pos[0], player_pos[1], Player2_width, player_current_height))

For enemy in enemies2[:]:
Pygame. Draw. Rect(screen, (255, 0, 0), enemy)

Score_font = pygame. Font. Sysfont(none, 36)
Score_text = score_font. Render(F"score: {score2}", true, (255, 255, 255))
Screen. Blit(score_text, (10, 10))

İf game_over2:
show_game_over_menu()

pygame.display.flip()

pygame.quit()

Win2 = true olunca bir you Win yazan ekran çıkmalı sonra m ile menüye gitmeli r ile oyunu tekrar başlatmalı.

Yardımcı olur musunuz?
 
Son düzenleyen: Moderatör:

Technopat Haberler

Geri
Yukarı