一、推箱子
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游戏的资料请关注编程网其它相关文章!