国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

pygame實現彈力球及其變速效果

2020-02-16 01:47:46
字體:
來源:轉載
供稿:網友

本文實例為大家分享了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()

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林站長站。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 铜山县| 石嘴山市| 垫江县| 玉树县| 富平县| 乌恰县| 澳门| 阳朔县| 上高县| 襄汾县| 中江县| 会东县| 金塔县| 牡丹江市| 旌德县| 阿勒泰市| 运城市| 南城县| 广汉市| 志丹县| 澄城县| 江门市| 肇庆市| 樟树市| 乾安县| 福海县| 孟州市| 延长县| 江口县| 五指山市| 明水县| 手游| 稻城县| 北海市| 宣化县| 铁力市| 洪泽县| 盐亭县| 怀集县| 大埔区| 乐平市|