首页 > 其他分享 >篮球自动弹跳

篮球自动弹跳

时间:2022-12-01 20:55:39浏览次数:39  
标签:ball ballrect 篮球 自动 pygame 弹跳 speed event size

(1)创建游戏窗口

import sys
import pygame
pygame.init()
size=width,height=640,480
screen=pygame.display.set_mode(size)

 

 

(2)让窗口一直显示

import sys
import pygame
pygame.init()
size=width,height=640,480
screen=pygame.display.set_mode(size)
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
sys.exit()

(3)在窗口添加小球

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("ball3.jpg")
ballrect=ball.get_rect()
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill(color)
screen.blit(ball,ballrect)
pygame.display.flip()

 

 

(4)让小球动起来

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("ball3.jpg")
ballrect=ball.get_rect()
speed=[5,5]
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
sys.exit()
ballrect=ballrect.move(speed)
screen.fill(color)
screen.blit(ball,ballrect)
pygame.display.flip()

(5)更改小球运动的方向

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("ball3.jpg")
ballrect=ball.get_rect()
speed=[5,5]
while True:
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()

 

 

(6)让篮球自动弹跳

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("ball3.jpg")
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,篮球,自动,pygame,弹跳,speed,event,size
From: https://www.cnblogs.com/666u/p/16942668.html

相关文章

  • SpringCloud(六) - RabbitMQ安装,三种消息发送模式,消息发送确认,消息消费确认(自动,手动)
    1、安装erlang语言环境1.1创建erlang安装目录mkdirerlang1.2上传解压压缩包上传到:/root/解压缩#tar-zxvfotp_src_22.0.tar.gz1.3进入解压缩目录,指定目......
  • Springboot自动装配源码及启动原理理解
    Springboot自动装配源码及启动原理理解springboot版本:2.2.2传统的Spring框架实现一个Web服务,需要导入各种依赖JAR包,然后编写对应的XML配置文件等,相较而言,SpringBoot显......
  • C#窗体控件,文字随电脑分辨率自动调整大小
    一、在类中添加方法,代码如下:#region窗体控件、字体随分辨率调整,自动调整大小publicstaticvoidSetTag(Controlcons){foreach(Cont......
  • 谷歌浏览器自动保存密码的解密
    起源今天看到了这个视频,大致讲的是谷歌自动存储的密码是放在本地的,但是以密文的方式存储,加密方式是AES-256inGCMmode,但是密钥也存储在本地,也就是说可以手动完成解密(虽......
  • springboot 项目自动重启脚本及注册方式
    创建脚本文件/etc/init.d/prs_xml.jar 添加脚本chkconfig-add prs_xml.jar查看服务列表chkconfig--list 启动服务chkconfigprs_xml.jaron设置启动等级chk......
  • 通过Jenkins自动化部署简单的前后端项目
    看公司成员,每次都为​​部署​​消耗不少时间,工作效率低,就想着提高工作效率,所以就尝试搭建jenkins前后端自动化部署项目。最后搭建完成也是非常开心。因为这效率提高不止......
  • 后缀数组&后缀自动机
    SA后缀排序的中心思想是倍增,为了优化常数会使用一些比较特别的技巧。写法上主要分为两个部分,即预处理部分和倍增部分。首先定义几个数组,\(sa_i\)是排名为\(i\)的数组的......
  • AC 自动机
    AC自动机是更高维的kmp。构造方法感觉更像是运用了dp的思想,先对所有模式串建立一棵Trie树,然后考虑某个节点\(x\),有个\(w\)的后缀,考虑如何去求它的fail值。于是......
  • WordPress编辑器支持Word文档自动粘贴
    ​如何做到ueditor批量上传word图片?1、前端引用代码<!DOCTYPE html PUBLIC "-//W3C//DTDXHTML1.0Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-......
  • 自动泊车-RISC-V-飞桨生态分析
    自动泊车-RISC-V-飞桨生态分析参考文献链接https://mp.weixin.qq.com/s/5tsJlc3fydFsxpA__qCg2Ahttps://mp.weixin.qq.com/s/OHiUdvyDs13YKj5HVI7jHghttps://mp.weixin......