返回顶部
首页 > 资讯 > 后端开发 > Python >Python Pygame实战之五款童年经典游戏合集
  • 929
分享到

Python Pygame实战之五款童年经典游戏合集

2024-04-02 19:04:59 929人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

目录一、推箱子1)代码展示2)效果展示二、滑雪1)代码展示2)效果展示​三、声控把音符1)代码展示2)效果展示四、保卫萝卜1)代码展示2)效果展示​五、飞机大战1)代码展示2)效果展

一、推箱子

1)代码展示

import os
import sys
import cfg
import pygame
from modules import *
from itertools import chain


'''游戏地图'''
class gameMap():
    def __init__(self, num_cols, num_rows):
        self.walls = []
        self.boxes = []
        self.targets = []
        self.num_cols = num_cols
        self.num_rows = num_rows
    '''增加游戏元素'''
    def addElement(self, elem_type, col, row):
        if elem_type == 'wall':
            self.walls.append(elementSprite('wall.png', col, row, cfg))
        elif elem_type == 'box':
            self.boxes.append(elementSprite('box.png', col, row, cfg))
        elif elem_type == 'target':
            self.targets.append(elementSprite('target.png', col, row, cfg))
    '''画游戏地图'''
    def draw(self, screen):
        for elem in self.elemsIter():
            elem.draw(screen)
    '''游戏元素迭代器'''
    def elemsIter(self):
        for elem in chain(self.targets, self.walls, self.boxes):
            yield elem
    '''该关卡中所有的箱子是否都在指定位置, 在的话就是通关了'''
    def levelCompleted(self):
        for box in self.boxes:
            is_match = False
            for target in self.targets:
                if box.col == target.col and box.row == target.row:
                    is_match = True
                    break
            if not is_match:
                return False
        return True
    '''某位置是否可到达'''
    def isValidPos(self, col, row):
        if col >= 0 and row >= 0 and col < self.num_cols and row < self.num_rows:
            block_size = cfg.BLOCKSIZE
            temp1 = self.walls + self.boxes
            temp2 = pygame.Rect(col * block_size, row * block_size, block_size, block_size)
            return temp2.collidelist(temp1) == -1
        else:
            return False
    '''获得某位置的box'''
    def getBox(self, col, row):
        for box in self.boxes:
            if box.col == col and box.row == row:
                return box
        return None


