中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何利用python來制作保護單身狗游戲

發布時間:2021-09-24 17:51:25 來源:億速云 閱讀:148 作者:柒染 欄目:開發技術

本篇文章為大家展示了如何利用python來制作保護單身狗游戲,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

正文

首先準備好圖片素材等:

如何利用python來制作保護單身狗游戲

bgm音樂必備:

import pygame
 
class Sound():
    def __init__(self):
        pygame.mixer.music.load('../sound/baab.mp3')
        pygame.mixer.music.play()

先上效果圖嘿嘿嘿 看好哈:

游戲界面——

如何利用python來制作保護單身狗游戲

程序運行——

如何利用python來制作保護單身狗游戲

(1)設置滾動的背景:

#地圖滾動
import pygame
import random
 
class GameBackground(object):
    # 初始化地圖
    def __init__(self, scene):
        # 加載相同張圖片資源,做交替實現地圖滾動
        self.image1 = pygame.image.load("../image/background.png")
        self.image2 = pygame.image.load("../image/background.png")
        # 保存場景對象
        self.main_scene = scene
        # 輔助移動地圖
        self.x1 = 0
        self.x2 = 1280
        self.snowflag=False
 
    # 計算地圖圖片繪制坐標
    def action(self):
        self.x1 = self.x1 - 1
        self.x2 = self.x2 - 1
        if self.x1 <= -1279:
            self.x1 = 1279
        if self.x2 <= -1279:
            self.x2 = 1279
 
    # 繪制地圖的兩張圖片
    def draw(self):
        self.main_scene.blit(self.image1, (self.x1,0))
        self.main_scene.blit(self.image2, (self.x2,0))
 
 
class Snow():
                # 雪花的豎直速度
    def __init__(self):
        self.x = 0  # 雪花的橫坐標
        self.y = 0  # 雪花的縱坐標
        self.vx = 0  # 雪花的水平速度
        self.vy = 0
        self.x = random.randint(0,1280)   # 初始化雪花橫坐標
        self.y = random.randint(0,390)   #初始化雪花縱坐標
    def getsnowpos(self):
        return self.x,self.y         # 返回雪花坐標位置

(2)單身狗不能碰到情侶檢測碰撞:

import pygame
 
 
class Collide():
    def __init__(self):
        self.gamestatus=True
        self.snowflag=False
        self.dogflag=0
        self.score=0
        self.scoreup=200
        self.gameover=False
        self.count=0
 
    def dogs_cpdogs(self,dogs,cpdogs):
        boooooooooooooooool=pygame.sprite.groupcollide(dogs,cpdogs,False,True)
        if boooooooooooooooool:
            self.score += self.scoreup
            print(self.score)
 
    def dog_cpdog(self,dog,cpdog2s):
        booooooool=pygame.sprite.spritecollide(dog,cpdog2s,True)
        if booooooool:
            self.snowflag = True
            self.count += 1
            if self.count>=3:
                self.gameover=True

(3)主函數:

import pygame
import sys,os
import random
from background import GameBackground,Snow
from dog import MySprite
from cpdog import Monster,MySprite2
from pygame.sprite import Group
from collidedetection import Collide
from sound import Sound
from score import Fontf
from crydog import MySpritec
 
 
pygame.init()
screen=pygame.display.set_mode((1280,390))
fpsset=pygame.time.Clock()
bgcolor=(255,240,30)
background=GameBackground(screen)
dog=MySprite() 
dog.load("../image/dog.png", 82, 62, 3)
dog.X = 0
dog.Y = 285
dogs = pygame.sprite.Group()
dogs.add(dog)
cpdogs=Group()
lasttimecreat=0
collidetest=Collide()
sound=Sound()
cpdog2s = pygame.sprite.Group()
snowlist = []
for i in range(0, 50):  # 建立50個雪花
    snow=Snow()
    snowlist.append(snow)
