Python之游戏

这篇博客介绍了两个Python游戏的开发,包括'摩擦,摩擦'和'飞机大战'。提供了素材提取链接以及相关代码文件如main.py, bullet.py, enemy.py, myplane.py和supply.py。" 135029348,21450849,Oracle数据库配置与管理指南,"['数据库', 'Oracle', '数据库配置']
摘要由CSDN通过智能技术生成

1.摩擦,摩擦

素材提取:
https://pan.baidu.com/s/1OTwscnbiU6umX0DluooJuw
密码:w6ng

import pygame
import sys
from math import *
from pygame.locals import *
from random import *

class Ball(pygame.sprite.Sprite):
    # 初始化动画精灵
    def __init__(self,grayball_image,greenball_image,position,speed,bg_size,target):
        pygame.sprite.Sprite.__init__(self)

        self.grayball_image = pygame.image.load(grayball_image).convert_alpha()
        self.greenball_image = pygame.image.load(greenball_image).convert_alpha()
        self.rect = self.grayball_image.get_rect()
        # 将小球放在指定位置
        self.rect.left , self.rect.top = position
        self.speed = speed
        self.width , self.height = bg_size[0], bg_size[1]
        self.radius = self.rect.width / 2
        # 为每个小球设定一个目标
        self.target = target
        # 添加一个控制变量记录当前小球是否被控
        self.control = False

     # 小球自由移动函数
    def move(self):
        self.rect = self.rect.move(self.speed)

        # 如果小球左侧出了边界,将小球左侧边界改为屏幕右侧边界
        # 这样便实现左进右出的效果
        if self.rect.right <= 0:
            self.rect.left = self.width
        elif self.rect.left >= self.width:
            self.rect.right = 0
        elif self.rect.bottom <= 0:
            self.rect.top = self.height
        elif self.rect.top >= self.height:
            self.rect.bottom = 0

    # check()方法用于判断鼠标一分钟产生的事件数量是否匹配此目标
    def check(self,motion):
        if self.target < motion < self.target + 5:
            return True
        else:
            return False

#定义玻璃面板类
class Glass(pygame.sprite.Sprite):
    def __init__(self, glass_image, mouse_image, bg_size):
        # 初始化动画精灵
        pygame.sprite.Sprite.__init__(self)

        self.glass_image = pygame.image.load(glass_image).convert_alpha()
        self.glass_rect = self.glass_image.get_rect()
        self.glass_rect.left , self.glass_rect.top = \
                                    (bg_size[0] - self.glass_rect.width) // 2, \
                                    bg_size[1] - self.glass_rect.height
        self.mouse_image = pygame.image.load(mouse_image).convert_alpha()
        self.mouse_rect = self.mouse_image.get_rect()
        self.mouse_rect.left , self.mouse_rect.top = \
                                    self.glass_rect.left, self.glass_rect.top
        # 使原来的鼠标不可见
        pygame.mouse.set_visible(False)

# 定义主函数
def main():
    pygame.init()

    grayball_image = "gray_ball.png"
    greenball_image = "green_ball.png"
    bg_image = "background.png"
    glass_image = "glass.png"
    mouse_image = "hand.png"

    running = True

    # 添加魔性的背景音乐
    pygame.mixer.music.load("bg_music.ogg")
    pygame.mixer.music.play()

    # 音乐播放完毕时游戏结束
    GAMEOVER = USEREVENT
    pygame.mixer.music.set_endevent(GAMEOVER)

    # 添加音效
    loser_sound = pygame.mixer.Sound("loser.wav")
    laugh_sound = pygame.mixer.Sound("laugh.wav")
    winner_sound = pygame.mixer.Sound("winner.wav")
    hole_sound = pygame.mixer.Sound("hole.wav")

    # 根据图片尺寸设置游戏界面边界
    bg_size = width , height = 1024, 681
    screen = pygame.display.set_mode(bg_size)
    pygame.display.set_caption("Play the ball")

    background = pygame.image.load(bg_image).convert_alpha()

    # 黑洞的位置
    hole = [(115,121,197,203), (223,229,388,394),(501,507,318,324), (696,702,190,196),(904,910,417,423)]
    # hole = [(117, 119, 199, 201), (225, 227, 390, 392),(503, 505, 320, 322), (698, 700, 192, 194), (906, 908, 419, 421)]

    # 用于存放小球的列表
    balls = []
    group = pygame.sprite.Group()
    # 创建5个小球
    ball_num = 5
    for i in range(ball_num):
        # 位置和速度都是随机的
        position = randint(0,width-100) ,randint(0,height-100)
        speed = [randint(-10,10),randint(-10,10)]
        # level用于改变游戏难度
        level = 3 * (i+1)
        ball = Ball(grayball_image, greenball_image, position, speed, bg_size, level)
        while pygame.sprite.spritecollide(ball, group, False, pygame.sprite.collide_circle):
            ball.rect.left , ball.rect.top = randint(0,width-100) ,randint(0,height-100)
        balls.append(ball)
        group.add(ball)

    # 创建屏幕下方的玻璃面板
    glass = Glass(glass_image,mouse_image,bg_size)

    # 创建一个motion用于记录鼠标一分钟产生的事件数量
    motion = 0

    # 添加自定义事件
    MYTIMER = USEREVENT + 1
    pygame.time.set_timer(MYTIMER, 1000)

    # 持续按键效果
    pygame.key.set_repeat(100,100)

    clock = pygame.time.Clock()

    while running:
        for event in pygame.event.get():
            if event.type == QUIT:
                sys.exit()
            elif event.type == GAMEOVER:
                loser_sound.play()
                pygame.time.delay(2000)
                laugh_sound.play()
                pygame.time.delay(4000)
                running = False
            elif event.type == MYTIMER:
                if motion:
                    for each in group:
                        if each.check(motion):
                            each.speed = [0,0]
                            each.control = True
                    motion = 0
            elif event.type == MOUSEMOTION:
                motion +=1
            elif event.type == KEYDOWN:
                if event.key == K_w:
                    for each in group:
                        if each.control:
                            each.speed[1] -= 1
                if event.key == K_s:
                    for each in group:
                        if each.control:
                            each.speed[1] += 1
                if event.key == K_a:
                    for each in group:
                        if each.control:
                            each.speed[0] -= 1
                if event.key == K_d:
                    for each in group:
                        if each.control:
                            each.speed[0] += 1
                if event.key == K_SPACE:
                    for each in group:
                        if each.control:
                            for i in hole:
                                if i[0] <= each.rect.left <= i[1] and i[2] <= each.rect.top <= i[3]:
                                    hole_sound.play()
                                    each.speed = [0,0]
                                    group.remove(each)
                                    temp = balls.pop(balls.index(each))
                                    balls.insert(0,temp)
                                    hole.remove(i)
                        if not hole:
                            pygame.mixer.music.stop()
                            winner_sound.play()
                            pygame.time.delay(3000)
                            sys.exit()

        # 绘制背景和摩擦面板
        screen.blit(background,(0,0))
        screen.blit(glass.glass_image, glass.glass_rect)

        # 将鼠标显示在摩擦面板内
        glass.mouse_rect.left ,glass.mouse_rect.top = pygame.mouse.get_pos()
        if
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

唱戏先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值