import pygame import random import sys
Khởi tạo Pygame
pygame.init()
Cấu hình màn hình (Full màn hình)
info = pygame.display.Info() SW, SH = info.currentw, info.currenth screen = pygame.display.setmode((SW, SH), pygame.FULLSCREEN) pygame.display.setcaption("Boss Rush Driver - Pygame Edition") clock = pygame.time.Clock()
Màu sắc
BGCOLOR = (26, 26, 26) PLAYERCOLOR = (0, 255, 255) ENEMYCOLOR = (255, 165, 0) BOSSP1COLOR = (255, 0, 0) BOSSP2COLOR = (128, 0, 128) # Tím khi sang Phase 2 BULLETPLAYER = (255, 255, 0) BULLETBOSS = (255, 50, 50) MAGENTACAR = (255, 0, 255)
class Bullet: def init(self, x, y, speed, color, width=10, height=20): self.rect = pygame.Rect(x, y, width, height) self.speed = speed self.color = color
def update(self):
self.rect.y += self.speed
class Enemy: def init(self, x, y, speed, color): self.rect = pygame.Rect(x, y, 60, 100) self.speed = speed self.color = color
def update(self):
self.rect.y += self.speed
class Game: def init(self): self.fontmain = pygame.font.SysFont("Arial", 30) self.fontbig = pygame.font.SysFont("Arial", 70, bold=True) self.hi_score = 0 self.reset()
def reset(self):
self.score = 0
self.player_rect = pygame.Rect(SW//2 - 30, SH - 150, 60, 100)
self.player_bullets = []
self.boss_bullets = []
self.enemies = []
# Boss stats
self.is_boss_spawned = False
self.boss_rect = pygame.Rect(SW//2 - 150, -250, 300, 150)
self.boss_hp = 50
self.boss_phase = 1
self.boss_dir = 1
self.game_over = False
self.win = False
# Timers
self.last_spawn = pygame.time.get_ticks()
self.last_boss_shot = pygame.time.get_ticks()
def handle_input(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and self.player_rect.left > 0:
self.player_rect.x -= 10
if keys[pygame.K_RIGHT] and self.player_rect.right < SW:
self.player_rect.x += 10
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit(); sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit(); sys.exit()
if event.key == pygame.K_SPACE and not self.game_over:
# Bắn đạn
b = Bullet(self.player_rect.centerx - 5, self.player_rect.top - 20, -15, BULLET_PLAYER)
self.player_bullets.append(b)
if event.key == pygame.K_r and self.game_over:
self.reset()
def update(self):
if self.game_over: return
now = pygame.time.get_ticks()
# Spawn quái thường nếu chưa có Boss
if self.score < 150 and not self.is_boss_spawned:
spawn_rate = max(300, 800 - (self.score * 2))
if now - self.last_spawn > spawn_rate:
x = random.randint(50, SW - 110)
self.enemies.append(Enemy(x, -100, random.randint(7, 12), ENEMY_COLOR))
self.last_spawn = now
elif self.score >= 150 and not self.is_boss_spawned:
self.is_boss_spawned = True
# Logic Boss
if self.is_boss_spawned:
# Xuất hiện Boss (di chuyển xuống màn hình)
if self.boss_rect.y < 50:
self.boss_rect.y += 2
# Kiểm tra Phase
if self.boss_hp <= 25:
self.boss_phase = 2
# Boss di chuyển qua lại
move_speed = 5 if self.boss_phase == 1 else 12
self.boss_rect.x += move_speed * self.boss_dir
if self.boss_rect.right >= SW or self.boss_rect.left <= 0:
self.boss_dir *= -1
# Boss bắn đạn và gọi xe hồng
shot_delay = 1000 if self.boss_phase == 1 else 400
if now - self.last_boss_shot > shot_delay:
# Bắn đạn đỏ
self.boss_bullets.append(Bullet(self.boss_rect.centerx - 10, self.boss_rect.bottom, 10 if self.boss_phase == 1 else 16, BULLET_BOSS, 20, 40))
# Gọi xe hồng (Magenta)
spawn_chance = 0.4 if self.boss_phase == 1 else 0.8
if random.random() < spawn_chance:
x = random.randint(self.boss_rect.left, self.boss_rect.right - 60)
speed = 15 if self.boss_phase == 1 else 22
self.enemies.append(Enemy(x, self.boss_rect.bottom, speed, MAGENTA_CAR))
self.last_boss_shot = now
# Cập nhật đạn Player
for b in self.player_bullets[:]:
b.update()
if b.rect.bottom < 0: self.player_bullets.remove(b)
else:
# Đạn trúng quái
for e in self.enemies[:]:
if b.rect.colliderect(e.rect):
if b in self.player_bullets: self.player_bullets.remove(b)
self.enemies.remove(e)
self.score += 2
# Đạn trúng Boss
if self.is_boss_spawned and b.rect.colliderect(self.boss_rect):
if b in self.player_bullets: self.player_bullets.remove(b)
self.boss_hp -= 1
if self.boss_hp <= 0:
self.win = True
self.game_over = True
# Cập nhật đạn Boss
for b in self.boss_bullets[:]:
b.update()
if b.rect.top > SH: self.boss_bullets.remove(b)
if b.rect.colliderect(self.player_rect):
self.game_over = True
# Cập nhật Enemies
for e in self.enemies[:]:
e.update()
if e.rect.top > SH:
self.enemies.remove(e)
self.score += 1
if e.rect.colliderect(self.player_rect):
self.game_over = True
# Va chạm với thân Boss
if self.is_boss_spawned and self.player_rect.colliderect(self.boss_rect):
self.game_over = True
if self.score > self.hi_score: self.hi_score = self.score
def draw(self):
screen.fill(BG_COLOR)
# Vẽ Player
pygame.draw.rect(screen, PLAYER_COLOR, self.player_rect)
pygame.draw.rect(screen, (255, 255, 255), self.player_rect, 2)
# Vẽ Enemies
for e in self.enemies:
pygame.draw.rect(screen, e.color, e.rect)
pygame.draw.rect(screen, (255, 255, 255), e.rect, 1)
# Vẽ đạn
for b in self.player_bullets:
pygame.draw.rect(screen, b.color, b.rect)
for b in self.boss_bullets:
pygame.draw.rect(screen, b.color, b.rect)
# Vẽ Boss
if self.is_boss_spawned:
color = BOSS_P1_COLOR if self.boss_phase == 1 else BOSS_P2_COLOR
pygame.draw.rect(screen, color, self.boss_rect)
pygame.draw.rect(screen, (255, 255, 255), self.boss_rect, 5)
# HP Boss
hp_text = self.font_main.render(f"BOSS HP: {self.boss_hp} (PHASE {self.boss_phase})", True, (255, 50, 50))
screen.blit(hp_text, (SW - 350, 50))
# HUD
score_txt = self.font_main.render(f"Score: {self.score}", True, (255, 255, 255))
hi_txt = self.font_main.render(f"High Score: {self.hi_score}", True, (255, 255, 0))
screen.blit(score_txt, (50, 50))
screen.blit(hi_txt, (50, 90))
if self.game_over:
msg = "CONGRATULATIONS!" if self.win else "GAME OVER"
color = (0, 255, 0) if self.win else (255, 255, 255)
over_txt = self.font_big.render(msg, True, color)
restart_txt = self.font_main.render("Press 'R' to Restart or 'ESC' to Quit", True, (200, 200, 200))
screen.blit(over_txt, (SW//2 - over_txt.get_width()//2, SH//2 - 50))
screen.blit(restart_txt, (SW//2 - restart_txt.get_width()//2, SH//2 + 50))
pygame.display.flip()
Chạy game
game = Game() while True: game.handle_input() game.update() game.draw() clock.tick(144) # 144 FPS
Bình luận