最顶层的游戏框架设计代码_碰彩球游戏

python pygame success shell
python pygame success shell


下面是部分代码预览:

"""
   最顶层的游戏框架设计代码,碰彩球游戏
"""
from ball import *
from cover import *

def start_game(screen):
    """
       screen是屏幕对象
       本函数生成游戏中的各个对象,进入游戏循环,
       返回游戏成功或失败的逻辑值
    """
    
def display(screen,image):
    """
       本函数只是显示一个image,
       直到按窗口关闭按钮才会退出Pygame
    """
    image = pygame.image.load(image)
    screen.blit(image,(0,0))    
    while not pygame.event.get(QUIT):pass
    pygame.quit()

def main():

    cover_image = "images/gingerbread.png"
    screen = display_cover(cover_image)

    if screen == None: return
    
    success = start_game(screen)
    if success:
        image = "images/success.png"
    else:
        image = "image/fail.png"

    display(screen,image)    
        
if __name__ == "__main__":

    main()
        

下面是Ball模块的代码,和cover模块的代码:
如需要查看完整源代码,请扫码付款:

VIP免费查看

已有0人支付

"""
   碰彩球游戏的Ball类设计预备代码,小球别跑的ball类代码.
"""

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

class Ball(pygame.sprite.Sprite):
    counter = 0                    # 统计碰到鼠标指针的球的数量
    def __init__(self,image,group,screen):
        """
           image:球的图形,group:所在的组,screen:所在的屏幕
        """
        pygame.sprite.Sprite.__init__(self)# 初始化父类
        self.screen_rect = screen.get_rect() # 得到屏幕的矩形对象
        self.sw = screen.get_width()       # 获取屏幕宽度
        self.sh = screen.get_height()      # 获取屏幕高度
        self.image = image        
        self.rect = self.image.get_rect()  # 获取矩形对象
        self.rect.center = self.sw//2,self.sh//2
        self.speed = [randint(-4,4),randint(-4,4)]
        self.add(group)
        
    def update(self):
        """根据speed更新矩形对象"""
        self.rect.move_ip(self.speed)      # 移动矩形对象
        mpos = pygame.mouse.get_pos()      # 获取鼠标坐标
        if self.rect.collidepoint(mpos):   # 如果碰到点 
            self.kill()
            Ball.counter += 1 
        outside = not self.screen_rect.contains(self.rect)
        if outside :
            self.kill()           # 如果出去了,就自杀
            # print("游戏失败!")

def main():
    """主要函数"""
    size = 480,360
    screen = pygame.display.set_mode(size)
    
    images = [f'images/{i}.png' for i in range(5)]
    images = [pygame.image.load(image) for image in images]
    images = [pygame.transform.scale(image,(20,20)) for image in images]
    
    balls = pygame.sprite.Group()     
    
    clock = pygame.time.Clock()
    running = True
    while running:
        if randint(1,10) ==1 :
            Ball(choice(images),balls,screen)
        for event in pygame.event.get():
            if event.type == QUIT:running=False
        balls.update()
        if Ball.counter == 10 :        # 游戏成功结束
            print("胜利")
            running = False
        screen.fill((23,234,31))       # 填充背景
        balls.draw(screen)             # 画所有小球
        pygame.display.update()        # 显示刷新
        clock.tick(30)                 # 设定fps
    pygame.quit()
        
if __name__ == "__main__":

    main()        

封面的模块:

"""
   碰彩球游戏的显示函数,本模块定义了display_cover函数和display函数。
"""
import pygame
from pygame.locals import *

def display_cover(cover_image):
    """
      显示封面程序,本程序显示一幅像,
      根据玩家是按窗口的退出按钮,
      还是按任意键来决定是否进入游戏。
      由于只是显示静态图形,所以在while
      循环中并没有必要时刻更新画面。
    """
    cover_image = pygame.image.load(cover_image)
    size = cover_image.get_size()
    screen = pygame.display.set_mode(size)
    pygame.display.set_caption("pygame静态封面显示标准示例模块by lixingqiu")
    clock = pygame.time.Clock()
    screen.blit(cover_image,(0,0)) # 渲染screen上
    pygame.display.update()        # 显示刷新
    enter_game = True              # 进入游戏标志     
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == QUIT: # 按关闭按钮将不进入游戏
                running=False
                enter_game = False
            if event.type in (KEYDOWN,MOUSEBUTTONDOWN):
                running=False                
        
        clock.tick(10)           # 设定fps
    if not enter_game:
        pygame.quit()
    else:
        screen.fill((25,123,23))
        pygame.display.update()
        return screen
    
def display(screen,image):
    """
       本函数只是显示一个image,
       直到按窗口关闭按钮才会退出Pygame,
       由于显示静态图形,没必要在循环中不断刷新显示。
    """
    image = pygame.image.load(image)
    screen.blit(image,(0,0))
    pygame.display.update()        # 显示刷新
    clock = pygame.time.Clock()
    # 直到按窗口的关闭按钮,才会退出pygame,否则一直滴答滴答
    while not pygame.event.get(QUIT): clock.tick(10)
    pygame.quit()
    
if __name__ == "__main__":

    screen = display_cover("images/gingerbread.png")
    print(screen)
    # if screen : main()       

 

本站所有作品,教程等皆为原创,盗版必究。一旦付款,表示同意本站知识付费原则:数字商品,不支持退款。亦可直接向微信号scratch8付款购买。QQ群免费教学群:225792826 (提供真实姓名可入群)
李兴球的博客_Python创意编程技术前沿 » 最顶层的游戏框架设计代码_碰彩球游戏

学本领,探索更大的世界!

李兴球博客 风火轮编程主页