帮我用python做一个150行代码的弹珠小游戏
时间: 2024-01-28 14:03:21 浏览: 158
好的,下面是一个简单的弹珠小游戏的代码示例,共计约150行代码:
```python
import pygame
import random
# 初始化pygame
pygame.init()
# 设置窗口大小
WINDOW_WIDTH = 600
WINDOW_HEIGHT = 800
window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("弹珠小游戏")
# 设置颜色变量
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
# 设置字体
font = pygame.font.SysFont(None, 45)
# 设置游戏变量
score = 0
lives = 3
game_over = False
# 创建弹珠类
class Ball(pygame.sprite.Sprite):
def __init__(self, color, width, height):
super().__init__()
self.image = pygame.Surface([width, height])
self.image.fill(color)
self.rect = self.image.get_rect()
self.velocity = [random.randint(4,8), random.randint(-8,8)]
def update(self):
self.rect.x += self.velocity[0]
self.rect.y += self.velocity[1]
if self.rect.right >= WINDOW_WIDTH or self.rect.left <= 0:
self.velocity[0] = -self.velocity[0]
if self.rect.top <= 0:
self.velocity[1] = -self.velocity[1]
# 创建板子类
class Paddle(pygame.sprite.Sprite):
def __init__(self, color, width, height):
super().__init__()
self.image = pygame.Surface([width, height])
self.image.fill(color)
self.rect = self.image.get_rect()
self.rect.x = WINDOW_WIDTH // 2
self.rect.y = WINDOW_HEIGHT - height
def update(self):
pos = pygame.mouse.get_pos()
self.rect.x = pos[0]
if self.rect.left <= 0:
self.rect.left = 0
elif self.rect.right >= WINDOW_WIDTH:
self.rect.right = WINDOW_WIDTH
# 创建砖块类
class Brick(pygame.sprite.Sprite):
def __init__(self, color, width, height):
super().__init__()
self.image = pygame.Surface([width, height])
self.image.fill(color)
self.rect = self.image.get_rect()
# 创建弹珠、板子、砖块组
all_sprites = pygame.sprite.Group()
balls = pygame.sprite.Group()
paddles = pygame.sprite.Group()
bricks = pygame.sprite.Group()
for i in range(3):
ball = Ball(RED, 10, 10)
ball.rect.x = random.randint(0, WINDOW_WIDTH)
ball.rect.y = random.randint(0, WINDOW_HEIGHT // 2)
balls.add(ball)
all_sprites.add(ball)
paddle = Paddle(BLUE, 100, 10)
paddles.add(paddle)
all_sprites.add(paddle)
for i in range(10):
brick = Brick(BLUE, 60, 15)
brick.rect.x = i * 60 + 20
brick.rect.y = 60
bricks.add(brick)
all_sprites.add(brick)
# 创建游戏循环
clock = pygame.time.Clock()
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
# 更新所有精灵
all_sprites.update()
# 检测弹珠与板子的碰撞
if pygame.sprite.spritecollide(paddle, balls, False):
for ball in balls:
ball.velocity[1] = -ball.velocity[1]
# 检测弹珠与砖块的碰撞
for ball in balls:
bricks_hit = pygame.sprite.spritecollide(ball, bricks, True)
if bricks_hit:
ball.velocity[1] = -ball.velocity[1]
score += len(bricks_hit)
# 检测弹珠是否落地
for ball in balls:
if ball.rect.bottom > WINDOW_HEIGHT:
lives -= 1
balls.remove(ball)
all_sprites.remove(ball)
if lives == 0:
game_over = True
else:
ball = Ball(RED, 10, 10)
ball.rect.x = random.randint(0, WINDOW_WIDTH)
ball.rect.y = random.randint(0, WINDOW_HEIGHT // 2)
balls.add(ball)
all_sprites.add(ball)
# 绘制所有精灵
window.fill(WHITE)
all_sprites.draw(window)
# 绘制得分和生命值
score_text = font.render("Score: {}".format(score), True, BLACK)
window.blit(score_text, [10, 10])
lives_text = font.render("Lives: {}".format(lives), True, BLACK)
window.blit(lives_text, [WINDOW_WIDTH - 120, 10])
# 更新屏幕
pygame.display.update()
# 设置帧率
clock.tick(60)
# 游戏结束,显示最终得分
final_score_text = font.render("Final Score: {}".format(score), True, BLACK)
window.blit(final_score_text, [WINDOW_WIDTH // 2 - 100, WINDOW_HEIGHT // 2])
pygame.display.update()
# 停留3秒后退出游戏
pygame.time.wait(3000)
pygame.quit()
```
这个小游戏中,你需要控制板子移动,让弹珠不断反弹并消除所有砖块,同时要注意不让弹珠掉落到底部。游戏中包含了弹珠、板子、砖块三种元素,它们都是继承自pygame.sprite.Sprite类的精灵对象。游戏中使用了pygame.sprite.Group类来管理所有精灵对象,方便统一更新和绘制。
游戏的主要逻辑在游戏循环中,每次循环都要更新所有精灵的位置,检测碰撞和得分,并绘制得分和生命值。在游戏结束后,程序会停留3秒钟并退出游戏。
阅读全文