{"id":9087,"date":"2024-05-14T12:01:01","date_gmt":"2024-05-14T04:01:01","guid":{"rendered":""},"modified":"2024-05-14T12:01:01","modified_gmt":"2024-05-14T04:01:01","slug":"python\u753b\u56fe\u4ee3\u7801\u5927\u5168\u753b\u96ea\u4eba,\u7528python\u753b\u96ea\u4eba\u7f16\u7a0b\u4ee3\u7801","status":"publish","type":"post","link":"https:\/\/mushiming.com\/9087.html","title":{"rendered":"python\u753b\u56fe\u4ee3\u7801\u5927\u5168\u753b\u96ea\u4eba,\u7528python\u753b\u96ea\u4eba\u7f16\u7a0b\u4ee3\u7801"},"content":{"rendered":"
\u5927\u5bb6\u597d\uff0c\u5c0f\u7f16\u4e3a\u5927\u5bb6\u89e3\u7b54python\u753b\u56fe\u4ee3\u7801\u5927\u5168\u753b\u96ea\u4eba\u7684\u95ee\u9898\u3002\u5f88\u591a\u4eba\u8fd8\u4e0d\u77e5\u9053\u7528python\u753b\u96ea\u4eba\u7f16\u7a0b\u4ee3\u7801\uff0c\u73b0\u5728\u8ba9\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<\/p>\n
<\/p>\n
\u5f15\u8a00<\/p>\n
\u8fd9\u662f\u4e00\u4e2a\u7528pygame\u5199\u7684\u6ed1\u96ea\u8005\u7684\u6e38\u620f\u3002<\/p>\n
skier\u4ece\u4e0a\u5411\u4e0b\u6ed1\uff0c\u9014\u4e2d\u4f1a\u9047\u5230\u6811\u548c\u65d7\u5b50\uff0c\u6361\u8d77\u4e00\u4e2a\u65d7\u5b50\u5f9710\u5206\uff0c\u78b0\u5230\u4e00\u9897\u6811\u6263100\u5206\uff0c\u53ef\u4ee5\u7528\u5de6\u53f3\u7bad\u5934\u63a7\u5236skier\u65b9\u5411python\u521b\u610f\u3002<\/p>\n<\/p>\n
\u5b89\u88c5pygame<\/p>\n
pip install pygame<\/p>\n
\u7528pip\u6216\u8bbe\u7f6e\u754c\u9762\u5b89\u88c5\uff0c\u53ef\u81ea\u884c\u767e\u5ea6<\/p>\n
\u4ee5\u4e0b\u662f\u4e3b\u754c\u9762\u4ee3\u7801\uff0c\u6bcf\u4e00\u4e2a\u7c7b\u90fd\u662f\u4e00\u4e2apy\u6587\u4ef6\uff0c\u9700\u8981\u5bfc\u5305<\/p>\n
import pygame<\/p>\n
import random<\/p>\n
from settings import Settings<\/p>\n
from skier import Skier<\/p>\n
from treeFlag import TreeFlagClass<\/p>\n
# pygame.display.set_icon()<\/p>\n
# background_image = pygame.image.load('timg.jpg').convert()#\u80cc\u666f\u56fe<\/p>\n
# \u753b\u56fe<\/p>\n
# pygame.draw.circle(window,[0,0,0],[50,50],50,0)<\/p>\n
my_set = Settings() # \u7528\u7684\u662f\u8bbe\u7f6e\u7c7b \u7cfb\u7edf\u5916\u89c2\u8bbe\u7f6e<\/p>\n
skier_images = ['.\/skier_crash.png', '.\/skier_down.png', '.\/skier_left1.png', '.\/skier_left2.png',<\/p>\n
'.\/skier_right1.png', '.\/skier_right2.png', ] #\u6ed1\u96ea\u8005\u7684\u56fe<\/p>\n
if __name__ == '__main__':<\/p>\n
# \u521d\u59cb\u5316<\/p>\n
pygame.init()<\/p>\n
pygame.mixer.init()<\/p>\n
# \u6e38\u620f\u7a97\u53e3\u521d\u59cb\u5316<\/p>\n
window = pygame.display.set_mode((my_set.screen_width, my_set.screen_height)) # \u7a97\u53e3\u5927\u5c0f<\/p>\n
screen = pygame.display.get_surface()<\/p>\n
pygame.display.set_caption(my_set.name) # \u7a97\u53e3\u540d\u5b57<\/p>\n
clock = pygame.time.Clock() # \u5e27\u7387\u663e\u793a,\u5148\u5b9a\u4e49\u4e00\u4e2a\u65f6\u95f4\u5bf9\u8c61<\/p>\n
treeFlag_sprites = pygame.sprite.RenderUpdates() # \u521b\u5efasprite\u5bb9\u5668 \u6811<\/p>\n
# \u6dfb\u52a0\u6811\u6216\u65d7\u5b50\u521b\u5efa\u81ea\u5b9a\u4e49\u4e8b\u4ef6<\/p>\n
AddEnemy = pygame.USEREVENT + 1<\/p>\n
pygame.time.set_timer(AddEnemy, 20)<\/p>\n
# \u97f3\u4e50<\/p>\n
# pygame.mixer.muisc.load('bg_music.mp3')<\/p>\n
skier = Skier(1) # \u521b\u5efa\u6ed1\u96ea\u5c0f\u4eba<\/p>\n
# \u5de6\u4e0a\u89d2\u8ba1\u7b97\u5206\u6570<\/p>\n
# countObj = pygame.font.SysFont('\u65b9\u6b63\u5170\u4ead\u8d85\u7ec6\u9ed1\u7b80\u4f53',30)<\/p>\n
countObj = pygame.font.Font(None, 60)<\/p>\n
# countObj.set_bold(True) #\u52a0\u7c97<\/p>\n
print(pygame.font.get_fonts())<\/p>\n
textObj = countObj.render('SCORE:0', True, (255, 0, 0))<\/p>\n
textRectObj = textObj.get_rect()<\/p>\n
# \u8fd9\u4e2a\u662f\u8ba1\u7b97\u5206\u6570<\/p>\n
count_num = 0<\/p>\n
# \u8ba1\u7b97\u78b0\u649e\u6b21\u6570\uff0c10\u6b21\u573a\u666f\u52a0\u4e00\u6b21\u901f\u5ea6\uff0c\u6700\u591a\u53e0\u52a010\u6b21<\/p>\n
hit_count = 0<\/p>\n
# \u663e\u793a\u5f53\u524d\u901f\u5ea6<\/p>\n
speedObj = pygame.font.Font(None, 40)<\/p>\n
speed_text = speedObj.render('SPEED:1', True, (255, 0, 0))<\/p>\n
speedRectObj = speed_text.get_rect()<\/p>\n
speedRectObj.top = 60<\/p>\n
#\u8fd0\u884c\u6e38\u620f<\/p>\n
while True:<\/p>\n
# \u5148\u52a0\u8f7d\u80cc\u666f\u56fe<\/p>\n
clock.tick(60)<\/p>\n
window.fill(my_set.bg_color) # \u586b\u5145\uff0c\u53c2\u6570\u586b\u5199\u7684\u662frgb\u503c<\/p>\n
# window.blit(background_image,(0,0))<\/p>\n
ret = pygame.event.get() # \u4e8b\u4ef6<\/p>\n
# \u9000\u51fa\u6e38\u620f\u5224\u65ad<\/p>\n
for obj in ret:<\/p>\n
if obj.type == pygame.QUIT:<\/p>\n
print('\u5173\u95ed\u7a97\u53e3')<\/p>\n
exit()<\/p>\n
# \u83b7\u53d6\u952e\u76d8\u72b6\u6001<\/p>\n
pressed_keys = pygame.key.get_pressed()<\/p>\n
#\u8c03\u7528\u65b9\u6cd5\u66f4\u65b0<\/p>\n
skier.update(pressed_keys)<\/p>\n
#\u5224\u65ad\u4e8b\u4ef6\uff0c\u751f\u6210\u573a\u666f<\/p>\n
for event in pygame.event.get():<\/p>\n
if event.type == AddEnemy:<\/p>\n
if random.randint(1,10) %2:<\/p>\n
treeFlag_sprites.add(TreeFlagClass(1,1))<\/p>\n
else:<\/p>\n
treeFlag_sprites.add(TreeFlagClass(1,0))<\/p>\n
#\u5224\u65ad\u4e0e\u6811\u6216\u8005\u65d7\u5b50\u78b0\u649e<\/p>\n
hit = pygame.sprite.spritecollide(skier, treeFlag_sprites, False)<\/p>\n
if hit:<\/p>\n
if hit[0].type == 'tree':<\/p>\n
count_num -= 100<\/p>\n
hit_count += 1<\/p>\n
hit[0].kill()<\/p>\n
skier.image = pygame.image.load(skier_images[0]).convert()<\/p>\n
# skier.image = pygame.image.load(skier_images[1]).convert()<\/p>\n
else:<\/p>\n
count_num += 10<\/p>\n
hit_count += 1<\/p>\n
hit[0].kill()<\/p>\n
#\u5224\u65ad\u7d2f\u8ba1\u78b0\u649e\u6b21\u6570\uff1a<\/p>\n
if hit_count <50 and hit_count > 10:<\/p>\n
hit_num = hit_count\/\/10<\/p>\n
for obj in treeFlag_sprites:<\/p>\n
obj.speed = hit_num<\/p>\n
skier.speed = hit_num<\/p>\n
elif hit_count >= 50:<\/p>\n
for obj in treeFlag_sprites:<\/p>\n
obj.speed = 5<\/p>\n
skier.speed = 5<\/p>\n
#\u573a\u666f\u52a8\u753b\u66f4\u65b0<\/p>\n
treeFlag_sprites.update()<\/p>\n
tree_updates = treeFlag_sprites.draw(window)<\/p>\n
pygame.display.update(tree_updates)<\/p>\n
#\u6dfb\u52a0\u753b\u9762\u4ee5\u53ca\u5e27\u7387<\/p>\n
window.blit(skier.image, skier.rect) # \u6dfb\u52a0\u5c0f\u4eba\u753b\u9762<\/p>\n
textObj = countObj.render('SCORE:%d' % count_num, False, (255, 0, 0)) #\u663e\u793a\u5f97\u5206\u5185\u5bb9<\/p>\n
textRectObj = textObj.get_rect()<\/p>\n
window.blit(textObj, textRectObj) #\u8fd9\u662f\u5f97\u5206<\/p>\n
speed_text = speedObj.render('SPEED:%d'%skier.speed, True, (255, 0, 0)) #\u663e\u793a\u901f\u5ea6\u5185\u5bb9<\/p>\n
speedRectObj = speed_text.get_rect()<\/p>\n
speedRectObj.top = 60<\/p>\n
window.blit(speed_text,speedRectObj) #\u663e\u793a\u901f\u5ea6<\/p>\n
pygame.display.update() # \u5fc5\u987b\u8981\u66f4\u65b0\u663e\u793a\u7684\u5185\u5bb9<\/p>\n
\u4ee5\u4e0b\u662f\u6ed1\u96ea\u8005\u7c7b\u4ee3\u7801<\/p>\n
# \u5b9a\u4e49\u4e00\u4e2a\u96ea\u4eba\u7c7b<\/p>\n
import pygame<\/p>\n
from settings import Settings<\/p>\n
my_set = Settings()<\/p>\n
skier_images = ['.\/skier_crash.png', '.\/skier_down.png', '.\/skier_left1.png', '.\/skier_left2.png',<\/p>\n
'.\/skier_right1.png', '.\/skier_right2.png', ]<\/p>\n
down_image = pygame.image.load(skier_images[1])<\/p>\n
# \u96ea\u4eba\u521d\u59cb\u4f4d\u7f6e<\/p>\n
begin_top = 0<\/p>\n
begin_left = my_set.screen_width \/ 2 - down_image.get_width() \/ 2<\/p>\n
class Skier(pygame.sprite.Sprite):<\/p>\n
def __init__(self,speed):<\/p>\n
pygame.sprite.Sprite.__init__(self)<\/p>\n
'''<\/p>\n
:param image:\u6307\u7684\u662f\u56fe\u7247\u8def\u5f84<\/p>\n
:param rect:\u4f4d\u7f6e<\/p>\n
:param down_speed:\u662f\u5f80\u4e0b\u7684\u901f\u5ea6<\/p>\n
'''<\/p>\n
self.image = pygame.image.load(skier_images[1])<\/p>\n
self.rect = self.image.get_rect()<\/p>\n
self.rect.top = begin_top # \u96ea\u4eba\u521d\u59cb\u4f4d\u7f6e<\/p>\n
self.rect.left = begin_left<\/p>\n
# \u79fb\u52a8\u901f\u5ea6<\/p>\n
self.speed = speed<\/p>\n
#\u573a\u666f\u751f\u6210\u65f6\u95f4 \u4e5f\u5c31\u662f\u901f\u5ea6<\/p>\n
self.time_set = 40<\/p>\n
#\u6839\u636e\u6309\u952e\u4e8b\u4ef6\u6765\u79fb\u52a8<\/p>\n
def update(self, pressed_keys):<\/p>\n
# \u6309\u952e\u4e8b\u4ef6<\/p>\n
if pressed_keys[pygame.K_LEFT] or pressed_keys[pygame.K_a]:<\/p>\n
if self.rect.left > 0:<\/p>\n
self.image = pygame.image.load(skier_images[2]).convert()<\/p>\n
self.rect.left -= self.speed<\/p>\n
elif pressed_keys[pygame.K_RIGHT] or pressed_keys[pygame.K_d]:<\/p>\n
if self.rect.left < my_set.screen_width - self.image.get_width():<\/p>\n
self.image = pygame.image.load(skier_images[4]).convert()<\/p>\n
self.rect.left += self.speed<\/p>\n
if pressed_keys[pygame.K_UP] or pressed_keys[pygame.K_w]:<\/p>\n
if self.rect.top > 0 :<\/p>\n
self.image = pygame.image.load(skier_images[1]).convert()<\/p>\n
self.rect.top -= self.speed<\/p>\n
elif pressed_keys[pygame.K_DOWN] or pressed_keys[pygame.K_s]:<\/p>\n
if self.rect.top < my_set.screen_height - self.image.get_height():<\/p>\n
self.image = pygame.image.load(skier_images[1]).convert()<\/p>\n
self.rect.top += self.speed<\/p>\n
if not (pressed_keys[pygame.K_LEFT] or pressed_keys[pygame.K_a] or pressed_keys[pygame.K_RIGHT] or<\/p>\n
pressed_keys[pygame.K_d] or pressed_keys[pygame.K_UP] or pressed_keys[pygame.K_w] or<\/p>\n
pressed_keys[pygame.K_DOWN] or pressed_keys[pygame.K_s]):<\/p>\n
self.image = pygame.image.load(skier_images[1]).convert()<\/p>\n
\u6811\u548c\u65d7\u5b50\u7c7b<\/p>\n
import pygame<\/p>\n
from random import randrange<\/p>\n
from settings import Settings<\/p>\n
my_set = Settings()<\/p>\n
# \u5b9a\u4e49\u4e00\u4e2a\u573a\u666f\u7c7b<\/p>\n
class TreeFlagClass(pygame.sprite.Sprite):<\/p>\n
def __init__(self,speed,treeFlag): #\u4f20\u5165\u4e24\u4e2a\u53c2\u6570\uff0c\u521d\u59cb\u901f\u5ea6\u548cbool\u503c\u6811\u6216\u65d7\u5b50<\/p>\n
pygame.sprite.Sprite.__init__(self) #\u7236\u7c7b\u7684\u521d\u59cb\u5316\u65b9\u6cd5\u8981\u8c03\u7528<\/p>\n
'''<\/p>\n
:param image =\u56fe\u7247<\/p>\n
:param type \u5224\u65ad\u662f\u6811\u8fd8\u662f\u65d7\u5b50<\/p>\n
:param speed \u901f\u5ea6<\/p>\n
'''<\/p>\n
if treeFlag:<\/p>\n
self.image = pygame.image.load('.\/skier_tree.png')<\/p>\n
self.type = 'tree'<\/p>\n
else:<\/p>\n
self.image = pygame.image.load('.\/skier_flag.png')<\/p>\n
self.type = 'flag'<\/p>\n
self.rect = self.image.get_rect()<\/p>\n
self.rect.top = self.rect.height + my_set.screen_height<\/p>\n
self.rect.centerx = randrange(my_set.screen_width - self.rect.width) + self.rect.width \/ 2<\/p>\n
self.speed = speed<\/p>\n
def update(self, *args):<\/p>\n
self.rect.top -= self.speed<\/p>\n
if self.rect.top < -self.rect.height:<\/p>\n
self.kill()<\/p>\n
\u8bbe\u7f6e\u7c7b<\/p>\n
class Settings(object):<\/p>\n
def __init__(self):<\/p>\n
#\u8fd9\u662f\u4e00\u4e2a\u8bbe\u7f6e\u7c7b\uff0c\u7528\u6765\u7ba1\u7406\u6e38\u620f\u7684\u5916\u89c2<\/p>\n
self.screen_width = 600<\/p>\n
self.screen_height = 800<\/p>\n
self.bg_color = [255,255,255]<\/p>\n
self.name = 'skier'<\/p>\n
\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff0c\u4e5f\u5e0c\u671b\u5927\u5bb6\u591a\u591a\u652f\u6301\u811a\u672c\u4e4b\u5bb6\u3002<\/p>\n<\/p><\/div>\n