'''游戏界面'''
class gameInterface():
    def __init__(self, screen):
        self.screen = screen
        self.levels_path = cfg.LEVELDIR
        self.initGame()
    '''导入关卡地图'''
    def loadLevel(self, game_level):
        with open(os.path.join(self.levels_path, game_level), 'r') as f:
            lines = f.readlines()
        # 游戏地图
        self.game_map = gameMap(max([len(line) for line in lines]) - 1, len(lines))
        # 游戏surface
        height = cfg.BLOCKSIZE * self.game_map.num_rows
        width = cfg.BLOCKSIZE * self.game_map.num_cols
        self.game_surface = pygame.Surface((width, height))
        self.game_surface.fill(cfg.BACKGROUNDCOLOR)
        self.game_surface_blank = self.game_surface.copy()
        for row, elems in enumerate(lines):
            for col, elem in enumerate(elems):
                if elem == 'p':
                    self.player = pusherSprite(col, row, cfg)
                elif elem == '*':
                    self.game_map.addElement('wall', col, row)
                elif elem == '#':
                    self.game_map.addElement('box', col, row)
                elif elem == 'o':
                    self.game_map.addElement('target', col, row)
    '''游戏初始化'''
    def initGame(self):
        self.scroll_x = 0
        self.scroll_y = 0
    '''将游戏界面画出来'''
    def draw(self, *elems):
        self.scroll()
        self.game_surface.blit(self.game_surface_blank, dest=(0, 0))
        for elem in elems:
            elem.draw(self.game_surface)
        self.screen.blit(self.game_surface, dest=(self.scroll_x, self.scroll_y))
    '''因为游戏界面面积>游戏窗口界面, 所以需要根据人物位置滚动'''
    def scroll(self):
        x, y = self.player.rect.center
        width = self.game_surface.get_rect().w
        height = self.game_surface.get_rect().h
        if (x + cfg.SCREENSIZE[0] // 2) > cfg.SCREENSIZE[0]:
            if -1 * self.scroll_x + cfg.SCREENSIZE[0] < width:
                self.scroll_x -= 2
        elif (x + cfg.SCREENSIZE[0] // 2) > 0:
            if self.scroll_x < 0:
                self.scroll_x += 2
        if (y + cfg.SCREENSIZE[1] // 2) > cfg.SCREENSIZE[1]:
            if -1 * self.scroll_y + cfg.SCREENSIZE[1] < height:
                self.scroll_y -= 2
        elif (y + 250) > 0:
            if self.scroll_y < 0:
                self.scroll_y += 2


'''某一关卡的游戏主循环'''
def runGame(screen, game_level):
    clock = pygame.time.Clock()
    game_interface = gameInterface(screen)
    game_interface.loadLevel(game_level)
    font_path = os.path.join(cfg.FONTDIR, 'simkai.ttf')
    text = '按R键重新开始本关'
    font = pygame.font.Font(font_path, 15)
    text_render = font.render(text, 1, (255, 255, 255))
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit(0)
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    next_pos = game_interface.player.move('left', is_test=True)
                    if game_interface.game_map.isValidPos(*next_pos):
                        game_interface.player.move('left')
                    else:
                        box = game_interface.game_map.getBox(*next_pos)
                        if box:
                            next_pos = box.move('left', is_test=True)
                            if game_interface.game_map.isValidPos(*next_pos):
                                game_interface.player.move('left')
                                box.move('left')
                    break
                if event.key == pygame.K_RIGHT:
                    next_pos = game_interface.player.move('right', is_test=True)
                    if game_interface.game_map.isValidPos(*next_pos):
                        game_interface.player.move('right')
                    else:
                        box = game_interface.game_map.getBox(*next_pos)
                        if box:
                            next_pos = box.move('right', is_test=True)
                            if game_interface.game_map.isValidPos(*next_pos):
                                game_interface.player.move('right')
                                box.move('right')
                    break
                if event.key == pygame.K_DOWN:
                    next_pos = game_interface.player.move('down', is_test=True)
                    if game_interface.game_map.isValidPos(*next_pos):
                        game_interface.player.move('down')
                    else:
                        box = game_interface.game_map.getBox(*next_pos)
                        if box:
                            next_pos = box.move('down', is_test=True)
                            if game_interface.game_map.isValidPos(*next_pos):
                                game_interface.player.move('down')
                                box.move('down')
                    break
                if event.key == pygame.K_UP:
                    next_pos = game_interface.player.move('up', is_test=True)
                    if game_interface.game_map.isValidPos(*next_pos):
                        game_interface.player.move('up')
                    else:
                        box = game_interface.game_map.getBox(*next_pos)
                        if box:
                            next_pos = box.move('up', is_test=True)
                            if game_interface.game_map.isValidPos(*next_pos):
                                game_interface.player.move('up')
                                box.move('up')
                    break
                if event.key == pygame.K_r:
                    game_interface.initGame()
                    game_interface.loadLevel(game_level)
        game_interface.draw(game_interface.player, game_interface.game_map)
        if game_interface.game_map.levelCompleted():
            return
        screen.blit(text_render, (5, 5))
        pygame.display.flip()
        clock.tick(100)


'''主函数'''
def main():
    pygame.init()
    pygame.mixer.init()
    pygame.display.set_caption('推箱子 —— 源码基地:#959755565#')
    screen = pygame.display.set_mode(cfg.SCREENSIZE)
    pygame.mixer.init()
    audio_path = os.path.join(cfg.AUDIODIR, 'EineLiebe.mp3')
    pygame.mixer.music.load(audio_path)
    pygame.mixer.music.set_volume(0.4)
    pygame.mixer.music.play(-1)
    startInterface(screen, cfg)
    for level_name in sorted(os.listdir(cfg.LEVELDIR)):
        runGame(screen, level_name)
        switchInterface(screen, cfg)
    endInterface(screen, cfg)


'''run'''
if __name__ == '__main__':
    main()

2)效果展示

二、滑雪

1)代码展示

import sys
import cfg
import pygame
import random


'''滑雪者类'''
class SkierClass(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        # 滑雪者的朝向(-2到2)
        self.direction = 0
        self.imagepaths = cfg.SKIER_IMAGE_PATHS[:-1]
        self.image = pygame.image.load(self.imagepaths[self.direction])
        self.rect = self.image.get_rect()
        self.rect.center = [320, 100]
        self.speed = [self.direction, 6-abs(self.direction)*2]
    '''改变滑雪者的朝向. 负数为向左,正数为向右,0为向前'''
    def turn(self, num):
        self.direction += num
        self.direction = max(-2, self.direction)
        self.direction = min(2, self.direction)
        center = self.rect.center
        self.image = pygame.image.load(self.imagepaths[self.direction])
        self.rect = self.image.get_rect()
        self.rect.center = center
        self.speed = [self.direction, 6-abs(self.direction)*2]
        return self.speed
    '''移动滑雪者'''
    def move(self):
        self.rect.centerx += self.speed[0]
        self.rect.centerx = max(20, self.rect.centerx)
        self.rect.centerx = min(620, self.rect.centerx)
    '''设置为摔倒状态'''
    def setFall(self):
        self.image = pygame.image.load(cfg.SKIER_IMAGE_PATHS[-1])
    '''设置为站立状态'''
    def setForward(self):
        self.direction = 0
        self.image = pygame.image.load(self.imagepaths[self.direction])


'''
Function:
    障碍物类
Input:
    img_path: 障碍物图片路径
    location: 障碍物位置
    attribute: 障碍物类别属性
'''
class ObstacleClass(pygame.sprite.Sprite):
    def __init__(self, img_path, location, attribute):
        pygame.sprite.Sprite.__init__(self)
        self.img_path = img_path
        self.image = pygame.image.load(self.img_path)
        self.location = location
        self.rect = self.image.get_rect()
        self.rect.center = self.location
        self.attribute = attribute
        self.passed = False
    '''移动'''
    def move(self, num):
        self.rect.centery = self.location[1] - num


'''创建障碍物'''
def createObstacles(s, e, num=10):
    obstacles = pygame.sprite.Group()
    locations = []
    for i in range(num):
        row = random.randint(s, e)
        col = random.randint(0, 9)
        location  = [col*64+20, row*64+20]
        if location not in locations:
            locations.append(location)
            attribute = random.choice(list(cfg.OBSTACLE_PATHS.keys()))
            img_path = cfg.OBSTACLE_PATHS[attribute]
            obstacle = ObstacleClass(img_path, location, attribute)
            obstacles.add(obstacle)
    return obstacles


'''合并障碍物'''
def AddObstacles(obstacles0, obstacles1):
    obstacles = pygame.sprite.Group()
    for obstacle in obstacles0:
        obstacles.add(obstacle)
    for obstacle in obstacles1:
        obstacles.add(obstacle)
    return obstacles


'''显示游戏开始界面'''
def ShowStartInterface(screen, screensize):
    screen.fill((255, 255, 255))
    tfont = pygame.font.Font(cfg.FONTPATH, screensize[0]//5)
    cfont = pygame.font.Font(cfg.FONTPATH, screensize[0]//20)
    title = tfont.render(u'滑雪游戏', True, (255, 0, 0))
    content = cfont.render(u'按任意键开始游戏', True, (0, 0, 255))
    trect = title.get_rect()
    trect.midtop = (screensize[0]/2, screensize[1]/5)
    crect = content.get_rect()
    crect.midtop = (screensize[0]/2, screensize[1]/2)
    screen.blit(title, trect)
    screen.blit(content, crect)
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                return
        pygame.display.update()


'''显示分数'''
def showScore(screen, score, pos=(10, 10)):
    font = pygame.font.Font(cfg.FONTPATH, 30)
    score_text = font.render("Score: %s" % score, True, (0, 0, 0))
    screen.blit(score_text, pos)


'''更新当前帧的游戏画面'''
def updateFrame(screen, obstacles, skier, score):
    screen.fill((255, 255, 255))
    obstacles.draw(screen)
    screen.blit(skier.image, skier.rect)
    showScore(screen, score)
    pygame.display.update()


'''主程序'''
def main():
    # 游戏初始化
    pygame.init()
    pygame.mixer.init()
    pygame.mixer.music.load(cfg.BGMPATH)
    pygame.mixer.music.set_volume(0.4)
    pygame.mixer.music.play(-1)
    # 设置屏幕
    screen = pygame.display.set_mode(cfg.SCREENSIZE)
    pygame.display.set_caption('滑雪大冒险')
    # 游戏开始界面
    ShowStartInterface(screen, cfg.SCREENSIZE)
    # 实例化游戏精灵
    # --滑雪者
    skier = SkierClass()
    # --创建障碍物
    obstacles0 = createObstacles(20, 29)
    obstacles1 = createObstacles(10, 19)
    obstaclesflag = 0
    obstacles = AddObstacles(obstacles0, obstacles1)
    # 游戏clock
    clock = pygame.time.Clock()
    # 记录滑雪的距离
    distance = 0
    # 记录当前的分数
    score = 0
    # 记录当前的速度
    speed = [0, 6]
    # 游戏主循环
    while True:
        # --事件捕获
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT or event.key == pygame.K_a:
                    speed = skier.turn(-1)
                elif event.key == pygame.K_RIGHT or event.key == pygame.K_d:
                    speed = skier.turn(1)
        # --更新当前游戏帧的数据
        skier.move()
        distance += speed[1]
        if distance >= 640 and obstaclesflag == 0:
            obstaclesflag = 1
            obstacles0 = createObstacles(20, 29)
            obstacles = AddObstacles(obstacles0, obstacles1)
        if distance >= 1280 and obstaclesflag == 1:
            obstaclesflag = 0
            distance -= 1280
            for obstacle in obstacles0:
                obstacle.location[1] = obstacle.location[1] - 1280
            obstacles1 = createObstacles(10, 19)
            obstacles = AddObstacles(obstacles0, obstacles1)
        for obstacle in obstacles:
            obstacle.move(distance)
        # --碰撞检测
        hitted_obstacles = pygame.sprite.spritecollide(skier, obstacles, False)
        if hitted_obstacles:
            if hitted_obstacles[0].attribute == "tree" and not hitted_obstacles[0].passed:
                score -= 50
                skier.setFall()
                updateFrame(screen, obstacles, skier, score)
                pygame.time.delay(1000)
                skier.setForward()
                speed = [0, 6]
                hitted_obstacles[0].passed = True
            elif hitted_obstacles[0].attribute == "flag" and not hitted_obstacles[0].passed:
                score += 10
                obstacles.remove(hitted_obstacles[0])
        # --更新屏幕
        updateFrame(screen, obstacles, skier, score)
        clock.tick(cfg.FPS)


'''run'''
if __name__ == '__main__':
    main()

2)效果展示​

三、声控把音符

1)代码展示

import cfg
import cocos
import struct
from modules import *
from cocos.sprite import Sprite
from pyaudio import PyAudio, paInt16


'''定义声控游戏类'''
class VCGame(cocos.layer.ColorLayer):
    def __init__(self):
        super(VCGame, self).__init__(255, 255, 255, 255, 800, 600)
        # frames_per_buffer
        self.num_samples = 1000
        # 声控条
        self.vbar = Sprite(cfg.BLOCK_IMAGE_PATH)
        self.vbar.position = 20, 450
        self.vbar.scale_y = 0.1
        self.vbar.image_anchor = 0, 0
        self.add(self.vbar)
        # 皮卡丘
        self.pikachu = Pikachu(cfg.PIKACHU_IMAGE_PATH)
        self.add(self.pikachu)
        # 地面
        self.floor = cocos.cocosnode.CocosNode()
        self.add(self.floor)
        position = 0, 100
        for i in range(120):
            b = Block(cfg.BLOCK_IMAGE_PATH, position)
            self.floor.add(b)
            position = b.x + b.width, b.height
        # 声音输入
        audio = PyAudio()
        self.stream = audio.open(fORMat=paInt16, channels=1, rate=int(audio.get_device_info_by_index(0)['defaultSampleRate']), input=True, frames_per_buffer=self.num_samples)
        # 屏幕更新
        self.schedule(self.update)
    '''碰撞检测'''
    def collide(self):
        diffx = self.pikachu.x - self.floor.x
        for b in self.floor.get_children():
            if (b.x <= diffx + self.pikachu.width * 0.8) and (diffx + self.pikachu.width * 0.2 <= b.x + b.width):
                if self.pikachu.y < b.height:
                    self.pikachu.land(b.height)
                    break
    '''定义游戏规则'''
    def update(self, dt):
        # 获取每帧的音量
        audio_data = self.stream.read(self.num_samples)
        k = max(struct.unpack('1000h', audio_data))
        self.vbar.scale_x = k / 10000.0
        if k > 3000:
            self.floor.x -= min((k / 20.0), 150) * dt
        # 皮卡丘跳跃
        if k > 8000:
            self.pikachu.jump((k - 8000) / 1000.0)
        # 碰撞检测
        self.collide()
    '''重置'''
    def reset(self):
        self.floor.x = 0


'''run'''
if __name__ == '__main__':
    cocos.director.director.init(caption="xiaohei Go Go Go ")
    cocos.director.director.run(cocos.scene.Scene(VCGame()))

2)效果展示

四、保卫萝卜

1)代码展示

