pygame實(shí)現(xiàn)彈力球及其變速效果
本文實(shí)例為大家分享了pygame實(shí)現(xiàn)彈力球及其變速效果的具體代碼,供大家參考,具體內(nèi)容如下
期望:
1.球體接觸到框體后反彈
2.設(shè)置速度按鍵,按下后改變球體速度、顏色狀態(tài)
具體實(shí)現(xiàn):
import pygame from pygame.locals import * import sys, random class Circle(object): # 設(shè)置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) # 防止球體速度方向發(fā)生改變 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 如果僅此句,速度方向會(huì)發(fā)生改變 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實(shí)例 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()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python中樹(shù)與樹(shù)的表示知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家分享的是關(guān)于python中樹(shù)與樹(shù)的表示的相關(guān)知識(shí)點(diǎn),需要的讀者們學(xué)習(xí)下吧。2019-09-09詳解pandas中利用DataFrame對(duì)象的.loc[]、.iloc[]方法抽取數(shù)據(jù)
這篇文章主要介紹了pandas中利用DataFrame對(duì)象的.loc[]、.iloc[]方法抽取數(shù)據(jù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12python實(shí)現(xiàn)監(jiān)控linux性能及進(jìn)程消耗性能的方法
這篇文章主要介紹了python實(shí)現(xiàn)監(jiān)控linux性能及進(jìn)程消耗性能的方法,需要的朋友可以參考下2014-07-07詳解Python如何精確控制asyncio并發(fā)運(yùn)行多個(gè)任務(wù)
這篇文章主要為大家詳細(xì)介紹了Python如何精確控制asyncio并發(fā)運(yùn)行多個(gè)任務(wù),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-10-10用python生成mysql數(shù)據(jù)庫(kù)結(jié)構(gòu)文檔
大家好,本篇文章主要講的是用python生成mysql數(shù)據(jù)庫(kù)結(jié)構(gòu)文檔,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01python基于scrapy爬取京東筆記本電腦數(shù)據(jù)并進(jìn)行簡(jiǎn)單處理和分析
這篇文章主要介紹了python基于scrapy爬取京東筆記本電腦數(shù)據(jù)并進(jìn)行簡(jiǎn)單處理和分析的實(shí)例,幫助大家更好的理解和學(xué)習(xí)使用python。感興趣的朋友可以了解下2021-04-04Python pyautogui模塊實(shí)現(xiàn)鼠標(biāo)鍵盤自動(dòng)化方法詳解
這篇文章主要介紹了Python pyautogui 模塊實(shí)現(xiàn)鼠標(biāo)鍵盤自動(dòng)化方法詳解,需要的朋友可以參考下2020-02-02python通用讀取vcf文件的類(復(fù)制粘貼即可用)
這篇文章主要介紹了python通用讀取vcf文件的類(可以直接復(fù)制粘貼使用) ,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02python實(shí)現(xiàn)批量轉(zhuǎn)換圖片為黑白
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)批量轉(zhuǎn)換圖片為黑白,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06python實(shí)現(xiàn)內(nèi)存監(jiān)控系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)內(nèi)存監(jiān)控系統(tǒng),通過(guò)系統(tǒng)命令或操作系統(tǒng)文件獲取到內(nèi)存信息,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06