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類(lèi)屬性
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í)現(xiàn)一個(gè)簡(jiǎn)單的瀏覽器引擎
瀏覽器引擎是用來(lái)處理、渲染和顯示網(wǎng)頁(yè)內(nèi)容的核心組件,其主要任務(wù)是將用戶輸入的URL所代表的網(wǎng)頁(yè)資源加載并呈現(xiàn)出來(lái),通常包括HTML、CSS、JavaScript以及各種多媒體內(nèi)容,本文給大家介紹了如何基于python實(shí)現(xiàn)一個(gè)簡(jiǎn)單的瀏覽器引擎,需要的朋友可以參考下2024-10-10
Python import導(dǎo)入上級(jí)目錄文件的方法
這篇文章主要介紹了Python import導(dǎo)入上級(jí)目錄文件,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-01-01
Python爬蟲(chóng)實(shí)現(xiàn)熱門(mén)電影信息采集
這篇文章主要介紹了利用Python爬蟲(chóng)采集熱門(mén)電影信息,文中示例代碼很詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴快來(lái)跟隨小編一起學(xué)習(xí)吧2021-12-12
python雙端隊(duì)列原理、實(shí)現(xiàn)與使用方法分析
這篇文章主要介紹了python雙端隊(duì)列原理、實(shí)現(xiàn)與使用方法,結(jié)合實(shí)例形式分析了Python雙端隊(duì)列的概念、原理、定義及使用方法,需要的朋友可以參考下2019-11-11
python selenium 執(zhí)行完畢關(guān)閉chromedriver進(jìn)程示例
今天小編就為大家分享一篇python selenium 執(zhí)行完畢關(guān)閉chromedriver進(jìn)程示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11
解決Python plt.savefig 保存圖片時(shí)一片空白的問(wèn)題
今天小編就為大家分享一篇解決Python plt.savefig 保存圖片時(shí)一片空白的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
Python 實(shí)現(xiàn)PS濾鏡中的徑向模糊特效
這篇文章主要介紹了Python 實(shí)現(xiàn) PS 濾鏡中的徑向模糊特效,幫助大家更好的利用python處理圖片,感興趣的朋友可以了解下2020-12-12

