python球球大作战

使用pycharm简单编写一个2D球类游戏,包含一个玩家控制的球和多个自动移动的球。玩家可以通过键盘控制自己的球来“吃掉”其他球,增加得分

示例代码:

# !/usr/bin/env python
# -*- coding:utf-8 -*-

"""
@file: ball_game.py
@author: czx
@time: 2024/4/22 15:59
"""
# 导入库
import pygame #创建游戏
import random #随机数生成

# 定义颜色
RED = (255, 0, 0)

# 初始化pygame
pygame.init()

# 定义屏幕宽度和高度
WIDTH, HEIGHT = 800, 600

# 设置屏幕和标题
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("球球大作战")

# 设置时钟,控制游戏的帧率
clock = pygame.time.Clock()

# 定义球类
class Ball:
    def __init__(self, x, y, radius, color):
        self.x = x
        self.y = y
        self.radius = radius
        self.color = color
        self.speed_x = random.randint(-5, 5)
        self.speed_y = random.randint(-5, 5)

    def move(self):
        self.x += self.speed_x
        self.y += self.speed_y
        # 边界检查,确保球不会移出屏幕
        if self.x < self.radius or self.x > WIDTH - self.radius:
            self.speed_x = -self.speed_x
        if self.y < self.radius or self.y > HEIGHT - self.radius:
            self.speed_y = -self.speed_y

    def draw(self, screen):
        pygame.draw.circle(screen, self.color, (self.x, self.y), self.radius)

# 初始化玩家球
player = Ball(WIDTH // 2, HEIGHT // 2, 15, RED)

# 初始化地图大小和球的数量
MAP_WIDTH, MAP_HEIGHT = WIDTH, HEIGHT
ball_NUM = 10

# 创建球列表
balls = []
for _ in range(ball_NUM):
    # 随机生成球的半径,确保玩家球的半径大于其他球
    radius = random.randint(10, min(30, player.radius - 1)) if _ == 0 else random.randint(1, 30)
    # 随机生成球的x和y坐标,确保球完全在屏幕上
    x = random.randint(radius, MAP_WIDTH - radius)
    y = random.randint(radius, MAP_HEIGHT - radius)
    # 创建一个新球并添加到balls列表中
    balls.append(Ball(x, y, radius, (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))))

# 定义移动玩家球的函数
def player_move(keys, player):
    if keys[pygame.K_LEFT]:
        player.x -= 5
    if keys[pygame.K_RIGHT]:
        player.x += 5
    if keys[pygame.K_UP]:
        player.y -= 5
    if keys[pygame.K_DOWN]:
        player.y += 5
    # 边界检查,确保玩家球不会移出屏幕
    if player.x < player.radius:
        player.x = player.radius
    elif player.x > MAP_WIDTH - player.radius:
        player.x = MAP_WIDTH - player.radius
    if player.y < player.radius:
        player.y = player.radius
    elif player.y > MAP_HEIGHT - player.radius:
        player.y = MAP_HEIGHT - player.radius

# 定义吃球的函数
def eat_ball(player, balls):
    global score
    balls_to_remove = [ball for ball in balls if ball.x < 0 or ball.x > MAP_WIDTH or ball.y < 0 or ball.y > MAP_HEIGHT]
    balls_to_remove.extend([ball for ball in balls if player.x - player.radius < ball.x < player.x + player.radius * 2 and player.y - player.radius < ball.y < player.y + player.radius * 2])
    for ball in balls_to_remove:
        score += 1
        balls.remove(ball)

# 定义显示函数
def show(screen, player, balls):
    screen.fill((0, 0, 0))  # 填充背景为黑色
    player.draw(screen)  # 绘制玩家球
    for ball in balls:
        ball.draw(screen)  # 绘制其他球
    pygame.display.flip()  # 更新显示屏幕

# 主循环
running = True
score = 0
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    keys = pygame.key.get_pressed()
    player_move(keys, player)
    for ball in balls:
        ball.move()
    eat_ball(player, balls)
    show(screen, player, balls)  # 确保在所有绘制操作后调用show函数
    clock.tick(60)  # 设置帧率为60

# 退出pygame
pygame.quit()

如果还没有安装pygame的话,可以通过以下命令来安装:

pip install pygame -i https://pypi.tuna.tsinghua.edu.cn/simple

代码正常运行后,会创建一个简单的球类游戏,玩家可以通过键盘的箭头键来控制玩家球,吃掉其他小球以增加得分。

python球球大作战_第1张图片

你可能感兴趣的:(python,python,pygame,开发语言,游戏)