吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1947|回复: 24
上一主题 下一主题
收起左侧

[Python 原创] Python编写小游戏——天降鸿福

[复制链接]
跳转到指定楼层
楼主
sunnychen 发表于 2024-4-9 11:16 回帖奖励
通过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 < 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.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) < 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, [10, 10])

    # 显示游戏结束信息
    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()

110734.jpg (35.74 KB, 下载次数: 6)

110734.jpg

免费评分

参与人数 6吾爱币 +9 热心值 +6 收起 理由
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
VanYoung + 1 谢谢@Thanks!
Explorer12138 + 1 + 1 用心讨论,共获提升!
lyj722 + 1 我很赞同!
mytomsummer + 1 谢谢@Thanks!
popsos + 1 + 1 谢谢@Thanks!

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

推荐
最新的 发表于 2024-4-9 16:22
后面部分有缩进错误
推荐
q28066 发表于 2024-4-9 21:32
[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 < 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.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) < 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, [10, 10])

    # 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()
沙发
fireshark 发表于 2024-4-9 13:41
3#
changyufeichang 发表于 2024-4-9 14:54
很好的游戏,创意十足
5#
Corgibro 发表于 2024-4-9 16:56
创意不错啊,期待
6#
xlaipojie 发表于 2024-4-9 17:35
学习了,很不错的创意
7#
yunyanyishi 发表于 2024-4-9 18:35
我属于 Python 刚入门,还在学习中,研究学习一下这段代码
8#
janlili 发表于 2024-4-9 19:30
支持楼主分享,共同学习
9#
risingsun 发表于 2024-4-9 19:38
调试下,试试。
10#
fast123 发表于 2024-4-9 19:40
拿走了,感谢.
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-9 09:35

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表