python实现小球弹跳效果

yipeiwu_com5年前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 和 lint 删除项目无用资源的方法

有部分老项目是在Eclipse环境开发的,最近公司要求应用瘦身,老项目也在其中。如果在 AS 下开发就不会有这样的问题,但是在 Eclipse 中就不太方便了,于是就写了这个脚本。第一次...

pandas带有重复索引操作方法

有的时候,可能会遇到表格中出现重复的索引,在操作重复索引的时候可能要注意一些问题。 一、判断索引是否重复 a、Series索引重复判断 s = Series([1,2,3,4,5],...

Python变量类型知识点总结

Python变量类型知识点总结

变量存储在内存中的值。这就意味着在创建变量时会在内存中开辟一个空间。 基于变量的数据类型,解释器会分配指定内存,并决定什么数据可以被存储在内存中。 因此,变量可以指定不同的数据类型,...

21行Python代码实现拼写检查器

引入 大家在使用谷歌或者百度搜索时,输入搜索内容时,谷歌总是能提供非常好的拼写检查,比如你输入 speling,谷歌会马上返回 spelling。 下面是用21行python代码实现的一...

对Tensorflow中的矩阵运算函数详解

tf.diag(diagonal,name=None) #生成对角矩阵 import tensorflowas tf; diagonal=[1,1,1,1] with tf.Sess...