zoukankan      html  css  js  c++  java
  • 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游戏项目imageslife.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游戏项目imagesme1.png'
        img_path2 = 'D:python file游戏项目imagesme2.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游戏项目soundsgame_music.ogg')
        pygame.mixer.music.play(-1)
    
        # 音效
        mix1 = pygame.mixer.Sound('D:python file游戏项目soundsutton.wav')
        mix2 = pygame.mixer.Sound('D:python file游戏项目soundsullet.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游戏项目imageslife.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()
    

      

  • 相关阅读:
    微信分享链接出现config:invalid signature错误的解决方法
    微信开发,分享部分出现的问题
    thinkphp 3.2 去除调试模式后报错,怎么解决
    MySQL添加新用户、为用户创建数据库、为新用户分配权限
    xshell工具source导入几个G的数据库
    thinkphp5引入公共部分header、footer等
    用样本估计总体
    随机抽样
    平面几何相关定理
    直线和曲线相切,曲线和曲线相切
  • 原文地址:https://www.cnblogs.com/ThestarsinOctober/p/10184915.html
Copyright © 2011-2022 走看看