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

首頁 > 學院 > 開發設計 > 正文

pygame從入門到放棄(一)

2019-11-08 02:09:29
字體:
來源:轉載
供稿:網友

首先pip 那個pygame; 然后看代碼; 臨時寫的,圖片就不插了(防止舍友砍我,現在是凌晨。。。。) [TOC]

井字棋游戲

# 此代碼基本能立于不敗之地;import random#可視化輸出def draw_Board(board): hello world#學會能畫出圖形#這稱不上是一個游戲import pygame, sysfrom pygame.locals import *pygame.init()window_Surface = pygame.display.set_mode((500, 400), 0, 32)pygame.display.set_caption('Hello world!')#RBGBLACK = (0, 0, 0)WHITE = (255, 255, 255)RED = (255, 0, 0)GREEN = (0, 255, 0)BLUE = (0, 0, 255)#fontsbasic_Font = pygame.font.SysFont(None, 48)text = basic_Font.render('Hello world~~', True, WHITE, BLUE)text_Rect = text.get_rect()text_Rect.centerx = window_Surface.get_rect().centerxtext_Rect.centery = window_Surface.get_rect().centerywindow_Surface.fill(WHITE)pygame.draw.polygon(window_Surface, GREEN, ((146, 0), (291, 106), (236, 277), (56, 277), (0, 106)))pygame.draw.line(window_Surface, BLUE, (60, 60), (120, 60), 4)pygame.draw.line(window_Surface, BLUE, (120, 60), (60, 120))pygame.draw.line(window_Surface, BLUE, (60, 120), (120, 120), 4)pygame.draw.circle(window_Surface, BLUE, (300, 50), 20, 1)pygame.draw.ellipse(window_Surface, RED, (300, 250, 40, 80), 1)pygame.draw.rect(window_Surface, RED, (text_Rect.left - 20, text_Rect.top - 20, text_Rect.width + 40, text_Rect.height + 40))pixArry = pygame.PixelArray(window_Surface)pixArry[480][380] = BLACKdel pixArrywindow_Surface.blit(text, text_Rect)pygame.display.update()while 1: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit()

移動的方塊

#和上式差不多,也是畫出來的import pygame, sys, timeimport randomfrom pygame.locals import *pygame.init()window_Width = 400window_Height = 400window_Surface = pygame.display.set_mode((window_Width, window_Height), 0, 32)pygame.display.set_caption('Animation')down_Left = 1down_Right = 3up_Left = 7up_Right = 9move_Speed = 4Black = (0, 0, 0)RED = (255, 0, 0)GREEN =(0, 255, 0)BLUE = (0, 0, 255)b1 = {'rect':pygame.Rect(300, 80, 50, 100),'color':RED, 'dir':up_Right} #紅長方形b2 = {'rect':pygame.Rect(120, 200, 20, 20),'color':GREEN, 'dir':up_Left}#綠b3 = {'rect':pygame.Rect(100, 150, 60, 60),'color':BLUE, 'dir':down_Left}#藍blocks = [b1, b2, b3]while 1: window_Surface.fill(Black) for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() for b in blocks: #移動 if b['dir'] == down_Left: b['rect'].left -= move_Speed b['rect'].top += move_Speed if b['dir'] == down_Right: b['rect'].left += move_Speed b['rect'].top += move_Speed if b['dir'] == up_Left: b['rect'].left -= move_Speed b['rect'].top -= move_Speed if b['dir'] == up_Right: b['rect'].left += move_Speed b['rect'].top -= move_Speed#反彈 if b['rect'].top < 0: if b['dir'] == up_Left: b['dir'] = down_Left if b['dir'] == up_Right: b['dir'] = down_Right if b['rect'].bottom > window_Height: if b['dir'] == down_Left: b['dir'] = up_Left if b['dir'] == down_Right: b['dir'] = up_Right if b['rect'].left < 0: if b['dir'] == down_Left: b['dir'] = down_Right if b['dir'] == up_Left: b['dir'] = up_Right if b['rect'].right > window_Width: if b['dir'] == down_Right: b['dir'] = down_Left if b['dir'] == up_Right: b['dir'] = up_Left pygame.draw.rect(window_Surface, b['color'], b['rect'])#重置 pygame.display.update() time.sleep(random.uniform(0.01,0.03)) #控制時間隨機速度

