当前位置:首页 / 文章测试 / 测试游戏

测试游戏

开始打字练习

import pygame

import random

import time

# 初始化pygame

pygame.init()

# 设置屏幕宽度和高度

screen_width = 800

screen_height = 600

screen = pygame.display.set_mode((screen_width, screen_height))

pygame.display.set_caption("Hand Speed Training Game with Time Limit")

# 颜色定义

WHITE = (255, 255, 255)

RED = (255, 0, 0)

# 目标类

class Target:

def __init__(self):

self.x = random.randint(50, screen_width - 50)

self.y = random.randint(50, screen_height - 50)

self.radius = 20

self.is_alive = True

def draw(self):

pygame.draw.circle(screen, RED, (self.x, self.y), self.radius)

# 游戏主循环

def main():

targets = []

score = 0

total_time = 30 # 规定总时间(秒)

start_time = time.time()

running = True

while running:

current_time = time.time()

if current_time - start_time > total_time:

running = False

for event in pygame.event.get():

if event.type == pygame.QUIT:

running = False

elif event.type == pygame.MOUSEBUTTONDOWN:

mouse_pos = pygame.mouse.get_pos()

for target in targets:

distance = ((mouse_pos[0] - target.x) ** 2 + (mouse_pos[1] - target.y) ** 2) ** 0.5

if distance < target.radius:

target.is_alive = False

score += 1

screen.fill(WHITE)

if len(targets) < 5:

targets.append(Target())

for target in targets:

if target.is_alive:

target.draw()

else:

targets.remove(target)

font = pygame.font.SysFont(None, 36)

score_text = font.render("Score: " + str(score), True, RED)

screen.blit(score_text, (10, 10))

elapsed_time = current_time - start_time

time_text = font.render("Time left: {:.2f}s".format(total_time - elapsed_time), True, RED)

screen.blit(time_text, (10, 50))

pygame.display.flip()

pygame.quit()

print("游戏结束!你的得分是:", score)

if __name__ == "__main__":

main()

声明:以上文章均为用户自行发布,仅供打字交流使用,不代表本站观点,本站不承担任何法律责任,特此声明!如果有侵犯到您的权利,请及时联系我们删除。