Python 练习003 ——小人接球游戏

2023-12-16 05:59

本文主要是介绍Python 练习003 ——小人接球游戏,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目:

《Python编程:从入门到实践》 P250:
创建一个游戏,在屏幕上放置一个代表人的矩形,玩家可以用方向键来控制它,让一个球出现在屏幕顶端,且水平位置是随机的,让球以固定的速度下落,如果代表人的矩形与球发生碰撞(代表人将球抓住了),就让球消失,每当人抓住球或者球抵达了屏幕底端,都创建一个新球。


代码:

# -*- coding: utf-8 -*-import sys
import pygamefrom pygame.sprite import Group
from random import randintclass Person(pygame.sprite.Sprite):def __init__(self, screen):super().__init__()self.screen = screenself.person_width = 30self.person_height = 5# 在(0,0)处创建一个表示人的矩形,再设置正确的位置self.rect = pygame.Rect(0, 0, self.person_width, self.person_height)self.screen_rect = self.screen.get_rect()self.rect.centerx = self.screen_rect.centerxself.rect.bottom = self.screen_rect.bottom# 存储用小数表示的人的位置self.centerx = float(self.rect.centerx)self.centery = float(self.rect.centery)# 移动标志self.moving_right = Falseself.moving_left = Falseself.moving_up = Falseself.moving_down = Falseself.color = (60, 60 ,60)self.speed_factor = 1def update(self):# 更新人的位置if self.moving_right and self.rect.right < self.screen_rect.right:self.centerx += self.speed_factorif self.moving_left and self.rect.left > 0:self.centerx -= self.speed_factorif self.moving_up and self.rect.top > 0:self.centery -= self.speed_factorif self.moving_down and self.rect.bottom < self.screen_rect.bottom:self.centery += self.speed_factorself.rect.centerx = self.centerxself.rect.centery = self.centerydef draw_person(self):pygame.draw.rect(self.screen, self.color, self.rect)class Ball(pygame.sprite.Sprite):def __init__(self,screen):super().__init__()self.image = pygame.image.load('images/ball.bmp')self.rect = self.image.get_rect()self.speed = 0.5self.screen = screenself.screen_rect = self.screen.get_rect()self.y = float(self.rect.y)def check_edge(self):# 检查球是否落到屏幕底部if self.rect.top >= self.screen_rect.bottom:return Truedef update(self):self.y += self.speedself.rect.y = self.y
#       self.rect.y += self.speeddef create_ball(balls, screen):ball = Ball(screen)max_rect_x = ball.screen_rect.right - ball.rect.width + 1ball.rect.x = randint(0, max_rect_x)ball.rect.y = 0balls.add(ball)def check_person_ball_collisions(screen, person, balls):if pygame.sprite.spritecollideany(person, balls):for ball in balls:balls.remove(ball)create_ball(balls, screen)def check_keydown_events(event, person):if event.key == pygame.K_RIGHT:person.moving_right = Trueelif event.key == pygame.K_LEFT:person.moving_left = Trueelif event.key == pygame.K_UP:person.moving_up = Trueelif event.key == pygame.K_DOWN:person.moving_down = Trueelif event.key == pygame.K_q:sys.exit()def check_keyup_events(event, person):if event.key == pygame.K_RIGHT:person.moving_right = Falseelif event.key == pygame.K_LEFT:person.moving_left = Falseelif event.key == pygame.K_UP:person.moving_up = Falseelif event.key == pygame.K_DOWN:person.moving_down = Falsedef check_events(screen, person,):for event in pygame.event.get():if event.type == pygame.QUIT:sys.exit()# 移动飞船elif event.type == pygame.KEYDOWN:check_keydown_events(event, person)elif event.type == pygame.KEYUP:check_keyup_events(event, person)def run_game():pygame.init()# 设置屏幕参数screen_width = 1000screen_height = 600screen = pygame.display.set_mode((screen_width, screen_height))bg_color = (230, 230, 230)balls = Group()person = Person(screen)create_ball(balls, screen)while True:screen.fill(bg_color)check_events(screen, person)check_person_ball_collisions(screen, person, balls)for ball in balls:if ball.check_edge():create_ball(balls, screen)breakfor ball in balls:if ball.check_edge():balls.remove(ball)person.update()person.draw_person()balls.update()balls.draw(screen)pygame.display.flip()run_game

