首页 > 其他分享 >篮球跳动实例

篮球跳动实例

时间:2022-12-03 10:45:10浏览次数:39  
标签:ball ballrect 实例 color 篮球 跳动 pygame speed event

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("足球.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:
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()

pygame.quit()

标签:ball,ballrect,实例,color,篮球,跳动,pygame,speed,event
From: https://www.cnblogs.com/rweiq/p/16947120.html

相关文章

  • python:一个pygame篮球自动弹跳
    一个pygame篮球自动弹跳代码:importsysimportpygamepygame.init()size=width,height=640,480screen=pygame.display.set_mode(size)color=(0,0,0)bal......
  • 篮球自动弹跳
    importpygame,syspygame.init()screenGameCaption=pygame.display.set_caption("Ballgame")screen=pygame.display.set_mode([680,480])screen.fill([255,255,25......
  • redis字符串的底层源码以及应用实例
    内部编码int8个字节的长整型embstr小于等于39个字节的字符串raw大于39个字节的字符串string底层使用的sds自定义的字符串,因为c语言中string默认为\0为结尾,而redi......
  • 13 刘欣晨 第五章实例+实战
    实验 一 项目名称:     使用字符串拼接输出一个关于程序员的笑话programmer_1='程序员甲:搞IT太辛苦了,我想换行....怎么办?'programmer_2='程序员乙:敲一下回车......
  • vue3 + element plus 使用字节跳动图标
    使用场景:提一下vue2用法>> 下面回到正题vue3用法1 安装包:npminstall@icon-park/vue-next--save2 字节跳动图标库取图地址>>  3 用法:<te......
  • C++ 设计一个类模板,有数据成员T data[size],有求最大值的方法getMax()和排序的方法sort
    #include<iostream>#include<string>usingnamespacestd;template<typenameT,intsize>classData{Tdata[size];public:Data(){cout<<......
  • mongodb单实例lvm快照备份和恢复
    ps-ef|grepmongo            #查看mongodb实例数据目录所在位置root     1525 1389 320:44pts/0   00:00:17/usr/local/mongodb/bin/mon......
  • 13 刘欣晨 第十三章实例
    篮球跳跃实验 将该图片与代码放在同一文件夹中文件名及其后缀为ball1.jpg代码部分:importsys   #导入sys模块importpygame......
  • 跳动篮球-
    点击查看代码importsysimportpygamepygame.init()size=width,height=640,480screen=pygame.display.set_mode(size)color=(121,178,250)ball=pyg......
  • pygame篮球弹跳
     Pygame的基本应用 创建一个游戏窗口,然后再窗口内创建一个小球。以一定的速度移动小球,当小球碰到游戏窗口的边缘时,小球弹回,继续移动。importsysimportpygamepyg......