如何利用python来制作保护单身狗游戏

发布时间:2021-09-24 17:51:25 作者:柒染
来源:亿速云 阅读:150

本篇文章为大家展示了如何利用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来制作保护单身狗游戏,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. Luck Club-SDT超级单身狗 区块链游戏
  2. phpwamp单身狗模式的详解与分析,单身狗模式/即霸体模式的作用讲解。

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python

上一篇:如何实现美国Bandwagon搬瓦工VPS支付宝购买及续费

下一篇:python中数据类型的示例分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》