01Game

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

# 构建精灵类
class MySprite(pygame.sprite.Sprite):
    def __init__(self, imgpath):
        super(MySprite, self).__init__()
        self._image = pygame.image.load(imgpath)
        self._rect = self.image.get_rect()

    @property
    def image(self):
        return self._image

    @property
    def rect(self):
        return self._rect

    def setimg(self, newimg_path):
        self._image = pygame.image.load(newimg_path)


if __name__ == '__main__':
    # 设置图标
    pygame.display.set_icon(pygame.image.load('D:\python file\游戏项目\images\life.png'))
    window = pygame.display.set_mode((400, 600))
    window_rect = window.get_rect()
    print(window_rect.x, window_rect.y, window_rect.topleft, window_rect.right, window_rect.center)
    # 设置title
    pygame.display.set_caption('测试游戏')
    # 创建图片对象
    '''
    image1 = pygame.image.load('./images/me1.png')
    image2 = pygame.image.load('./images/me2.png')
    image=image1
    image_rect = image.get_rect()
    # 设置图片显示位置
    image_rect.midbottom = (window_rect.midbottom[0], window_rect.midbottom[1]-20)
    '''
    img_path1 = 'D:\python file\游戏项目\images\me1.png'
    img_path2 = 'D:\python file\游戏项目\images\me2.png'
    cur_img = img_path1
    plane_sprite1 = MySprite(cur_img)
    plane_sprite1.rect.midbottom = (window_rect.midbottom[0], window_rect.midbottom[1]-20)

    # 设置定时器
    pygame.time.set_timer(USEREVENT, 100)

    # 设置帧率
    clock = pygame.time.Clock()

    # 设置背景音乐
    pygame.mixer.init()
    pygame.mixer.music.load('D:\python file\游戏项目\sounds\game_music.ogg')
    pygame.mixer.music.play(-1)

    # 音效
    mix1 = pygame.mixer.Sound('D:\python file\游戏项目\sounds\button.wav')
    mix2 = pygame.mixer.Sound('D:\python file\游戏项目\sounds\bullet.wav')

    # 构建食物对象
    '''
    food = pygame.image.load('icon.ico')
    food_rect = food.get_rect()
    food_rect.center = window_rect.center
    '''
    # 构建精灵组
    group = pygame.sprite.Group()
    for i in range(10):
        food_sprite = MySprite('D:\python file\游戏项目\images\life.png')
        food_sprite.rect.x = random.randint(32, 369)
        food_sprite.rect.y = random.randint(32, 569)
        group.add(food_sprite)

    while True:
        # 获取所有的事件
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit(0)
            elif event.type == USEREVENT:
                # 切换飞机图片
                if cur_img == img_path1:
                    plane_sprite1.setimg(img_path2)
                else:
                    plane_sprite1.setimg(img_path1)

        # 获取按键
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:
            mix1.play()
            plane_sprite1.rect.x -= 5
        if keys[pygame.K_RIGHT]:
            mix2.play()
            plane_sprite1.rect.x += 5
        if keys[pygame.K_UP]:
            plane_sprite1.rect.y -= 5
        if keys[pygame.K_DOWN]:
            plane_sprite1.rect.y += 5

        # 检测碰撞
        collidelist = pygame.sprite.spritecollide(plane_sprite1, group, True)
        print(collidelist)


        window.fill((255,255,255))
        # 将一个image画到另一个image中
        window.blit(plane_sprite1.image, plane_sprite1.rect)
        for i in group:
            window.blit(i.image, i.rect)

        clock.tick(30)
        # 刷新
        pygame.display.flip()

  

猜你喜欢

转载自www.cnblogs.com/ThestarsinOctober/p/10184915.html