移動的方塊二

#?(^?^●)?隨便改了一下,和上面的差不多import pygame, sys, randomfrom pygame.locals import *#查看是否吃到食物def do_Rects_Overlap(rect1,rect2): for a, b in [(rect1, rect2), (rect2, rect1)]: if((is_Point_Inside_Rect(a.left, a.top, b))or (is_Point_Inside_Rect(a.left, a.bottom, b)) or (is_Point_Inside_Rect(a.right, a.top, b)) or (is_Point_Inside_Rect(a.right, a.bottom, b))): return True return Falsedef is_Point_Inside_Rect(x, y, rect): if (x > rect.left) and (x < rect.right) and (y > rect.top ) and (y < rect .bottom): return True else: return False#初始化pygame.init()mainClock = pygame.time.Clock()#游戲界面大小window_Width = 800window_Height = 600window_Surface = pygame.display.set_mode((window_Width, window_Height),0,32)#標題pygame.display.set_caption("呼哧's game")down_Left = 1down_Right = 3up_Left = 7up_Right = 9#速度move_Speed = 4#顏色BLACK = (0, 0, 0)GREEN = (0, 255, 0)WHITE = (255, 255, 255)#食物計數food_Counter = 0new_Food = 4#食物大小food_Size = 2bouncer = {'rect':pygame.Rect(300, 100, 50, 50), 'dir':down_Left}foods = []#初始化有6000個食物for i in range(6000): foods.append(pygame.Rect(random.randint(0, window_Width - food_Size), random.randint(0, window_Height - food_Size), food_Size, food_Size))while True: for event in pygame.event.get(): #關閉按鈕 if event.type == QUIT: pygame.quit() sys.exit()#增加食物功能 food_Counter += 1 if food_Counter >= new_Food : food_Counter = 0 foods.append(pygame.Rect(random.randint(0, window_Width - food_Size), random.randint(0, window_Height - food_Size), food_Size, food_Size))#填充 window_Surface.fill(BLACK)#移動 if bouncer['dir'] == down_Left : bouncer['rect'].left -= move_Speed bouncer['rect'].top += move_Speed if bouncer['dir'] == down_Right : bouncer['rect'].left += move_Speed bouncer['rect'].top += move_Speed if bouncer['dir'] == up_Left : bouncer['rect'].left -= move_Speed bouncer['rect'].top -= move_Speed if bouncer['dir'] == up_Right : bouncer['rect'].left += move_Speed bouncer['rect'].top -= move_Speed#反彈 if bouncer['rect'].top < 0: if bouncer['dir'] == up_Left: bouncer['dir'] = down_Left if bouncer['dir'] == up_Right: bouncer['dir'] = down_Right if bouncer['rect'].bottom > window_Height: if bouncer['dir'] == down_Left: bouncer['dir'] = up_Left if bouncer['dir'] == down_Right: bouncer['dir'] = up_Right if bouncer['rect'].left < 0: if bouncer['dir'] == up_Left: bouncer['dir'] = up_Right if bouncer['dir'] == down_Left: bouncer['dir'] = down_Right if bouncer['rect'].right > window_Width: if bouncer['dir'] == down_Right: bouncer['dir'] = down_Left if bouncer['dir'] == up_Right: bouncer['dir'] = up_Left #移動的方塊 pygame.draw.rect(window_Surface, WHITE, bouncer['rect']) #擦去綠色方塊 for food in foods[:]: if do_Rects_Overlap(bouncer['rect'], food): foods.remove(food) #顯示綠色方塊 for i in range(len(foods)): pygame.draw.rect(window_Surface, GREEN, foods[i]) #重置 pygame.display.update() #類似time.sleep mainClock.tick(40)

可操作的方塊一

