日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

pygame写游戏,常用代码记录

發布時間:2023/11/29 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 pygame写游戏,常用代码记录 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

pygame 寫起游戲來還是挺不錯的,不過我也沒用過別的什么東西寫,所以也沒什么發言權。 些游戲我是從這篇文章開始入門的13歲天才兒童教你寫游戲

下面是一些常用的代碼片段,記錄下來,給別人看,也用來給我想不起來的時候看看。

pygame的常見開頭

# 1 - Import library import math import random import pygame from pygame.locals import *# 2 - Initialize the game pygame.init() pygame.mixer.init() # music initial width, height = 640, 480 screen=pygame.display.set_mode((width, height))

加載圖片和聲音

player = pygame.image.load("resources/images/dude.png") screen.blit(player, (100, 100)) hit = pygame.mixer.Sound("resources/audio/explode.wav") hit.set_volume(0.05)

碰撞檢測

bullrect=pygame.Rect(arrow.get_rect()) bullrect.left=bullet_x bullrect.top=bullet_ybadrect = pygame.Rect(badguyimg.get_rect()) badrect.left = badguy_x badrect.top = badguy_y if badrect.colliderect(bullrect):print 'Shooted'

常見事件循環

while True:for event in pygame.event.get():if event.type == pygame.QUIT:pygame.quit()exit(0)if event.type == pygame.KEYDOWN:if event.key == K_q:pygame.quit()exit(0)# refresh screen pygame.display.flip()

顯示文字

pygame.font.init() font = pygame.font.Font(None, 24) text = font.render("Good job", True, (255,0,0)) textRect = text.get_rect() textRect.centerx = screen.get_rect().centerx textRect.centery = screen.get_rect().centery+24 screen.blit(gameover, (0,0)) screen.blit(text, textRect)

轉載于:https://my.oschina.net/goskyblue/blog/387389

總結

以上是生活随笔為你收集整理的pygame写游戏,常用代码记录的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。