pygame星空接宝石鼠标移动游戏

"""有个大学生叫我帮他做的一个pygame小游戏,操作一个像头盔一样的角色以去接宝石,不小心接到灭霸游戏就结束了。

太空接宝石,宝石是画的各种颜色的圆圈。敌人用的是灭霸的图像。用鼠标操作一个铠甲样的图形去接宝石。
接到宝石加10分,接到灭霸游戏结束。
"""

import pygame
from pygame.locals import *
from random import randint

class Gemstone(pygame.sprite.Sprite):
    
    def __init__(self,screen,group):
        """
           实始化, radius“半径“,screen:所在屏幕,group:所在组
        """        
        pygame.sprite.Sprite.__init__(self)    # 初始化父类
        self.sw = screen.get_width()           # 屏幕宽席
        self.sh = screen.get_height()          # 屏幕高度
        radius = randint(10,20)
        self.image = pygame.Surface((radius*2,radius*2)) # 新建surface,代表它的图形
        self.image.set_colorkey((0,0,0))
        color = randint(0,255),randint(0,255),randint(0,255) #  随机RGB颜色
        pygame.draw.circle(self.image,color,(radius,radius),radius)
         
        self.rect = self.image.get_rect()      # 获取矩形对象
        position = randint(0,self.sw),randint(-200,-50)
        self.rect.center = position            # 定矩形的位置
        self.dx = 0                            # 水平方向单位位移
        self.dy = randint(9,15)                # 垂直方向单位位移 
        group.add(self)       

    def update(self):
        """更新坐标"""
        self.rect.move_ip(self.dx,self.dy)     # 移动矩形
        self.bottom_detect()                   # 碰到底检测
        
    def bottom_detect(self):
        """碰到边缘就反弹"""
        if self.rect.top>self.sh : self.kill()

class Enemy(pygame.sprite.Sprite):
    
    def __init__(self,image,screen,group):
        """
           实始化, image:图像,screen:所在屏幕,group:所在组
        """        
        pygame.sprite.Sprite.__init__(self)    # 初始化父类
        self.sw = screen.get_width()           # 屏幕宽席
        self.sh = screen.get_height()          # 屏幕高度        
        self.image = image                     # 代表它的图形          
        self.rect = self.image.get_rect()      # 获取矩形对象
        position = randint(0,self.sw),randint(-200,-50)
        self.rect.center = position            # 定矩形的位置
        self.dx = 0                            # 水平方向单位位移
        self.dy = randint(15,20)               # 垂直方向单位位移 
        group.add(self)       

    def update(self):
        """更新坐标"""
        self.rect.move_ip(self.dx,self.dy)     # 移动矩形
        self.bottom_detect()                   # 碰到底检测
        
    def bottom_detect(self):
        """碰到边缘就反弹"""
        if self.rect.top>self.sh : self.kill()

本文隐藏内容 登陆 后才可以浏览
def main(): """主要函数""" score = 0 # 得分 frame_counter = 0 width,height = 843,463 background = "星空背景.png" mieba_image = "灭霸x.png" player_image = "player.png" pygame.init() screen = pygame.display.set_mode((width,height)) pygame.display.set_caption("太空接宝石by李兴球") background = pygame.image.load(background) mieba_image = pygame.image.load(mieba_image) player_image = pygame.image.load(player_image) stone_group = pygame.sprite.Group() # 创建宝石组 enemy_group = pygame.sprite.Group() # 创建敌人组 player = Player(player_image,screen) fnt = pygame.font.Font("msyh.ttf",28) score_image = fnt.render("当前得分:" + str(score),True,(250,250,250) ) game_over_image = fnt.render("游戏结束!",True,(250,250,250) ) clock = pygame.time.Clock() # 时钟对象 fps = 30 running = True # 控制循环的逻辑变量 game_over = False while running : # 以下三句是不定时产生宝石或敌人 frame_counter += 1 if frame_counter % randint(20,40) == 0 : Gemstone(screen,stone_group) if frame_counter % randint(50,100) == 0 : Enemy(mieba_image,screen,enemy_group) event = pygame.event.poll() # 从事件队列中取一个事件 if event.type == QUIT:running = False # 如果此事件为退出,则逻辑变量为False sp1 = pygame.sprite.spritecollideany(player,stone_group) # 和宝石的碰撞 if sp1: sp1.kill() score += 10 score_image = fnt.render("当前得分:" + str(score),True,(250,250,250)) sp2 = pygame.sprite.spritecollideany(player,enemy_group) # 和敌人的碰撞 if sp2: sp2.kill() game_over = True stone_group.update() # 更新宝石的坐标 enemy_group.update() # 更新敌人的坐标 player.update() screen.blit(background,(0,0)) # 画背景图 if not game_over: stone_group.draw(screen) # 重画所有宝石 enemy_group.draw(screen) # 重画所有敌人 screen.blit(player.image,player.rect) screen.blit(score_image,(width//2-50,40)) else: screen.blit(game_over_image,(width//2-50,50)) fps = 12 pygame.display.update() # 更新屏幕显示 clock.tick(fps) pygame.quit() if __name__ == "__main__": main()

pygame太空接宝石