import sys
import cfg
import math
import random
import pygame
from modules import *


'''游戏初始化'''
def initGame():
    # 初始化pygame, 设置展示窗口
    pygame.init()
    pygame.mixer.init()
    screen = pygame.display.set_mode(cfg.SCREENSIZE)
    pygame.display.set_caption('兔子')
    # 加载必要的游戏素材
    game_images = {}
    for key, value in cfg.IMAGE_PATHS.items():
        game_images[key] = pygame.image.load(value)
    game_sounds = {}
    for key, value in cfg.SOUNDS_PATHS.items():
        if key != 'moonlight':
            game_sounds[key] = pygame.mixer.Sound(value)
    return screen, game_images, game_sounds


'''主函数'''
def main():
    # 初始化
    screen, game_images, game_sounds = initGame()
    # 播放背景音乐
    pygame.mixer.music.load(cfg.SOUNDS_PATHS['moonlight'])
    pygame.mixer.music.play(-1, 0.0)
    # 字体加载
    font = pygame.font.Font(None, 24)
    # 定义兔子
    bunny = BunnySprite(image=game_images.get('rabbit'), position=(100, 100))
    # 跟踪玩家的精度变量, 记录了射出的箭头数和被击中的獾的数量.
    acc_record = [0., 0.]
    # 生命值
    healthvalue = 194
    # 弓箭
    arrow_sprites_group = pygame.sprite.Group()
    # 獾
    badguy_sprites_group = pygame.sprite.Group()
    badguy = BadguySprite(game_images.get('badguy'), position=(640, 100))
    badguy_sprites_group.add(badguy)
    # 定义了一个定时器, 使得游戏里经过一段时间后就新建一支獾
    badtimer = 100
    badtimer1 = 0
    # 游戏主循环, running变量会跟踪游戏是否结束, exitcode变量会跟踪玩家是否胜利.
    running, exitcode = True, False
    clock = pygame.time.Clock()
    while running:
        # --在给屏幕画任何东西之前用黑色进行填充
        screen.fill(0)
        # --添加的风景也需要画在屏幕上
        for x in range(cfg.SCREENSIZE[0]//game_images['grass'].get_width()+1):
            for y in range(cfg.SCREENSIZE[1]//game_images['grass'].get_height()+1):
                screen.blit(game_images['grass'], (x*100, y*100))
        for i in range(4): screen.blit(game_images['castle'], (0, 30+105*i))
        # --倒计时信息
        countdown_text = font.render(str((90000-pygame.time.get_ticks())//60000)+":"+str((90000-pygame.time.get_ticks())//1000%60).zfill(2), True, (0, 0, 0))
        countdown_rect = countdown_text.get_rect()
        countdown_rect.topright = [635, 5]
        screen.blit(countdown_text, countdown_rect)
        # --按键检测
        # ----退出与射击
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                game_sounds['shoot'].play()
                acc_record[1] += 1
                mouse_pos = pygame.mouse.get_pos()
                angle = math.atan2(mouse_pos[1]-(bunny.rotated_position[1]+32), mouse_pos[0]-(bunny.rotated_position[0]+26))
                arrow = ArrowSprite(game_images.get('arrow'), (angle, bunny.rotated_position[0]+32, bunny.rotated_position[1]+26))
                arrow_sprites_group.add(arrow)
        # ----移动兔子
        key_pressed = pygame.key.get_pressed()
        if key_pressed[pygame.K_w]:
            bunny.move(cfg.SCREENSIZE, 'up')
        elif key_pressed[pygame.K_s]:
            bunny.move(cfg.SCREENSIZE, 'down')
        elif key_pressed[pygame.K_a]:
            bunny.move(cfg.SCREENSIZE, 'left')
        elif key_pressed[pygame.K_d]:
            bunny.move(cfg.SCREENSIZE, 'right')
        # --更新弓箭
        for arrow in arrow_sprites_group:
            if arrow.update(cfg.SCREENSIZE):
                arrow_sprites_group.remove(arrow)
        # --更新獾
        if badtimer == 0:
            badguy = BadguySprite(game_images.get('badguy'), position=(640, random.randint(50, 430)))
            badguy_sprites_group.add(badguy)
            badtimer = 100 - (badtimer1 * 2)
            badtimer1 = 20 if badtimer1>=20 else badtimer1+2
        badtimer -= 1
        for badguy in badguy_sprites_group:
            if badguy.update():
                game_sounds['hit'].play()
                healthvalue -= random.randint(4, 8)
                badguy_sprites_group.remove(badguy)
        # --碰撞检测
        for arrow in arrow_sprites_group:
            for badguy in badguy_sprites_group:
                if pygame.sprite.collide_mask(arrow, badguy):
                    game_sounds['enemy'].play()
                    arrow_sprites_group.remove(arrow)
                    badguy_sprites_group.remove(badguy)
                    acc_record[0] += 1
        # --画出弓箭
        arrow_sprites_group.draw(screen)
        # --画出獾
        badguy_sprites_group.draw(screen)
        # --画出兔子
        bunny.draw(screen, pygame.mouse.get_pos())
        # --画出城堡健康值, 首先画了一个全红色的生命值条, 然后根据城堡的生命值往生命条里面添加绿色.
        screen.blit(game_images.get('healthbar'), (5, 5))
        for i in range(healthvalue):
            screen.blit(game_images.get('health'), (i+8, 8))
        # --判断游戏是否结束
        if pygame.time.get_ticks() >= 90000:
            running, exitcode = False, True
        if healthvalue <= 0:
            running, exitcode = False, False
        # --更新屏幕
        pygame.display.flip()
        clock.tick(cfg.FPS)
    # 计算准确率
    accuracy = acc_record[0] / acc_record[1] * 100 if acc_record[1] > 0 else 0
    accuracy = '%.2f' % accuracy
    showEndGameInterface(screen, exitcode, accuracy, game_images)


'''run'''
if __name__ == '__main__':
    main()

2)效果展示​

五、飞机大战

1)代码展示

import sys
import cfg
import pygame
from modules import *


'''游戏界面'''
def GamingInterface(num_player, screen):
    # 初始化
    pygame.mixer.music.load(cfg.SOUNDPATHS['Cool Space Music'])
    pygame.mixer.music.set_volume(0.4)
    pygame.mixer.music.play(-1)
    explosion_sound = pygame.mixer.Sound(cfg.SOUNDPATHS['boom'])
    fire_sound = pygame.mixer.Sound(cfg.SOUNDPATHS['shot'])
    font = pygame.font.Font(cfg.FONTPATH, 20)
    # 游戏背景图
    bg_imgs = [cfg.IMAGEPATHS['bg_big'], cfg.IMAGEPATHS['seamless_space'], cfg.IMAGEPATHS['space3']]
    bg_move_dis = 0
    bg_1 = pygame.image.load(bg_imgs[0]).convert()
    bg_2 = pygame.image.load(bg_imgs[1]).convert()
    bg_3 = pygame.image.load(bg_imgs[2]).convert()
    # 玩家, 子弹和小行星精灵组
    player_group = pygame.sprite.Group()
    bullet_group = pygame.sprite.Group()
    asteroid_group = pygame.sprite.Group()
    # 产生小行星的时间间隔
    asteroid_ticks = 90
    for i in range(num_player):
        player_group.add(Ship(i+1, cfg))
    clock = pygame.time.Clock()
    # 分数
    score_1, score_2 = 0, 0
    # 游戏主循环
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        # --玩家一: ↑↓←→控制, j射击; 玩家二: wsad控制, 空格射击
        pressed_keys = pygame.key.get_pressed()
        for idx, player in enumerate(player_group):
            direction = None
            if idx == 0:
                if pressed_keys[pygame.K_UP]:
                    direction = 'up'
                elif pressed_keys[pygame.K_DOWN]:
                    direction = 'down'
                elif pressed_keys[pygame.K_LEFT]:
                    direction = 'left'
                elif pressed_keys[pygame.K_RIGHT]:
                    direction = 'right'
                if direction:
                    player.move(direction)
                if pressed_keys[pygame.K_j]:
                    if player.cooling_time == 0:
                        fire_sound.play()
                        bullet_group.add(player.shot())
                        player.cooling_time = 20
            elif idx == 1:
                if pressed_keys[pygame.K_w]:
                    direction = 'up'
                elif pressed_keys[pygame.K_s]:
                    direction = 'down'
                elif pressed_keys[pygame.K_a]:
                    direction = 'left'
                elif pressed_keys[pygame.K_d]:
                    direction = 'right'
                if direction:
                    player.move(direction)
                if pressed_keys[pygame.K_SPACE]:
                    if player.cooling_time == 0:
                        fire_sound.play()
                        bullet_group.add(player.shot())
                        player.cooling_time = 20
            if player.cooling_time > 0:
                player.cooling_time -= 1
        if (score_1 + score_2) < 500:
            background = bg_1
        elif (score_1 + score_2) < 1500:
            background = bg_2
        else:
            background = bg_3
        # --向下移动背景图实现飞船向上移动的效果
        screen.blit(background, (0, -background.get_rect().height + bg_move_dis))
        screen.blit(background, (0, bg_move_dis))
        bg_move_dis = (bg_move_dis + 2) % background.get_rect().height
        # --生成小行星
        if asteroid_ticks == 0:
            asteroid_ticks = 90
            asteroid_group.add(Asteroid(cfg))
        else:
            asteroid_ticks -= 1
        # --画飞船
        for player in player_group:
            if pygame.sprite.spritecollide(player, asteroid_group, True, None):
                player.explode_step = 1
                explosion_sound.play()
            elif player.explode_step > 0:
                if player.explode_step > 3:
                    player_group.remove(player)
                    if len(player_group) == 0:
                        return
                else:
                    player.explode(screen)
            else:
                player.draw(screen)
        # --画子弹
        for bullet in bullet_group:
            bullet.move()
            if pygame.sprite.spritecollide(bullet, asteroid_group, True, None):
                bullet_group.remove(bullet)
                if bullet.player_idx == 1:
                    score_1 += 1
                else:
                    score_2 += 1
            else:
                bullet.draw(screen)
        # --画小行星
        for asteroid in asteroid_group:
            asteroid.move()
            asteroid.rotate()
            asteroid.draw(screen)
        # --显示分数
        score_1_text = '玩家一得分: %s' % score_1
        score_2_text = '玩家二得分: %s' % score_2
        text_1 = font.render(score_1_text, True, (0, 0, 255))
        text_2 = font.render(score_2_text, True, (255, 0, 0))
        screen.blit(text_1, (2, 5))
        screen.blit(text_2, (2, 35))
        # --屏幕刷新
        pygame.display.update()
        clock.tick(60)


'''主函数'''
def main():
    pygame.init()
    pygame.font.init()
    pygame.mixer.init()
    screen = pygame.display.set_mode(cfg.SCREENSIZE)
    pygame.display.set_caption('简易版——飞机大战 ')
    num_player = StartInterface(screen, cfg)
    if num_player == 1:
        while True:
            GamingInterface(num_player=1, screen=screen)
            EndInterface(screen, cfg)
    else:
        while True:
            GamingInterface(num_player=2, screen=screen)
            EndInterface(screen, cfg)


'''run'''
if __name__ == '__main__':
    main()

2)效果展示

以上就是python Pygame实战之五款童年经典游戏合集的详细内容,更多关于Python Pygame游戏的资料请关注编程网其它相关文章!

--结束END--

本文标题: Python Pygame实战之五款童年经典游戏合集

本文链接: https://lsjlt.com/news/117359.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
  • Python Pygame实战之五款童年经典游戏合集
    目录一、推箱子1)代码展示2)效果展示二、滑雪1)代码展示2)效果展示​三、声控把音符1)代码展示2)效果展示四、保卫萝卜1)代码展示2)效果展示​五、飞机大战1)代码展示2)效果展...
    99+
    2024-04-02
  • python游戏实战项目之童年经典超级玛丽
    导语 “超级玛丽”——有多少人还记得这款经典游戏?那个戴帽子的大胡子穿着背带裤的马里奥! 带您重温经典的回忆,超级马里奥拯救不开心!炫酷来袭。 如果您的童年,也曾被魔性的 灯~灯...
    99+
    2024-04-02
  • Pygame实战之经典泡泡龙小游戏
    目录导语​正文一、准备中二、开始敲代码三、效果展示总结导语 Python版的消除类的游戏还是很多的,木木子之前也是推过不少~ 比如:百变的消消乐,还记得嘛?今天就出一个消除类——泡...
    99+
    2024-04-02
  • Pygame实战之实现经典外星人游戏
    目录前言正文安装环境主要代码前言 说说外星人那些事儿.... 你以为的外星人......他们都是乘坐UFO来地球的,长的嗯哼可能比较有特色。也许长这样 害!其实可能也或许不长这样。...
    99+
    2024-04-02
  • Python+Pygame实现经典魂斗罗游戏
    目录一、效果展示二、操作说明三、核心代码今天分享一个经典小游戏魂斗罗的 Python 版实现。 一、效果展示 二、操作说明 A:向左 D:向右 W:跳起 S:趴下 J:射击 P:退...
    99+
    2024-04-02
  • Python实现四个经典小游戏合集
    目录 一、效果展示1、俄罗斯方块2、扫雷3、五子棋4、贪吃蛇二、代码展示1、俄罗斯方块2、扫雷3、五子棋4、贪吃蛇 一、效果展示 1、俄罗斯方块 这个应该是玩起来最最简单的了… 2...
    99+
    2024-04-02
  • 基于Python+Pygame实现经典赛车游戏
    目录导语一、环境安装二、代码展示1.主程序main.py2.地图设置maps.py三、效果展示1.游戏界面2.游戏运行中3.15分到手导语 哈喽!哈喽~我是木木子,很久没给大家更新游...
    99+
    2024-04-02
  • Python Pygame实战之打砖块小游戏
    目录导语一、准备中1)游戏规则:2)环境安装二、开始敲代码1)配置文件2)定义一些类3)定义开始、结束界面4)定义游戏5)主函数与运行界面三、效果展示导语 嘿!前不久刚刚给大家过一款...
    99+
    2024-04-02
  • 基于Python+Pygame怎么实现经典赛车游戏
    这篇文章主要介绍“基于Python+Pygame怎么实现经典赛车游戏”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“基于Python+Pygame怎么实现经典赛车游戏”文章能帮助大家解决问题。一、环境...
    99+
    2023-06-30
  • 十个Python经典小游戏的代码合集
    目录1.小猫运动游戏源码游戏效果2.弹球游戏源码游戏效果3.画正方形游戏源码游戏效果4.彩点实验游戏源码游戏效果5.彩点实验圆形游戏源码游戏效果6.彩点实验下三角游戏源码游戏效果7....
    99+
    2024-04-02
  • Python Pygame实战之红心大战游戏的实现
    目录导语一、 红心大战用户手册二、红心大战游戏规则三、准备中四、代码演示五、效果展示导语 还记得那些年,我们玩过的Windows小游戏吗? 说起Windows自带的游戏,相信许多8...
    99+
    2024-04-02
  • Python+Pygame实战之24点游戏的实现
    目录导语游戏介绍实现代码游戏效果展示导语 我第一次玩24点是初中的时候,那时候和堂弟表哥在堂妹家玩,堂妹提出玩24点游戏,堂妹比我们小三岁,可能正在上小学吧。 拿出一副扑克牌去掉大小...
    99+
    2024-04-02
  • Python+Pygame实战之泡泡游戏的实现
    目录导语一、环境安装二、代码展示三、效果展示导语 泡泡王国 欢乐多多 咕噜噜,吹泡泡,七彩泡泡满天飘。大的好像彩气球,小的就像紫葡萄。 ​当泡泡漫天飞舞时,大朋友、小朋友都会情不自禁...
    99+
    2024-04-02
  • Python Pygame实战之欢乐打地鼠游戏
    目录导语​​​一、准备中二、环境安装​三、开始敲代码1)导入模块2)不同的图片设置3)界面头标、标题等。4)音乐设置5)右侧窗口控件不同的按钮设置四、效果展示导语 闲来无事: 铲屎...
    99+
    2024-04-02
  • Python Pygame实战之打砖块游戏的实现
    目录导语开发工具环境搭建效果展示原理简介导语 想起来好久没更这个系列的文章了,周末过来补一波好了。本期我们将利用python制作一个打砖块小游戏,废话不多说,让我们愉快地开始吧~ 开...
    99+
    2024-04-02
  • Python+Pygame实战之吃豆豆游戏的实现
    目录导语​一、首先​二、正式开始三、效果展示导语​ ​昨晚玩起了小时候玩的游戏“吃豆豆”,但是我发现,一局游戏三条命,我根本不能吃完所有的豆豆,总是被敌人吃掉...
    99+
    2024-04-02
  • Python+Pygame实战之文字剧情游戏的实现
    目录前言一、《巨龙之洞》1)小故事2)环境配置3)代码展示4)效果展示二、《太空矿工》1)小故事2)环境配置3)代码展示4)效果展示前言 哈喽!我是你们的栗子同学—&md...
    99+
    2022-12-08
    Python Pygame文字剧情游戏 Python 文字剧情游戏 Python Pygame 游戏
  • python实战之利用pygame实现贪吃蛇游戏(一)
    目录一、前言二、搭建界面三、运行结果四、结语一、前言 之前尝试了自己用pygame写井字棋,这次玩的是贪吃蛇系列。 个人感觉模块可能会比较大,所以选择将函数和主要逻辑代码分在了两个文件中。 fuc为函数模块,存储了...
    99+
    2022-06-02
    pygame实现贪吃蛇游戏 python游戏 python pygame
  • python实战之利用pygame实现贪吃蛇游戏(二)
    目录一、前言二、创建蛇三、创建食物四、蛇的移动五、按键感应六、整合部分七、结语一、前言 在上一篇博客中,我们实现了基本的界面搭建,这次实现一下逻辑部分。 二、创建蛇 首先,先分析一下...
    99+
    2024-04-02
  • Python+Pygame实战之疯狂吃水果游戏的实现
    目录导语一、准备中 1)游戏玩法2)环境安装3)素材准备二、代码展示三、效果展示导语 嘿嘿!木木子今日闪现——已经给大家写了很多内容啦~ 涉及的人工...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作