python?pygame實現(xiàn)控制物體移動
更新時間:2022年01月04日 08:55:58 作者:大學(xué)生編程地
這篇文章主要為大家詳細(xì)介紹了python?pygame控制物體移動,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了python pygame控制物體移動的具體代碼,供大家參考,具體內(nèi)容如下
import pygame import sys from pygame.locals import * pygame.init() size = width,hight = 600,400 speed = [-2,1] by = (255,255,255) screen = pygame.display.set_mode(size) pygame.display.set_caption("移動皮卡丘") #這個將圖片路徑,改為自己想要的物體的路徑 pp = pygame.image.load("E:\python\Python練習(xí)\imgs\pp.jpg") position = pp.get_rect() l_head = pp r_head = pygame.transform.flip(pp,True,False) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == KEYDOWN: if event.key == K_LEFT: speed = [-1,0] if event.key == K_RIGHT: speed = [1,0] if event.key == K_UP: speed = [0,-1] if event.key == K_DOWN: speed = [0,1] position = position.move(speed) if position.left < 0 or position.right > width: pp = pygame.transform.flip(pp,True,False) speed[0] = -speed[0] if position.top < 0 or position.bottom > hight : speed[1] = -speed[1] screen.fill(by) screen.blit(pp,position) pygame.display.flip() pygame.time.delay(10)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關(guān)文章
python3.8.3安裝教程及環(huán)境配置的詳細(xì)教程(64-bit)
這篇文章主要介紹了python3.8.3安裝教程及環(huán)境配置的詳細(xì)教程(64-bit),本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11詳解python statistics模塊及函數(shù)用法
本節(jié)介紹 Python 中的另一個常用模塊 —— statistics模塊,該模塊提供了用于計算數(shù)字?jǐn)?shù)據(jù)的數(shù)理統(tǒng)計量的函數(shù)。這篇文章重點給大家介紹python statistics 模塊的一些用法,感興趣的朋友跟隨小編一起看看吧2019-10-10Python發(fā)送網(wǎng)絡(luò)請求(requests)
這篇文章主要介紹了Python發(fā)送網(wǎng)絡(luò)請求(requests),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09