您好,登錄后才能下訂單哦!
引言
這是一個用pygame寫的滑雪者的游戲。
skier從上向下滑,途中會遇到樹和旗子,撿起一個旗子得10分,碰到一顆樹扣100分,可以用左右箭頭控制skier方向。
安裝pygame
pip install pygame
用pip或設(shè)置界面安裝,可自行百度
以下是主界面代碼,每一個類都是一個py文件,需要導(dǎo)包
import pygame import random from settings import Settings from skier import Skier from treeFlag import TreeFlagClass # pygame.display.set_icon() # background_image = pygame.image.load('timg.jpg').convert()#背景圖 # 畫圖 # pygame.draw.circle(window,[0,0,0],[50,50],50,0) my_set = Settings() # 用的是設(shè)置類 系統(tǒng)外觀設(shè)置 skier_images = ['./skier_crash.png', './skier_down.png', './skier_left1.png', './skier_left2.png', './skier_right1.png', './skier_right2.png', ] #滑雪者的圖 if __name__ == '__main__': # 初始化 pygame.init() pygame.mixer.init() # 游戲窗口初始化 window = pygame.display.set_mode((my_set.screen_width, my_set.screen_height)) # 窗口大小 screen = pygame.display.get_surface() pygame.display.set_caption(my_set.name) # 窗口名字 clock = pygame.time.Clock() # 幀率顯示,先定義一個時間對象 treeFlag_sprites = pygame.sprite.RenderUpdates() # 創(chuàng)建sprite容器 樹 # 添加樹或旗子創(chuàng)建自定義事件 AddEnemy = pygame.USEREVENT + 1 pygame.time.set_timer(AddEnemy, 20) # 音樂 # pygame.mixer.muisc.load('bg_music.mp3') skier = Skier(1) # 創(chuàng)建滑雪小人 # 左上角計算分?jǐn)?shù) # countObj = pygame.font.SysFont('方正蘭亭超細(xì)黑簡體',30) countObj = pygame.font.Font(None, 60) # countObj.set_bold(True) #加粗 print(pygame.font.get_fonts()) textObj = countObj.render('SCORE:0', True, (255, 0, 0)) textRectObj = textObj.get_rect() # 這個是計算分?jǐn)?shù) count_num = 0 # 計算碰撞次數(shù),10次場景加一次速度,最多疊加10次 hit_count = 0 # 顯示當(dāng)前速度 speedObj = pygame.font.Font(None, 40) speed_text = speedObj.render('SPEED:1', True, (255, 0, 0)) speedRectObj = speed_text.get_rect() speedRectObj.top = 60 #運(yùn)行游戲 while True: # 先加載背景圖 clock.tick(60) window.fill(my_set.bg_color) # 填充,參數(shù)填寫的是rgb值 # window.blit(background_image,(0,0)) ret = pygame.event.get() # 事件 # 退出游戲判斷 for obj in ret: if obj.type == pygame.QUIT: print('關(guān)閉窗口') exit() # 獲取鍵盤狀態(tài) pressed_keys = pygame.key.get_pressed() #調(diào)用方法更新 skier.update(pressed_keys) #判斷事件,生成場景 for event in pygame.event.get(): if event.type == AddEnemy: if random.randint(1,10) %2: treeFlag_sprites.add(TreeFlagClass(1,1)) else: treeFlag_sprites.add(TreeFlagClass(1,0)) #判斷與樹或者旗子碰撞 hit = pygame.sprite.spritecollide(skier, treeFlag_sprites, False) if hit: if hit[0].type == 'tree': count_num -= 100 hit_count += 1 hit[0].kill() skier.image = pygame.image.load(skier_images[0]).convert() # skier.image = pygame.image.load(skier_images[1]).convert() else: count_num += 10 hit_count += 1 hit[0].kill() #判斷累計碰撞次數(shù): if hit_count <50 and hit_count > 10: hit_num = hit_count//10 for obj in treeFlag_sprites: obj.speed = hit_num skier.speed = hit_num elif hit_count >= 50: for obj in treeFlag_sprites: obj.speed = 5 skier.speed = 5 #場景動畫更新 treeFlag_sprites.update() tree_updates = treeFlag_sprites.draw(window) pygame.display.update(tree_updates) #添加畫面以及幀率 window.blit(skier.image, skier.rect) # 添加小人畫面 textObj = countObj.render('SCORE:%d' % count_num, False, (255, 0, 0)) #顯示得分內(nèi)容 textRectObj = textObj.get_rect() window.blit(textObj, textRectObj) #這是得分 speed_text = speedObj.render('SPEED:%d'%skier.speed, True, (255, 0, 0)) #顯示速度內(nèi)容 speedRectObj = speed_text.get_rect() speedRectObj.top = 60 window.blit(speed_text,speedRectObj) #顯示速度 pygame.display.update() # 必須要更新顯示的內(nèi)容
以下是滑雪者類代碼
# 定義一個雪人類 import pygame from settings import Settings my_set = Settings() skier_images = ['./skier_crash.png', './skier_down.png', './skier_left1.png', './skier_left2.png', './skier_right1.png', './skier_right2.png', ] down_image = pygame.image.load(skier_images[1]) # 雪人初始位置 begin_top = 0 begin_left = my_set.screen_width / 2 - down_image.get_width() / 2 class Skier(pygame.sprite.Sprite): def __init__(self,speed): pygame.sprite.Sprite.__init__(self) ''' :param image:指的是圖片路徑 :param rect:位置 :param down_speed:是往下的速度 ''' self.image = pygame.image.load(skier_images[1]) self.rect = self.image.get_rect() self.rect.top = begin_top # 雪人初始位置 self.rect.left = begin_left # 移動速度 self.speed = speed #場景生成時間 也就是速度 self.time_set = 40 #根據(jù)按鍵事件來移動 def update(self, pressed_keys): # 按鍵事件 if pressed_keys[pygame.K_LEFT] or pressed_keys[pygame.K_a]: if self.rect.left > 0: self.image = pygame.image.load(skier_images[2]).convert() self.rect.left -= self.speed elif pressed_keys[pygame.K_RIGHT] or pressed_keys[pygame.K_d]: if self.rect.left < my_set.screen_width - self.image.get_width(): self.image = pygame.image.load(skier_images[4]).convert() self.rect.left += self.speed if pressed_keys[pygame.K_UP] or pressed_keys[pygame.K_w]: if self.rect.top > 0 : self.image = pygame.image.load(skier_images[1]).convert() self.rect.top -= self.speed elif pressed_keys[pygame.K_DOWN] or pressed_keys[pygame.K_s]: if self.rect.top < my_set.screen_height - self.image.get_height(): self.image = pygame.image.load(skier_images[1]).convert() self.rect.top += self.speed if not (pressed_keys[pygame.K_LEFT] or pressed_keys[pygame.K_a] or pressed_keys[pygame.K_RIGHT] or pressed_keys[pygame.K_d] or pressed_keys[pygame.K_UP] or pressed_keys[pygame.K_w] or pressed_keys[pygame.K_DOWN] or pressed_keys[pygame.K_s]): self.image = pygame.image.load(skier_images[1]).convert()
樹和旗子類
import pygame from random import randrange from settings import Settings my_set = Settings() # 定義一個場景類 class TreeFlagClass(pygame.sprite.Sprite): def __init__(self,speed,treeFlag): #傳入兩個參數(shù),初始速度和bool值樹或旗子 pygame.sprite.Sprite.__init__(self) #父類的初始化方法要調(diào)用 ''' :param image =圖片 :param type 判斷是樹還是旗子 :param speed 速度 ''' if treeFlag: self.image = pygame.image.load('./skier_tree.png') self.type = 'tree' else: self.image = pygame.image.load('./skier_flag.png') self.type = 'flag' self.rect = self.image.get_rect() self.rect.top = self.rect.height + my_set.screen_height self.rect.centerx = randrange(my_set.screen_width - self.rect.width) + self.rect.width / 2 self.speed = speed def update(self, *args): self.rect.top -= self.speed if self.rect.top < -self.rect.height: self.kill()
設(shè)置類
class Settings(object): def __init__(self): #這是一個設(shè)置類,用來管理游戲的外觀 self.screen_width = 600 self.screen_height = 800 self.bg_color = [255,255,255] self.name = 'skier'
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。