用pygame写一个很小众的游戏
以下是一个小众的pygame游戏示例:
游戏名称:躲避弹球
游戏规则:玩家需要控制一个小球躲避弹球,直到获得一定数量的分数或者被弹球命中。
游戏操作:玩家可以使用键盘上的左右箭头键来控制小球的移动。
游戏界面:游戏界面包括一个黑色背景和一个白色小球,弹球从上方不断下落,玩家需要躲避弹球并尽可能多地接触到白色小球,以获得分数。
游戏实现:
import pygame
import random
# 初始化pygame
pygame.init()
# 设置游戏窗口大小
screen_width = 500
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
# 设置游戏标题
pygame.display.set_caption("躲避弹球")
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
# 定义字体
font = pygame.font.Font(None, 36)
# 初始化游戏数据
score = 0
game_over = False
# 定义球的大小和速度
ball_radius = 10
ball_speed = 5
# 定义弹球的初始位置和速度
ball_x = random.randint(ball_radius, screen_width - ball_radius)
ball_y = 0
ball_dx = random.randint(-ball_speed, ball_speed)
ball_dy = ball_speed
# 定义小球的初始位置和速度
player_x = screen_width // 2
player_y = screen_height - 50
player_dx = ball_speed
# 游戏循环
while not game_over:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
# 更新游戏数据
ball_x += ball_dx
ball_y += ball_dy
if ball_x < ball_radius or ball_x > screen_width - ball_radius:
ball_dx = -ball_dx
if ball_y < ball_radius:
ball_dy = ball_speed
elif ball_y > player_y - ball_radius and ball_x > player_x - ball_radius and ball_x < player_x + ball_radius:
ball_dy = -ball_dy
score += 1
elif ball_y > screen_height - ball_radius:
game_over = True
player_x += player_dx
if player_x < ball_radius or player_x > screen_width - ball_radius:
player_dx = -player_dx
# 绘制游戏界面
screen.fill(BLACK)
pygame.draw.circle(screen, WHITE, (player_x, player_y), ball_radius)
pygame.draw.circle(screen, RED, (ball_x, ball_y), ball_radius)
score_text = font.render("Score: {}".format(score), True, WHITE)
screen.blit(score_text, (10, 10))
# 更新屏幕
pygame.display.update()
# 显示游戏结束信息
game_over_text = font.render("Game Over", True, WHITE)
screen.blit(game_over_text, (screen_width // 2 - 80, screen_height // 2 - 20))
pygame.display.update()
# 等待用户关闭游戏窗口
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
原文地址: https://www.cveoy.top/t/topic/bcIO 著作权归作者所有。请勿转载和采集!