当前位置: 首页 > news >正文

pygame开发课程系列(7):打砖块,飞行射击,跳跃游戏实例开发

第七章 实践项目

在本章中,我们将通过三个实践项目来巩固和应用前面章节中学到的知识。这些项目涵盖了经典打砖块游戏、飞行射击游戏和简单的平台游戏,它们将帮助你更好地理解 Pygame 的使用,并为你开发自己的游戏提供灵感和实践经验。

7.1 项目 1: 经典打砖块在这里插入图片描述

目标: 设计一个简单的打砖块游戏,玩家控制一个挡板,反弹一个球,打破砖块以获得分数。

7.1.1 设计基本逻辑
  1. 初始化 Pygame

    import pygame
    import syspygame.init()
    
  2. 设置游戏窗口

    screen = pygame.display.set_mode((800, 600))
    pygame.display.set_caption("经典打砖块")
    
  3. 定义颜色

    BLACK = (0, 0, 0)
    WHITE = (255, 255, 255)
    RED = (255, 0, 0)
    
  4. 创建挡板、球和砖块

    # 挡板
    paddle = pygame.Rect(350, 550, 100, 20)
    paddle_speed = 5# 球
    ball = pygame.Rect(390, 530, 20, 20)
    ball_speed_x = 3
    ball_speed_y = -3# 砖块
    bricks = [pygame.Rect(60 + i * 60, 50 + j * 30, 50, 20) for i in range(10) for j in range(5)]
    
  5. 游戏循环

    clock = pygame.time.Clock()while True:for event in pygame.event.get():if event.type == pygame.QUIT:pygame.quit()sys.exit()keys = pygame.key.get_pressed()if keys[pygame.K_LEFT]:paddle.x -= paddle_speedif keys[pygame.K_RIGHT]:paddle.x += paddle_speed# 更新球的位置ball.x += ball_speed_xball.y += ball_speed_y# 碰撞检测if ball.left <= 0 or ball.right >= 800:ball_speed_x = -ball_speed_xif ball.top <= 0:ball_speed_y = -ball_speed_yif ball.colliderect(paddle):ball_speed_y = -ball_speed_y# 砖块碰撞for brick in bricks[:]:if ball.colliderect(brick):ball_speed_y = -ball_speed_ybricks.remove(brick)# 绘制screen.fill(BLACK)pygame.draw.rect(screen, WHITE, paddle)pygame.draw.ellipse(screen, RED, ball)for brick in bricks:pygame.draw.rect(screen, WHITE, brick)pygame.display.flip()clock.tick(60)
    

完整代码如下:

import pygame
import syspygame.init()screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("经典打砖块")BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)# 挡板
paddle = pygame.Rect(350, 550, 100, 20)
paddle_speed = 5# 球
ball = pygame.Rect(390, 530, 20, 20)
ball_speed_x = 3
ball_speed_y = -3# 砖块
bricks = [pygame.Rect(60 + i * 60, 50 + j * 30, 50, 20) for i in range(10) for j in range(5)]clock = pygame.time.Clock()while True:for event in pygame.event.get():if event.type == pygame.QUIT:pygame.quit()sys.exit()keys = pygame.key.get_pressed()if keys[pygame.K_LEFT]:paddle.x -= paddle_speedif keys[pygame.K_RIGHT]:paddle.x += paddle_speed# 更新球的位置ball.x += ball_speed_xball.y += ball_speed_y# 碰撞检测if ball.left <= 0 or ball.right >= 800:ball_speed_x = -ball_speed_xif ball.top <= 0:ball_speed_y = -ball_speed_yif ball.colliderect(paddle):ball_speed_y = -ball_speed_y# 砖块碰撞for brick in bricks[:]:if ball.colliderect(brick):ball_speed_y = -ball_speed_ybricks.remove(brick)# 绘制screen.fill(BLACK)pygame.draw.rect(screen, WHITE, paddle)pygame.draw.ellipse(screen, RED, ball)for brick in bricks:pygame.draw.rect(screen, WHITE, brick)pygame.display.flip()clock.tick(60)

最终运行效果如图:
在这里插入图片描述

7.2 项目 2: 飞行射击游戏

目标: 创建一个简单的飞行射击游戏,玩家控制飞行器,射击敌人,避免被敌人击中。

7.2.1 设计基本逻辑
  1. 初始化 Pygame设置窗口

    import pygame
    import syspygame.init()
    screen = pygame.display.set_mode((800, 600))
    pygame.display.set_caption("飞行射击游戏")
    
  2. 定义颜色和资源

    WHITE = (255, 255, 255)
    BLACK = (0, 0, 0)# 飞行器
    player = pygame.Rect(375, 500, 50, 30)
    player_speed = 5# 子弹
    bullets = []# 敌人
    enemies = [pygame.Rect(i * 60, j * 50, 40, 30) for i in range(10) for j in range(3)]
    
  3. 游戏循环

    clock = pygame.time.Clock()while True:for event in pygame.event.get():if event.type == pygame.QUIT:pygame.quit()sys.exit()keys = pygame.key.get_pressed()if keys[pygame.K_LEFT]:player.x -= player_speedif keys[pygame.K_RIGHT]:player.x += player_speedif keys[pygame.K_SPACE]:bullets.append(pygame.Rect(player.centerx - 5, player.top - 10, 10, 20))# 更新子弹位置for bullet in bullets[:]:bullet.y -= 10if bullet.bottom < 0:bullets.remove(bullet)# 碰撞检测for bullet in bullets[:]:for enemy in enemies[:]:if bullet.colliderect(enemy):bullets.remove(bullet)enemies.remove(enemy)# 绘制screen.fill(BLACK)pygame.draw.rect(screen, WHITE, player)for bullet in bullets:pygame.draw.rect(screen, WHITE, bullet)for enemy in enemies:pygame.draw.rect(screen, WHITE, enemy)pygame.display.flip()clock.tick(60)
    

