服务器之家

服务器之家 > 正文

Pygame实战练习之保护单身狗游戏

时间:2022-01-10 00:37     来源/作者:顾木子吖

导语

各位戏精大家好!我是木木子,这个中秋已经结束了,你们都带着对象回家了码?

中秋那几天朋友圈简直是大型秀恩爱现场。

又是一年中秋夜,依旧凭实力单身!呼吁大家记得保护下单身狗啊喂!

Pygame实战练习之保护单身狗游戏

作为一条人畜无害可可爱爱的单身狗

每年的假期几乎都被伤的体无完肤,今年我们不仅要好好生存,还要勇敢报复,让情侣们知道:

单身狗不是这么好欺负的~单身狗们报仇吧!今天带大家写一款贼有趣的单身狗小游戏~

Pygame实战练习之保护单身狗游戏

正文

首先准备好图片素材等:

Pygame实战练习之保护单身狗游戏

bgm音乐必备:

?
1
2
3
4
5
6
import pygame
 
class sound():
    def __init__(self):
        pygame.mixer.music.load('../sound/baab.mp3')
        pygame.mixer.music.play()

​先上效果图嘿嘿嘿 看好哈:

游戏界面——

Pygame实战练习之保护单身狗游戏

程序运行——

Pygame实战练习之保护单身狗游戏

(1)设置滚动的背景:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#地图滚动
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)单身狗不能碰到情侣检测碰撞:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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)主函数:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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()

效果图:游戏规则躲避情侣狗,一直奔跑加分!

Pygame实战练习之保护单身狗游戏

​总结

好啦!单身狗大战即将开始,come on。

哈哈哈哈 这游戏蛮有趣的撒!你们的支持是我最大的动力,记得三连哦~木啊!

Pygame实战练习之保护单身狗游戏

到此这篇关于pygame实战练习之保护单身狗游戏的文章就介绍到这了,更多相关pygame 保护单身狗游戏内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/weixin_55822277/article/details/120413467

标签:

相关文章

热门资讯

蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整 2021-08-24
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
返回顶部