scor=Fontf(screen)
onoff = True
crydog=None
 
 
while 1:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        # player ctrl
        if event.type==pygame.KEYDOWN:
            if event.key==pygame.K_RIGHT:
                dog.movieRight=True
            if event.key == pygame.K_LEFT:
                dog.movieLeft=True
            if event.key==pygame.K_SPACE:
                if not dog.jumping:
                    dog.jumping=True
                    dog.vUP = -14
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_RIGHT:
                dog.movieRight=False
            if event.key==pygame.K_LEFT:
                dog.movieLeft=False
        if event.type==pygame.MOUSEBUTTONDOWN:
            mousex,mousey=pygame.mouse.get_pos()
            if mousex>=540 and mousex<=540 + scor.text_width:
                if mousey>=180 and mousey<=180 + scor.text_height:
                    onoff = False
    if dog.jumping:
        if dog.vUP < 0:
            dog.vUP += 0.6
        elif dog.vUP >= 0:
            dog.vUP += 0.8
        dog.Y += dog.vUP
        if dog.Y >= 290:
            dog.jumping = False
            dog.Y = 290
            dog.vUP = 0.0
    if not collidetest.gameover:
 
        if onoff:
            scor.beginpage()
            print(onoff)
 
        else:
            print(onoff)
        #碰撞檢測
            collidetest.dogs_cpdogs(dogs,cpdogs)
            collidetest.dog_cpdog(dog,cpdog2s)
            #背景
            background.action()
            background.draw()
            #分數
            # scor.beginpage()
            collidetest.score+=1
            scor.displayScore(collidetest.score)
            #時間控制
            ticks = pygame.time.get_ticks()
            #player繪制
            dogs.update(ticks)
            dogs.draw(screen)
 
            cpdog2s.update(ticks)
            cpdog2s.draw(screen)
            if ticks >lasttimecreat  + random.randint(1000,20000):
                 #生成障礙物
                cp = Monster(screen)
                cpdogs.add(cp)
                cpdog2 = MySprite2()
                cpdog2.load("../image/cpdogs.png", 113, 62, 3)
                cpdog2.X = 1280
                cpdog2.Y = 285
                cpdog2s.add(cpdog2)
                lasttimecreat=ticks
 
            for m in cpdogs:
                m.draw_monster()
                if m.rect.x <=640:
                    del m
            cpdogs.update()
        #snow
            if collidetest.snowflag:
                for snow in snowlist:
                    # 每個雪花位置的變換
                    # if random.randint(0,1):
                    snow.vx = random.randint(-3,3)  # 雪花的橫向速度
                    snow.vy = 1                     # 雪花的豎直速度
                    snow.x += snow.vx               # 雪花的橫軸移動位置
                    snow.y += snow.vy               # 雪花的縱軸移動位置
                    if snow.y > 500:
                        snow.y = 0
                    pygame.draw.circle(screen,[255,255,255],snow.getsnowpos(),1)
    else:
        if collidetest.snowflag:
            background.action()
            background.draw()
            for snow in snowlist:
                snow.vx = random.randint(-3, 3)
                snow.vy = 1
                snow.x += snow.vx
                snow.y += snow.vy
                if snow.y > 500:
                    snow.y = 0
                pygame.draw.circle(screen, [255, 255, 255], snow.getsnowpos(), 1)
            if not crydog:
                crydog=MySpritec()
                crydog.load("../image/crydog.png", 82, 62,3)
                crydog.X = 100
                crydog.Y = 285
                crydogs=Group()
                crydogs.add(crydog)
            ticks = pygame.time.get_ticks()
            # player繪制
            crydogs.update(ticks)
            crydogs.draw(screen)
            scor.gameoverrrrrrrrr()
            if crydog.X>1280:
                sys.exit()
    fpsset.tick(60)
    pygame.display.update()

效果圖:游戲規則躲避情侶狗,一直奔跑加分!

如何利用python來制作保護單身狗游戲

好啦!單身狗大戰即將開始,come on。

上述內容就是如何利用python來制作保護單身狗游戲,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

阳山县| 鲁山县| 千阳县| 桃园县| 双辽市| 沧州市| 榆社县| 赣州市| 永丰县| 合江县| 河池市| 金湖县| 连南| 广州市| 乐安县| 南丹县| 宜阳县| 玉环县| 诸暨市| 炎陵县| 嘉善县| 曲麻莱县| 大关县| 宁津县| 铜川市| 蕉岭县| 塔城市| 安庆市| 金川县| 荃湾区| 漯河市| 三台县| 马边| 三河市| 余江县| 凤山县| 团风县| 江孜县| 西安市| 新龙县| 山东省|