首页 > 其他分享 >pygame篮球弹跳

pygame篮球弹跳

时间:2022-12-02 11:01:10浏览次数:45  
标签:ball ballrect 篮球 小球 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,篮球,小球,pygame,弹跳,speed,event
From: https://www.cnblogs.com/plxels/p/16943730.html

相关文章

  • 篮球自动弹跳
    代码:#-*-coding:utf-8-*-importsys#导入sys模块importpygame#导入pygame模块pygame.init()#初始化pygamesize=width,height=640,480#设置窗口scr......
  • 弹跳小球
    弹跳小球importsysimportpygamepygame.init()#初始化pygamesize=width,height=640,480#设置窗口大小screen=pygame.display.set_mode(size)#显......
  • 弹跳球
    importsysimportpygamepygame.init()size=width,height=640,480screen=pygame.display.set_mode(size)color=(0,0,0)ball=pygame.image.load("th.jpg")bal......
  • python篮球自动弹跳
           具体思路是首先导入sys和pygame模块然后初始化pygame然后显示窗口加载篮球图片执行死循环检查事件设置移动篮球将图片画在窗口上最后更新全部显示......
  • 篮球自动弹跳
    importsysimportpygamepygame.init()size=width,height=640,480screen=pygame.display.set_mode(size)color=(0,0,0)ball=pygame.image.......
  • 跳动的篮球
    importsys#导入sys模块importpygame#导入pygame模块pygame.init()#初始化pygame......
  • 第十三章实例1篮球自动弹跳
    #-*-coding:utf-8-*-importsys#导入sys模块importpygame#导入pygame模块pygame.init......
  • 篮球自动跳跃
    importsysimportpygamepygame.init()#初始化pygamesize=width,height=640,640#设置窗口screen=pygame.display.set_mode(size)#显示窗口color=(0,0,......
  • 篮球自动弹跳
    #-*-codings:utf-8-*-importsysimportpygamepygame.init()size=width,height=640,480screen=pygame.display.set_mode(size)color=(0,0,0)ball=pygame.imag......
  • 篮球
         ......