整体代码:

import pygame
import syspygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("飞行射击游戏")WHITE = (255, 255, 255)
BLACK = (0, 0, 0)# 飞行器
player = pygame.Rect(375, 500, 50, 30)
player_speed = 5# 子弹
bullets = []# 敌人
enemies = [pygame.Rect(i * 60, j * 50, 40, 30) for i in range(10) for j in range(3)]clock = pygame.time.Clock()while True:for event in pygame.event.get():if event.type == pygame.QUIT:pygame.quit()sys.exit()keys = pygame.key.get_pressed()if keys[pygame.K_LEFT]:player.x -= player_speedif keys[pygame.K_RIGHT]:player.x += player_speedif keys[pygame.K_SPACE]:bullets.append(pygame.Rect(player.centerx - 5, player.top - 10, 10, 20))# 更新子弹位置for bullet in bullets[:]:bullet.y -= 10if bullet.bottom < 0:bullets.remove(bullet)# 碰撞检测for bullet in bullets[:]:for enemy in enemies[:]:if bullet.colliderect(enemy):bullets.remove(bullet)enemies.remove(enemy)# 绘制screen.fill(BLACK)pygame.draw.rect(screen, WHITE, player)for bullet in bullets:pygame.draw.rect(screen, WHITE, bullet)for enemy in enemies:pygame.draw.rect(screen, WHITE, enemy)pygame.display.flip()clock.tick(60)

运行效果如下:
在这里插入图片描述

7.3 项目 3: 简单的爬梯跳跃游戏

目标: 设计一个基本的小游戏玩家可以在梯子上移动和跳跃。

7.3.1 设计基本逻辑
  1. 初始化 Pygame设置窗口

    import pygame
    import syspygame.init()
    screen = pygame.display.set_mode((800, 600))
    pygame.display.set_caption("简单的平台游戏")
    
  2. 定义颜色和资源

    WHITE = (255, 255, 255)
    GREEN = (0, 255, 0)
    BLUE = (0, 0, 255)# 玩家
    player = pygame.Rect(50, 500, 40, 60)
    player_speed = 5
    player_jump = False
    player_y_velocity = 0# 平台
    platforms = [pygame.Rect(0, 550, 800, 50), pygame.Rect(200, 400, 200, 20)]
    gravity = 0.5
    
  3. 游戏循环

    clock = pygame.time.Clock()while True:for event in pygame.event.get():if event.type == pygame.QUIT:pygame.quit()sys.exit()keys = pygame.key.get_pressed()if keys[pygame.K_LEFT]:player.x -= player_speedif keys[pygame.K_RIGHT]:player.x += player_speedif keys[pygame.K_SPACE] and not player_jump:player_y_velocity = -10player_jump = True# 应用重力player_y_velocity += gravityplayer.y += player_y_velocity# 平台碰撞for platform in platforms:if player.colliderect(platform):if player_y_velocity > 0:player.bottom = platform.topplayer_y_velocity = 0player_jump = False# 边界检测if player.bottom > 600:player.bottom = 600player_y_velocity = 0player_jump = False# 绘制screen.fill(BLUE)pygame.draw.rect(screen, WHITE, player)for platform in platforms:pygame.draw.rect(screen, GREEN, platform)pygame.display.flip()clock.tick(60)
    

运行效果如下:
在这里插入图片描述

小结

在本章中,我们通过三个实践项目展示了如何使用 Pygame 开发简单的游戏。这些项目不仅让你了解了游戏开发的基本流程,还帮助你掌握了 Pygame 的核心功能。你可以使用这些项目作为起点,继续扩展和改进你的游戏,或者尝试创建更多不同类型的游戏。通过不断实践和优化,你将能够创建更复杂、更有趣的游戏项目。


http://www.mrgr.cn/news/1879.html

相关文章:

  • Linux安装MQTT 服务器(图文教程)
  • AI作画提示词(Prompts)工程:技巧与最佳实践
  • uniapp中节点信息的使用
  • 华三(H3C)UIS3030 Uni-R4900服务器硬件监控指标解读
  • 工业匠芯 | 匠芯创科技工业级芯片D21X、D13X、D12X、G73X选型应用
  • Conditional Flow Matching: Simulation-Free Dynamic Optimal Transport论文阅读笔记
  • 照片整理专家,照片整理大师,照片图库整理,智能图片整理软件
  • GEC6818开发板的学习
  • 【微信小程序】自定义组件 - 组件的生命周期
  • Python实现GAN(生成对抗网络)图像修复算法
  • re模块入门教程
  • 花几千上万学习Java,真没必要!(四十六)
  • 【Linux操作系统】基础IO
  • 大数据-86 Spark 集群 WordCount 用 Scala Java 调用Spark 编译并打包上传运行 梦开始的地方
  • SQL 时间盲注 (injection 第十五关)
  • leetcode 堆栈(栈+优先队列)——java实现
  • 牛客网SQL进阶129 :月均完成试卷数不小于3的用户
  • spring(1)
  • Hadoop 中的大数据技术:调优篇(2)
  • 0815,析构函数,拷贝构造函数,赋值运算符函数