#有了這個是不是就感覺有了天下一樣呢,哈哈哈。^_^#游戲簡要說明:上下左右wsad或者上下左右;#單機增加食物,x隨機移動;#食物變化具體請看代碼;import pygame, sys, random, timefrom pygame.locals import *#初始化pygame.init()main_Clock = pygame.time.Clock()#界面設計window_Width = 800window_Height = 500window_Surface = pygame.display.set_mode((window_Width, window_Height), 0, 32)pygame.display.set_caption("Huchi's moving rect")#顏色BLACK = (0, 0, 0)RED = (255, 0, 0)GREEN = (0, 255, 0)BLUE = (0, 0, 255)WHITE = (255, 255, 255)#食物food_Counter = 0new_Food = 40food_Size = 20player = pygame.Rect(300, 0, 50 ,50)foods = []for i in range(20): foods.append(pygame.Rect(random.randint(0, window_Width - food_Size), random.randint(0, window_Height - food_Size), food_Size, food_Size))#移動move_Left = Falsemove_Right = Falsemove_Up = Falsemove_Down = False#速度move_Speed = 10while True: for event in pygame.event.get(): #關閉按鈕 if event.type == QUIT: pygame.quit() sys.exit()#按鍵 if event.type == KEYDOWN: if event.key == K_LEFT or event.key == ord('a'): move_Right = False move_Left = True if event.key == K_RIGHT or event.key == ord('d'): move_Left = False move_Right = True if event.key == K_UP or event.key == ord('w'): move_Down = False move_Up = True if event.key == K_DOWN or event.key == ord('s'): move_Up = False move_Down = True#松開 if event.type == KEYUP: if event.key == K_ESCAPE: pygame.quit() sys.exit() if event.key == K_LEFT or event.key == ord('a'): move_Left = False if event.key == K_RIGHT or event.key == ord('d'): move_Right = False if event.key == K_UP or event.key == ord('w'): move_Up = False if event.key == K_DOWN or event.key == ord('s'): move_Down = False if event.key == ord('x'): player.top = random.randint(0, window_Height - player.height) player.left = random.randint(0, window_Width - player.width) #鼠標增加 if event.type == MOUSEBUTTONUP: foods.append(pygame.Rect(random.randint(0, window_Width - food_Size), random.randint(0, window_Height - food_Size), food_Size,food_Size))#食物增加 food_Counter += 1 if food_Counter >= new_Food: food_Counter = 0 foods.append(pygame.Rect(random.randint(0, window_Width - food_Size), random.randint(0, window_Height - food_Size), food_Size, food_Size)) # 填充 window_Surface.fill(BLACK)#阻止越界 if move_Down and player.bottom < window_Height: player.top += move_Speed if move_Up and player.top > 0: player.top -= move_Speed if move_Left and player.left > 0: player.left -= move_Speed if move_Right and player.right <window_Width: player.left += move_Speed#畫玩者的rect pygame.draw.rect(window_Surface, WHITE, player)#吃掉食物 for food in foods[:]: if player.colliderect(food): foods.remove(food)#畫食物 for i in range(len(foods)): pygame.draw.rect(window_Surface, GREEN, foods[i])#動態化 pygame.display.update() main_Clock.tick(40)#勝利 if len(foods) == 0: basic_Font = pygame.font.SysFont(None, 48) text = basic_Font.render('You won!', True, BLACK, BLUE) text_Rect = text.get_rect() window_Surface.blit(text, text_Rect) pygame.display.update() time.sleep(1)

入門期的第一個成果

