import pygame
import random
import sys
# 初始化
pygame.init()
W, H = 800, 600
screen = pygame.display.set_mode((W, H))
pygame.display.set_caption("世界杯:亲手踢到决赛")
clock = pygame.time.Clock()
FPS = 60
# 颜色
GREEN = (34, 139, 34)
WHITE = (255,255,255)
RED = (255,0,0)
BLUE = (0,0,255)
BLACK = (0,0,0)
GRAY = (200,200,200)
# 字体
font = pygame.font.SysFont("simhei", 24)
big_font = pygame.font.SysFont("simhei", 40)
# 队伍
teams = ["德国","法国","巴西","阿根廷","西班牙","葡萄牙","比利时","英格兰",
"荷兰","乌拉圭","克罗地亚","摩洛哥","瑞士","美国","墨西哥","日本"]
random.shuffle(teams)
player_team = teams[0]
enemy_teams = teams[1:]
stage = "小组赛"
match_no = 0
wins = 0
player_score = 0
enemy_score = 0
game_state = "playing" # playing, end
# 玩家
player = pygame.Rect(W//2 - 15, H-80, 30, 50)
ball = pygame.Rect(W//2 - 10, H//2 - 10, 20, 20)
ball_speed_x = 0
ball_speed_y = 0
ball_owned = True
# 球门
goal_top = pygame.Rect(W//2 - 100, 0, 200, 20)
goal_bot = pygame.Rect(W//2 - 100, H-20, 200, 20)
# AI防守队员
ai = pygame.Rect(W//2-25, 150, 50, 50)
def reset_match():
global player_score, enemy_score, ball_owned, ball_speed_x, ball_speed_y, game_state
player.x = W//2-15
player.y = H-80
ball.x = player.x + 15
ball.y = player.y - 20
ball_speed_x = 0
ball_speed_y = 0
ball_owned = True
ai.x = W//2-25
ai.y = 150
game_state = "playing"
def draw_text(text, x, y, color=WHITE, size=24):
f = pygame.font.SysFont("simhei", size)
surf = f.render(text, True, color)
screen.blit(surf, (x,y))
# 主循环
running = True
while running:
screen.fill(GREEN)
pygame.draw.rect(screen, GRAY, (0,0,W,H), 10)
pygame.draw.rect(screen, WHITE, (W//2-100, 0, 200, 20))
pygame.draw.rect(screen, WHITE, (W//2-100, H-20, 200, 20))
for e in pygame.event.get():
if e.type == pygame.QUIT:
running = False
if e.type == pygame.KEYDOWN:
if e.key == pygame.K_SPACE and ball_owned and game_state=="playing":
# 射门!
ball_speed_y = -12
ball_speed_x = random.choice([-2,-1,0,1,2])
ball_owned = False
if e.key == pygame.K_r and game_state=="end":
reset_match()
# 移动玩家
keys = pygame.key.get_pressed()
if game_state == "playing":
if keys[pygame.K_LEFT] and player.left > 0:
player.x -= 8
if keys[pygame.K_RIGHT] and player.right < W:
player.x += 8
if keys[pygame.K_UP] and player.top > H//2:
player.y -= 5
if keys[pygame.K_DOWN] and player.bottom < H:
player.y += 5
# 球跟着人
if ball_owned:
ball.centerx = player.centerx
ball.centery = player.top - 10
# 球飞行
else:
ball.x += ball_speed_x
ball.y += ball_speed_y
ball_speed_y += 0.4 # 重力
# 边界反弹
if ball.left < 0 or ball.right > W:
ball_speed_x *= -1
if ball.top < 0:
ball_speed_y *= -0.7
if ball.bottom > H:
reset_match()
# AI移动
if game_state == "playing":
if ai.centerx < ball.centerx and ai.left>0:
ai.x += 3
if ai.centerx > ball.centerx and ai.right= H:
enemy_score += 1
draw_text("被进球…", W//2-80, H//2, BLUE, 50)
game_state = "end"
# 绘制
pygame.draw.rect(screen, BLUE, player)
pygame.draw.ellipse(screen, WHITE, ball)
pygame.draw.rect(screen, RED, ai)
# 信息
draw_text(f"你的队伍: {player_team}", 20, 20)
draw_text(f"阶段: {stage}", 20, 50)
draw_text(f"比分: {player_score} - {enemy_score}", W//2-50, 20)
draw_text("方向键移动 空格射门", W//2-120, H-40)
if game_state == "end":
draw_text("按 R 重开", W//2-70, H//2+50, WHITE, 30)
# 赢了就晋级
if player_score >= 3:
draw_text("你赢了!晋级!", W//2-120, H//2+100, WHITE, 35)
if keys[pygame.K_r]:
match_no += 1
player_score = enemy_score = 0
if match_no < 4:
stage = "小组赛"
elif match_no <6:
stage = "淘汰赛"
elif match_no <7:
stage = "半决赛"
else:
stage = "决赛"
reset_match()
pygame.display.flip()
clock.tick(FPS)
pygame.quit()
sys.exit()