运行结果:

在这里插入图片描述

这篇关于Python 练习003 ——小人接球游戏的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/499379

相关文章

Python版本信息获取方法详解与实战

《Python版本信息获取方法详解与实战》在Python开发中,获取Python版本号是调试、兼容性检查和版本控制的重要基础操作,本文详细介绍了如何使用sys和platform模块获取Python的主... 目录1. python版本号获取基础2. 使用sys模块获取版本信息2.1 sys模块概述2.1.1

一文详解Python如何开发游戏

《一文详解Python如何开发游戏》Python是一种非常流行的编程语言,也可以用来开发游戏模组,:本文主要介绍Python如何开发游戏的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录一、python简介二、Python 开发 2D 游戏的优劣势优势缺点三、Python 开发 3D

Python函数作用域与闭包举例深度解析

《Python函数作用域与闭包举例深度解析》Python函数的作用域规则和闭包是编程中的关键概念,它们决定了变量的访问和生命周期,:本文主要介绍Python函数作用域与闭包的相关资料,文中通过代码... 目录1. 基础作用域访问示例1:访问全局变量示例2:访问外层函数变量2. 闭包基础示例3:简单闭包示例4

Python实现字典转字符串的五种方法

《Python实现字典转字符串的五种方法》本文介绍了在Python中如何将字典数据结构转换为字符串格式的多种方法,首先可以通过内置的str()函数进行简单转换;其次利用ison.dumps()函数能够... 目录1、使用json模块的dumps方法:2、使用str方法:3、使用循环和字符串拼接:4、使用字符

Python版本与package版本兼容性检查方法总结

《Python版本与package版本兼容性检查方法总结》:本文主要介绍Python版本与package版本兼容性检查方法的相关资料,文中提供四种检查方法,分别是pip查询、conda管理、PyP... 目录引言为什么会出现兼容性问题方法一:用 pip 官方命令查询可用版本方法二:conda 管理包环境方法

基于Python开发Windows自动更新控制工具

《基于Python开发Windows自动更新控制工具》在当今数字化时代,操作系统更新已成为计算机维护的重要组成部分,本文介绍一款基于Python和PyQt5的Windows自动更新控制工具,有需要的可... 目录设计原理与技术实现系统架构概述数学建模工具界面完整代码实现技术深度分析多层级控制理论服务层控制注

pycharm跑python项目易出错的问题总结

《pycharm跑python项目易出错的问题总结》:本文主要介绍pycharm跑python项目易出错问题的相关资料,当你在PyCharm中运行Python程序时遇到报错,可以按照以下步骤进行排... 1. 一定不要在pycharm终端里面创建环境安装别人的项目子模块等,有可能出现的问题就是你不报错都安装

Python打包成exe常用的四种方法小结

《Python打包成exe常用的四种方法小结》本文主要介绍了Python打包成exe常用的四种方法,包括PyInstaller、cx_Freeze、Py2exe、Nuitka,文中通过示例代码介绍的非... 目录一.PyInstaller11.安装:2. PyInstaller常用参数下面是pyinstal

Python爬虫HTTPS使用requests,httpx,aiohttp实战中的证书异步等问题

《Python爬虫HTTPS使用requests,httpx,aiohttp实战中的证书异步等问题》在爬虫工程里,“HTTPS”是绕不开的话题,HTTPS为传输加密提供保护,同时也给爬虫带来证书校验、... 目录一、核心问题与优先级检查(先问三件事)二、基础示例:requests 与证书处理三、高并发选型:

Python中isinstance()函数原理解释及详细用法示例

《Python中isinstance()函数原理解释及详细用法示例》isinstance()是Python内置的一个非常有用的函数,用于检查一个对象是否属于指定的类型或类型元组中的某一个类型,它是Py... 目录python中isinstance()函数原理解释及详细用法指南一、isinstance()函数