首页 > 编程语言 >python:一个pygame篮球自动弹跳

python:一个pygame篮球自动弹跳

时间:2022-12-03 10:23:39浏览次数:32  
标签:ball ballrect python screen pygame 弹跳 speed event

一个pygame篮球自动弹跳

代码:

import sys
import pygame

pygame.init()
size = width, height = 640,480
screen = pygame.display.set_mode(size)
color = (0,0,0)

ball = pygame.image.load("ball.png")
ballrect = ball.get_rect()

speed = [5,5]
clock = pygame.time.Clock()

while True:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    ballrect = ballrect.move(speed)
    if ballrect.left < 0 or ballrect.right > width:
        speed[0] = -speed[0]
    if ballrect.top < 0 or ballrect.bottom > height:
        speed[1] = -speed[1]

    screen.fill(color)
    screen.blit(ball,ballrect)
    pygame.display.flip()

运行结果:

 

 

 

 

标签:ball,ballrect,python,screen,pygame,弹跳,speed,event
From: https://www.cnblogs.com/lhtt/p/16947072.html

相关文章

  • Python实验报告
    实验13:Pygame游戏编程一、实验目的和要求学会Pygame的基本应用二、Pygame的优点及应用  使用Python进行游戏开发的首选模块就是Pygame,专为电子游戏设计(包括图像、......
  • Python笔记-多进程多线程
    日常运维中,经常需要并发来提升工作效率。Python提供了多线程和多进程两种方式。importtimeimportthreadingimportmultiprocessingdefprint_fun(num):print(time.str......
  • Python笔记-脚本参数传递
    编写Python脚本,经常需要从外部传递参数,此时需要用到getopt和sys。语法如下:getopt.getopt(args,shortopts,longopts=[])args参数列表shortopts短参数,如:-hlongopt......
  • Python笔记-Python2和Python3兼容
    Python2与Python3在很数据类型、语法上面都有很大区别。为保证编写的脚本在Python2和Python3下兼容,需要在代码中做版本判断。示例代码如下:importsyspversion=int(s......
  • Python笔记-从配置读取参数
    实用的脚步通常需要一些动态参数,如果参数太多,从命令行传递就太麻烦了。从配置文件读取,是比较实用的方法。以下示例为从test.cfg中读取参数,配置文件为json格式。配置文件......
  • Python 让图像变卡通图
    ✅作者简介:热爱科研的算法开发者,Python、Matlab项目可交流、沟通、学习。......
  • python解析yaml文件
    1、初始化yaml文件的目录def__init__(self):self.yaml_root_path='D:\\Code\\PythonProject\\UIAutoProject\\config\\yaml\\'2、读取yaml文件的方法defread_......
  • Python遍历某个文件夹下的所有文件夹,每个文件夹只保留最新7个文件
    importosroot=r"D:\_back"fordirpath,dirnames,filenamesinos.walk(root):fordirnameindirnames:_dir=os.path.join(dirpath,dirname)......
  • Python实验报告——第13章 Pygame游戏编程
    实验报告实例01:制作一个跳跃的小球游戏代码如下:importsysimportpygamepygame.init()size=width,height=640,480screem=pygame.display.set_mode(size)c......
  • python 高阶函数
    高阶函数(High-orderFunction)​ 数学概念y=f(g(x))​ 在数学和计算机科学中,高阶函数应当是至少满足下面一个条件的函数​ 接受一个或多个函数作为参数​ 输......