Python编写小游戏——天降鸿福
通过Python 编写小游戏,让学习变得有趣。通过练习逐步掌握 Python 编程的基本概念和技能,也能给学习者带来成就感和乐趣。在开发游戏时,主要用到Pygame库,用于处理游戏中的图形、音频、事件等。
天降鸿福是一种有趣的反应游戏,主要用到Python的游戏循环、随机数生成和碰撞测试。
玩家需要移动鼠标控制一个可左右移动的盘子,在屏幕上接住不同颜色的球来得分。游戏中有三种颜色的球,分别是红色、绿色和蓝色,每种颜色的球都有不同的分值。
但需要注意的是,红色的球是负分球,接到红色球会减少分数同时盘子长度减少,当盘子长度减少到0时,游戏结束。
玩家需要尽可能地接住绿色和蓝色球来得分,同时避免接到红色球。
import pygame
import random
pygame.init()
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Ball Catcher")
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
font = pygame.font.SysFont(None, 36)
class Ball(pygame.sprite.Sprite):
def __init__(self, color, score):
super().__init__()
self.radius = random.randint(10, 30)
self.color = color
self.score = score
self.speed = random.randint(1, 5)
self.image = pygame.Surface(, pygame.SRCALPHA)
pygame.draw.circle(self.image, self.color, (self.radius, self.radius), self.radius)
self.render_score()
self.rect = self.image.get_rect()
self.rect.x = random.randint(0, SCREEN_WIDTH - self.rect.width)
self.rect.y = 0
def render_score(self):
text_surface = font.render(str(self.score), True, WHITE)
text_rect = text_surface.get_rect(center=(self.radius, self.radius))
self.image.blit(text_surface, text_rect)
def update(self):
self.rect.y += self.speed
class Paddle(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.length = 5# 盘子的长度
self.width = self.length * 20# 盘子的宽度
self.image = pygame.Surface()
self.image.fill(WHITE)
self.rect = self.image.get_rect()
self.rect.x = (SCREEN_WIDTH - self.width) // 2
self.rect.y = SCREEN_HEIGHT - 20
def update(self):
pos = pygame.mouse.get_pos()
self.rect.x = pos - self.width // 2
if self.rect.x < 0:
self.rect.x = 0
if self.rect.x > SCREEN_WIDTH - self.width:
self.rect.x = SCREEN_WIDTH - self.width
def decrease_length(self):
if self.length > 0:
self.length -= 1
self.width = self.length * 20
self.image = pygame.Surface()
self.image.fill(WHITE)
self.rect = self.image.get_rect(center=self.rect.center)
if self.length == 0:
return True
return False
all_sprites = pygame.sprite.Group()
balls = pygame.sprite.Group()
paddle = Paddle()
all_sprites.add(paddle)
# 主循环
running = True
score = 0
clock = pygame.time.Clock()
game_over = False
while running:
# 事件处理
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
all_sprites.update()
if not game_over and random.randint(1, 100) < 5:
ball_color, ball_score = random.choice([(RED, -1), (GREEN, 1), (BLUE, 2)])
ball = Ball(ball_color, ball_score)
all_sprites.add(ball)
balls.add(ball)
hits = pygame.sprite.spritecollide(paddle, balls, True)
for ball in hits:
score += ball.score
if ball.color == RED:
if paddle.decrease_length():
game_over = True
screen.fill((0, 0, 0))
all_sprites.draw(screen)
text = font.render("Score: " + str(score), True, WHITE)
screen.blit(text, )
# 显示游戏结束信息
if game_over:
game_over_text = font.render("Game Over", True, RED)
screen.blit(game_over_text, [(SCREEN_WIDTH - game_over_text.get_width()) // 2, SCREEN_HEIGHT // 2])
pygame.display.flip()
clock.tick(30)
# 退出Pygame
pygame.quit()
后面部分有缩进错误 import pygame
import random
# Initialize pygame
pygame.init()
# Constants
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
# Set up the screen
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Ball Catcher")
# Fonts
font = pygame.font.SysFont(None, 36)
class Ball(pygame.sprite.Sprite):
def __init__(self, color, score):
super().__init__()
self.radius = random.randint(10, 30)
self.color = color
self.score = score
self.speed = random.randint(1, 5)
self.image = pygame.Surface(, pygame.SRCALPHA)
pygame.draw.circle(self.image, self.color, (self.radius, self.radius), self.radius)
self.render_score()
self.rect = self.image.get_rect()
self.rect.x = random.randint(0, SCREEN_WIDTH - self.rect.width)
self.rect.y = 0
def render_score(self):
text_surface = font.render(str(self.score), True, WHITE)
text_rect = text_surface.get_rect(center=(self.radius, self.radius))
self.image.blit(text_surface, text_rect)
def update(self):
self.rect.y += self.speed
class Paddle(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.length = 5
self.width = self.length * 20
self.image = pygame.Surface()
self.image.fill(WHITE)
self.rect = self.image.get_rect()
self.rect.x = (SCREEN_WIDTH - self.width) // 2
self.rect.y = SCREEN_HEIGHT - 20
def update(self):
pos = pygame.mouse.get_pos()
self.rect.x = pos - self.width // 2
if self.rect.x < 0:
self.rect.x = 0
if self.rect.x > SCREEN_WIDTH - self.width:
self.rect.x = SCREEN_WIDTH - self.width
def decrease_length(self):
if self.length > 0:
self.length -= 1
self.width = self.length * 20
self.image = pygame.Surface()
self.image.fill(WHITE)
self.rect = self.image.get_rect(center=self.rect.center)
if self.length == 0:
return True
return False
# Initialize sprites
all_sprites = pygame.sprite.Group()
balls = pygame.sprite.Group()
paddle = Paddle()
all_sprites.add(paddle)
# Game variables
score = 0
game_over = False
clock = pygame.time.Clock()
def handle_events():
for event in pygame.event.get():
if event.type == pygame.QUIT:
return False
return True
# Main game loop
while True:
if not handle_events():
break
all_sprites.update()
# Spawn new balls
if not game_over and random.randint(1, 100) < 5:
ball_color, ball_score = random.choice([(RED, -1), (GREEN, 1), (BLUE, 2)])
ball = Ball(ball_color, ball_score)
all_sprites.add(ball)
balls.add(ball)
# Check for collisions
hits = pygame.sprite.spritecollide(paddle, balls, True)
for ball in hits:
score += ball.score
if ball.color == RED:
if paddle.decrease_length():
game_over = True
# Draw everything
screen.fill((0, 0, 0))
all_sprites.draw(screen)
text = font.render("Score: " + str(score), True, WHITE)
screen.blit(text, )
# Display game over message
if game_over:
game_over_text = font.render("Game Over", True, RED)
screen.blit(game_over_text, [(SCREEN_WIDTH - game_over_text.get_width()) // 2, SCREEN_HEIGHT // 2])
pygame.display.flip()
clock.tick(30)
# Quit pygame
pygame.quit()
很有创意,功能还可以优化
很好的游戏,创意十足 创意不错啊,期待 学习了,很不错的创意 {:1_921:}我属于 Python 刚入门,还在学习中,研究学习一下这段代码 支持楼主分享,共同学习 调试下,试试。 拿走了,感谢.