pygame实现弹力球及其变速效果
Python  /  管理员 发布于 5年前   439
本文实例为大家分享了pygame实现弹力球及其变速效果的具体代码,供大家参考,具体内容如下
期望:
1.球体接触到框体后反弹
2.设置速度按键,按下后改变球体速度、颜色状态
具体实现:
import pygamefrom pygame.locals import *import sys, randomclass Circle(object): # 设置Circle类属性 def __init__(self): self.vel_x = 1 self.vel_y = 1 self.radius = 20 self.pos_x, self.pos_y = random.randint(0, 255), random.randint(0, 255) self.width = 0 self.color = 0, 0, 0 # 球体颜色速度改变方法 def change_circle(self, number): self.color = random.randint(0, 255), random.randint(0, 255), random.randint(0, 255) # 防止球体速度方向发生改变 if self.vel_x < 0: self.vel_x = -number else: self.vel_x = number if self.vel_y < 0: self.vel_y = -number else: self.vel_y = number # self.vel_x, self.vel_y = number, number 如果仅此句,速度方向会发生改变 def circle_run(self): # 防止球体超出游戏界面框体 if self.pos_x > 580 or self.pos_x < 20: self.vel_x = -self.vel_x if self.pos_y > 480 or self.pos_y < 20: self.vel_y = -self.vel_y self.pos_x += self.vel_x self.pos_y += self.vel_y pos = self.pos_x, self.pos_y pygame.draw.circle(screen, self.color, pos, self.radius, self.width)pygame.init()screen = pygame.display.set_mode((600, 500))# Circle实例circle1 = Circle()while True: for event in pygame.event.get(): if event.type == QUIT: sys.exit() elif event.type == KEYUP: if event.key == pygame.K_1: circle1.change_circle(1) elif event.key == pygame.K_2: circle1.change_circle(2) elif event.key == pygame.K_3: circle1.change_circle(3) elif event.key == pygame.K_4: circle1.change_circle(4) screen.fill((0, 0, 100)) circle1.circle_run() pygame.display.update()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号