Python编写小游戏——天降鸿福

查看 124|回复 10
作者:sunnychen   
通过Python 编写小游戏,让学习变得有趣。通过练习逐步掌握 Python 编程的基本概念和技能,也能给学习者带来成就感和乐趣。
在开发游戏时,主要用到Pygame库,用于处理游戏中的图形、音频、事件等。
天降鸿福是一种有趣的反应游戏,主要用到Python的游戏循环、随机数生成和碰撞测试。
玩家需要移动鼠标控制一个可左右移动的盘子,在屏幕上接住不同颜色的球来得分。游戏中有三种颜色的球,分别是红色、绿色和蓝色,每种颜色的球都有不同的分值。
但需要注意的是,红色的球是负分球,接到红色球会减少分数同时盘子长度减少,当盘子长度减少到0时,游戏结束。
玩家需要尽可能地接住绿色和蓝色球来得分,同时避免接到红色球。
[Python] 纯文本查看 复制代码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([self.radius * 2, self.radius * 2], 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.width, 20])
        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[0] - self.width // 2
        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.width, 20])
            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)

盘子, 鸿福

最新的   

后面部分有缩进错误
q28066   

[Python] 纯文本查看 复制代码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([self.radius * 2, self.radius * 2], 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.width, 20])
        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[0] - self.width // 2
        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.width, 20])
            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)
fireshark   

很有创意,功能还可以优化
changyufeichang   

很好的游戏,创意十足
Corgibro   

创意不错啊,期待
xlaipojie   

学习了,很不错的创意
yunyanyishi   

我属于 Python 刚入门,还在学习中,研究学习一下这段代码
janlili   

支持楼主分享,共同学习
risingsun   

调试下,试试。
您需要登录后才可以回帖 登录 | 立即注册

返回顶部