python实现小球弹跳效果

yipeiwu_com6年前Python基础

本文实例为大家分享了python实现小球弹跳效果的具体代码,供大家参考,具体内容如下

import pygame, sys
 
pygame.init()
 
screenGameCaption = pygame.display.set_caption("Ball game")
screen = pygame.display.set_mode([680, 480])
 
screen.fill([255, 255, 255])
x = 50
y = 50
x_speed = 10
y_speed = 10
# load img
my_ball = pygame.image.load('ball.png')
 
running = True
 
while running:
 for event in pygame.event.get():
  if event.type == pygame.QUIT:
   running = False
 pygame.time.delay(100)
 pygame.draw.rect(screen, [255, 255, 255], [x, y, 190, 190], 0)
 x = x + x_speed
 y = y + y_speed
 
 if x > screen.get_width() - 190 or x < 0:
  x_speed = - x_speed
 if y > screen.get_height() - 190 or y < 0:
  y_speed = - y_speed
 screen.blit(my_ball, [x, y])
 pygame.display.flip()
pygame.quit()

效果图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

简单谈谈python中的语句和语法

python程序结构 python“一切皆对象”,这是接触python听到最多的总结了。在python中最基层的单位应该就是对象了,对象需要靠表达式建立处理,而表达式往往存在于语句中,多...

python使用socket 先读取长度,在读取报文内容示例

本文实例讲述了python使用socket 先读取长度,在读取报文内容。分享给大家供大家参考,具体如下: tlpmts1:~/sbin # cat test9105.py # -*-...

flask入门之表单的实现

一、原生表单 form.html {% extends 'common/base.html' %} {% block title %} 原生表单 {% endblock %}...

使用Python 统计高频字数的方法

问题 (来自Udacity机器学习工程师纳米学位预览课程) 用 Python 实现函数 count_words(),该函数输入字符串 s 和数字 n,返回 s 中 n 个出现频率最高的单...

浅谈django三种缓存模式的使用及注意点

浅谈django三种缓存模式的使用及注意点

django是动态网页,一般来说需要实时的生成访问的页面,展示给访问者,这样,内容可以随时变化,也就说请求到达视图函数之后,然后进行模板渲染,将字符串返回给用户,用户会看到相应的html...