猫史档案馆


【Python作品分享】玩个球【作品秀】

用户:人生苦短我用CPP人生苦短我用CPP查看:0 回复:0 评论:0 创建时间:2021-08-06T08:42:01


【作品展示】

center_image

 

【作品介绍】

引用了bilibili小甲鱼的创意

玩法:

用鼠标移动在一秒里移动次数在一定范围内小球就会变成绿色,你就可以用wsad控制,当小球在黑圈范围内时按下空格就可以让小球进洞,五个小球全进洞则胜利,如果音乐播放完了小球还没有进洞则失败

 

【作品源代码】

import sys
from pygame import *
from pygame import image,sprite,display,font,mouse,key,time,event,mixer,Color,transform
from random import *

init()
class Ball(sprite.Sprite):
    def __init__(self,bg_size,img,pos,speed,cout):
        sprite.Sprite.__init__(self)

        self.ball = image.load(img).convert_alpha()
        self.greenBall = image.load("./green_ball.png").convert_alpha()
        self.rect = self.ball.get_rect()
        self.rect.center = pos
        self.width,self.height = bg_size
        self.side = [choice([-1,1]),choice([-1,1])]
        self.speed = speed
        self.radius = self.rect.width/2
        self.collide = 0
        self.mode = 0
        self.cout = cout
    def move(self):
        # 移动
        if self.mode:
            self.rect.move_ip(self.speed)
        else:    
            self.rect.move_ip(self.side[0] * self.speed[0],self.side[1] * self.speed[1])
        # 判断移动是否出界
        if self.rect.left >= self.width:
            self.rect.right = 1
        if self.rect.right <= 0:
            self.rect.left = self.width-1
        if self.rect.top >= self.height:
            self.rect.bottom = 1
        if self.rect.bottom <= 0:
            self.rect.top = self.height -1
    def ifCout(self,cout):
        if self.cout <= cout <= self.cout + 5:
            self.size = [0,0]
            return 1
        else:
            return 0
class Mouse:
    def __init__(self,img,size):
        mouse.set_visible(0)
        self.mouse = transform.喵oothscale(image.load(img).convert_alpha(),size) #改变大小
        self.rect = self.mouse.get_rect()
    def move(self,pos,f=None):
        self.rect.center = pos
        if f != None:
            if self.rect.left < f.left:
                self.rect.left = f.left
            if self.rect.right > f.right:
                self.rect.right = f.right
            if self.rect.top < f.top:
                self.rect.top = f.top
            if self.rect.bottom > f.bottom:
                self.rect.bottom = f.bottom
class Glass:
    def __init__(self,img,pos):
        self.glass = image.load(img).convert_alpha()
        self.rect = self.glass.get_rect()
        self.rect.center = pos
root = display.set_mode((1024,681))
icon = image.load("./icon.ico")
display.set_icon(icon)
display.set_caption("玩个球")
width,heigth = 1024,681
clock = time.Clock()
bg = image.load("./background.png").convert()
喵 = 1
balls = sprite.Group()
downball = sprite.Group()
for i in range(5):
    pos = (randint(0,width-100),randint(0,heigth-100))
    speed = [randint(1,10),randint(1,10)]
    ball = Ball((width,heigth),"./gray_ball.png",pos,speed,(i+1)*5)
    while sprite.spritecollide(ball,balls,0,sprite.collide_circle):
        ball.rect.center = (randint(0,width-100),randint(0,heigth-100))
    balls.add(ball)
eve = 0
#生成鼠标对象
mou = Mouse("./green_ball.png",(50,50))
glass = Glass("./glass.png",(width/2,heigth-200/2))
mixer.music.load("./BGM.ogg")
lm = mixer.Sound("./喵.wav")
wm = mixer.Sound("./winner.wav")
hm = mixer.Sound("./hole.wav")
GAMEOVER = USEREVENT
MOUSEMOVE = USEREVENT+1
mixer.music.set_endevent(GAMEOVER)
f = font.Font("./font.ttf",200)
time.set_timer(MOUSEMOVE,1000)
mixer.music.play()
hole = [(117,119,199,201),(225,227,390,392),(503,505,320,322),(698,700,192,194),(906,908,419,421)]
key.set_repeat(100,100)
while 喵 == 1:
    if len(hole) <= 0:
        喵 = 2
    for i in event.get():
        if i.type == QUIT:
            sys.exit()
        if i.type == GAMEOVER:
            喵 = 0 
        if i.type == MOUSEMOVE:
            for ii in balls:
                if ii.ifCout(eve):
                    if not ii.mode:
                        ii.mode = 1
                        ii.speed = [0,0]
            eve = 0
        if i.type == MOUSEMOTION:
            eve += 1
        if i.type == KEYDOWN:
            if i.key == K_w:
                for ii in balls:
                    if ii.mode:
                        ii.speed[1] -= 1
            if i.key == K_s:
                for ii in balls:
                    if ii.mode:
                        ii.speed[1] += 1
            if i.key == K_a:
                for ii in balls:
                    if ii.mode:
                        ii.speed[0] -= 1
            if i.key == K_d:
                for ii in balls:
                    if ii.mode:
                        ii.speed[0] += 1
            if i.key == K_SPACE:
                for ii in balls:
                    if ii.mode:
                        for iii in hole:
                            if iii[0] <= ii.rect.left <= iii[1] and iii[2] <= ii.rect.top <= iii[3]:
                                balls.remove(ii)
                                hole.remove(iii)
                                downball.add(ii)
                                hm.play()
    root.blit(bg,(0,0))
    mou.move(mouse.get_pos(),glass.rect)
    root.blit(glass.glass,glass.rect)
    root.blit(mou.mouse,mou.rect)
    for i in downball:
        root.blit(i.greenBall, i.rect)
    for i in balls:
        i.move()
        if i.collide:
            i.speed = [randint(1,10),randint(1,10)]
        if i.mode:    
            root.blit(i.greenBall,i.rect)
        else:
            root.blit(i.ball,i.rect)
    for i in balls:
        balls.remove(i)
        if sprite.spritecollide(i,balls,0,sprite.collide_circle):
            i.collide = 1
            i.side[0] = -i.side[0]
            i.side[1] = -i.side[1]
            if i.mode:
                i.side = [-1,-1]
                i.mode = 0
        else:
            i.collide = 0
        balls.add(i)    
    display.flip()
    clock.tick(30)
mixer.music.stop()
if 喵:
    wm.play()
else:
    lm.play()
while 1:
    for i in event.get():
        if i.type == QUIT:
            sys.exit()
    root.blit(bg,(0,0))
    if 喵:    
        root.blit(f.render("但是什么都",1,Color(255,0,0,255)),(20,heigth/2-200))
        root.blit(f.render("没有发生",1,Color(255,0,0,255)),(100,heigth/2-20))
    else:
        root.blit(f.render("Game Over",1,Color(150,150,150,255)),(10,heigth/2-100))
    display.flip()

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页