作业

import pygame
import sys
import random

class NumberGuessingGame:
def init(self):
# 初始化Pygame
pygame.init()
pygame.font.init()

    # 设置窗口
    self.width, self.height = 800, 600
    self.screen = pygame.display.set_mode((self.width, self.height))
    pygame.display.set_caption("数字猜测游戏")
    
    # 设置字体
    self.font_large = pygame.font.SysFont("SimHei", 48)
    self.font_medium = pygame.font.SysFont("SimHei", 36)
    self.font_small = pygame.font.SysFont("SimHei", 24)
    
    # 游戏变量
    self.secret_number = random.randint(1, 100)
    self.max_attempts = 10
    self.attempts = 0
    self.guess = ""
    self.message = "欢迎参加数字猜测游戏!"
    self.game_over = False
    self.win = False
    
    # 颜色定义
    self.WHITE = (255, 255, 255)
    self.BLACK = (0, 0, 0)
    self.RED = (255, 0, 0)
    self.GREEN = (0, 255, 0)
    self.BLUE = (0, 0, 255)
    self.GRAY = (200, 200, 200)
    
def handle_events(self):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if not self.game_over:
                if event.key == pygame.K_RETURN:
                    if self.guess:
                        try:
                            guess_value = int(self.guess)
                            self.attempts += 1
                            
                            if guess_value < self.secret_number:
                                self.message = f"太小了!再大一点... ({self.attempts}/{self.max_attempts})"
                            elif guess_value > self.secret_number:
                                self.message = f"太大了!再小一点... ({self.attempts}/{self.max_attempts})"
                            else:
                                self.message = f"恭喜你!你在第{self.attempts}次尝试中猜对了!"
                                self.game_over = True
                                self.win = True
                            
                            if self.attempts >= self.max_attempts and not self.win:
                                self.message = f"很遗憾,你已经用完了所有尝试。正确答案是{self.secret_number}。"
                                self.game_over = True
                            
                            self.guess = ""
                        except ValueError:
                            self.message = "请输入有效的数字!"
                elif event.key == pygame.K_BACKSPACE:
                    self.guess = self.guess[:-1]
                else:
                    # 只允许输入数字
                    if event.unicode.isdigit() and len(self.guess) < 3:
                        self.guess += event.unicode
            else:
                # 游戏结束后按任意键重新开始
                if event.key == pygame.K_r:
                    self.reset_game()

def reset_game(self):
    self.secret_number = random.randint(1, 100)
    self.attempts = 0
    self.guess = ""
    self.message = "欢迎参加数字猜测游戏!"
    self.game_over = False
    self.win = False

def draw(self):
    self.screen.fill(self.WHITE)
    
    # 绘制标题
    title = self.font_large.render("数字猜测游戏", True, self.BLUE)
    title_rect = title.get_rect(center=(self.width//2, 50))
    self.screen.blit(title, title_rect)
    
    # 绘制游戏说明
    instructions = self.font_small.render("猜一个1到100之间的数字,你有10次机会", True, self.BLACK)
    instructions_rect = instructions.get_rect(center=(self.width//2, 110))
    self.screen.blit(instructions, instructions_rect)
    
    # 绘制当前猜测
    if not self.game_over:
        guess_text = self.font_medium.render(f"你当前的猜测: {self.guess}", True, self.BLACK)
        guess_rect = guess_text.get_rect(center=(self.width//2, 200))
        self.screen.blit(guess_text, guess_rect)
        
        # 绘制输入框
        pygame.draw.rect(self.screen, self.GRAY, (self.width//2 - 100, 250, 200, 50), 2)
        input_text = self.font_medium.render(self.guess, True, self.BLACK)
        input_rect = input_text.get_rect(center=(self.width//2, 275))
        self.screen.blit(input_text, input_rect)
        
        # 绘制提示
        hint = self.font_small.render("按回车键确认,按R键重新开始", True, self.BLACK)
        hint_rect = hint.get_rect(center=(self.width//2, 330))
        self.screen.blit(hint, hint_rect)
    else:
        # 游戏结束画面
        if self.win:
            result_text = self.font_large.render("恭喜你赢了!", True, self.GREEN)
        else:
            result_text = self.font_large.render("游戏结束", True, self.RED)
        
        result_rect = result_text.get_rect(center=(self.width//2, 200))
        self.screen.blit(result_text, result_rect)
        
        # 绘制重新开始提示
        restart_hint = self.font_medium.render("按R键重新开始游戏", True, self.BLACK)
        restart_rect = restart_hint.get_rect(center=(self.width//2, 300))
        self.screen.blit(restart_hint, restart_rect)
    
    # 绘制消息
    message_color = self.GREEN if (self.game_over and self.win) else self.RED if (self.game_over and not self.win) else self.BLACK
    message_text = self.font_medium.render(self.message, True, message_color)
    message_rect = message_text.get_rect(center=(self.width//2, 400))
    self.screen.blit(message_text, message_rect)
    
    # 绘制尝试次数
    attempts_text = self.font_small.render(f"尝试次数: {self.attempts}/{self.max_attempts}", True, self.BLACK)
    attempts_rect = attempts_text.get_rect(center=(self.width//2, 450))
    self.screen.blit(attempts_text, attempts_rect)
    
    # 更新显示
    pygame.display.flip()

def run(self):
    clock = pygame.time.Clock()
    while True:
        self.handle_events()
        self.draw()
        clock.tick(60)

if name == "main":
game = NumberGuessingGame()
game.run()

posted @ 2025-06-21 11:14  czicccc  阅读(7)  评论(0)    收藏  举报