#加入了圖片,在上述的游戲中加入了圖片與背景音樂(可以開關);#我的圖片與bgm是放在同目錄的文件下的;import pygame, sys, random, timefrom pygame.locals import *pygame.init()main_Clock = pygame.time.Clock()window_Width = 800window_Height = 644window_Surface = pygame.display.set_mode((window_Width, window_Height),0 ,32)#顏色BLACK = (0, 0, 0)RED = (255, 0, 0)GREEN = (0, 255, 0)BLUE = (0, 0, 255)#食物設置food_Counter = 0new_Food = 20food_Size = 100#玩家圖片player = pygame.Rect(300, 100, 40, 40)player_Image = pygame.image.load('images//player.jpg')#圖片1player_Stretched_Image = pygame.transform.scale(player_Image, (20, 20))food_Image = pygame.image.load('images//food.jpg')foods_Image = pygame.transform.scale(food_Image, (20, 20))foods = []#初始20個食物for i in range(20): foods.append(pygame.Rect(random.randint(0, window_Width - 20), random.randint(0, window_Height - 20), food_Size, food_Size ))#移動move_Left = Falsemove_Right = Falsemove_Up = Falsemove_Down = False#速度move_Speed = 10#背景音樂pickup_Sound = pygame.mixer.Sound('videos//eat.wav')pygame.mixer.music.load('videos//bgm.mp3') #音樂pygame.mixer.music.play(-1, 0.0)music_Playing = Truewhile True: for event in pygame.event.get(): #關閉按鈕 if event.type == QUIT: pygame.quit() sys.exit()#按鍵 if event.type == KEYDOWN: if event.key == K_LEFT or event.key == ord('a'): move_Right = False move_Left = True if event.key == K_RIGHT or event.key == ord('d'): move_Left = False move_Right = True if event.key == K_UP or event.key == ord('w'): move_Down = False move_Up = True if event.key == K_DOWN or event.key == ord('s'): move_Up = False move_Down = True#松開 if event.type == KEYUP: if event.key == K_ESCAPE: pygame.quit() sys.exit() if event.key == K_LEFT or event.key == ord('a'): move_Left = False if event.key == K_RIGHT or event.key == ord('d'): move_Right = False if event.key == K_UP or event.key == ord('w'): move_Up = False if event.key == K_DOWN or event.key == ord('s'): move_Down = False if event.key == ord('x'): player.top = random.randint(0, window_Height - player.height) player.left = random.randint(0, window_Width - player.width) if event.key == ord('m'): if music_Playing: pygame.mixer.music.stop() else: pygame.mixer.music.play(-1, 0.0) music_Playing = not music_Playing #鼠標增加 if event.type == MOUSEBUTTONUP: foods.append(pygame.Rect(random.randint(0, window_Width - food_Size), random.randint(0, window_Height - food_Size), food_Size,food_Size))#食物增加 food_Counter += 1 if food_Counter >= new_Food: food_Counter = 0 foods.append(pygame.Rect(random.randint(0, window_Width - food_Size), random.randint(0, window_Height - food_Size), food_Size, food_Size)) # 填充 window_Surface.fill(BLACK) # 阻止越界 if move_Down and player.bottom < window_Height: player.top += move_Speed if move_Up and player.top > 0: player.top -= move_Speed if move_Left and player.left > 0: player.left -= move_Speed if move_Right and player.right < window_Width: player.left += move_Speed window_Surface.blit(player_Stretched_Image, player) for food in foods[:]: if player.colliderect(food): foods.remove(food) player = pygame.Rect(player.left, player.top, player.width + 2, player.height + 2) player_Stretched_Image = pygame.transform.scale(player_Image, (player.width, player.height)) if music_Playing: pickup_Sound.play() if len(foods) >= 40: foods = [] if len(foods) == 0: for i in range(20): foods.append(pygame.Rect(random.randint(0, window_Width - 20), random.randint(0, window_Height - 20), food_Size, food_Size))#勝利條件 if player.height > window_Height: player.width = player.height = 1 basic_Font = pygame.font.SysFont(None, 48) text = basic_Font.render('You won!', True, BLACK, BLUE) text_Rect = text.get_rect() window_Surface.blit(text, text_Rect) window_Surface.blit(player_Stretched_Image, player) pygame.display.update() time.sleep(3) window_Surface.blit(player_Stretched_Image, player) pygame.display.update() for food in foods: window_Surface.blit(food_Image, food) pygame.display.update() main_Clock.tick(40)

別攔我,我要去看源碼,哈哈哈,interesting。:)


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 馆陶县| 金华市| 遂溪县| 滦南县| 五原县| 桂阳县| 鄯善县| 忻城县| 凉山| 中西区| 贺兰县| 克拉玛依市| 徐州市| 茂名市| 望都县| 会宁县| 康乐县| 五大连池市| 山东省| 布尔津县| 永仁县| 海口市| 临潭县| 中山市| 兴义市| 正蓝旗| 东台市| 松滋市| 尼木县| 财经| 龙岩市| 宣化县| 乡宁县| 雅安市| 封开县| 乌兰浩特市| 宁明县| 乐昌市| 高雄县| 历史| 乌什县|