Skip to content

Commit

Permalink
bump to v0.1.2, add bloodfootball
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlesPikachu committed Apr 25, 2022
1 parent 232e03b commit 11a02f9
Show file tree
Hide file tree
Showing 864 changed files with 556 additions and 37 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ This repository is created just for learning python(Commercial prohibition).
| Game26 | angrybirds | 愤怒的小鸟 | [click](./cpgames/modules/core/angrybirds) | [click](https://mp.weixin.qq.com/s/-Z_4PEF7f3ZS1CKd9D6Brg) |
| Game27 | flipcardbymemory | 记忆翻牌小游戏 | [click](./cpgames/modules/core/flipcardbymemory) | [click](https://mp.weixin.qq.com/s/H5UisWDWubdaQEBo-sH6iA) |
| Game28 | magictower | 魔塔小游戏 | [click](./cpgames/modules/core/magictower) | [click]() |
| Game29 | bloodfootball | 热血足球小游戏 | [click](./cpgames/modules/core/bloodfootball) | [click]() |


# Install
Expand Down
2 changes: 1 addition & 1 deletion cpgames/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
'''url'''
__url__ = 'https://github.com/CharlesPikachu/Games'
'''version'''
__version__ = '0.1.1'
__version__ = '0.1.2'
'''author'''
__author__ = 'Zhenchao Jin'
'''email'''
Expand Down
4 changes: 2 additions & 2 deletions cpgames/modules/__init__.py → cpgames/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'''initialize'''
from .core import (
from .games import (
SkiGame, MazeGame, GobangGame, TetrisGame, PacmanGame, GemGemGame, TankWarGame, SokobanGame, PingpongGame, TRexRushGame,
BomberManGame, WhacAMoleGame, CatchCoinsGame, FlappyBirdGame, AngryBirdsGame, MagicTowerGame, AircraftWarGame, BunnyBadgerGame,
MineSweeperGame, GreedySnakeGame, PuzzlePiecesGame, TowerDefenseGame, AlienInvasionGame, BreakoutcloneGame, TwentyfourPointGame,
FlipCardByMemoryGame, TwoZeroFourEightGame, VoiceControlPikachuGame
FlipCardByMemoryGame, TwoZeroFourEightGame, VoiceControlPikachuGame, BloodFootballGame
)
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from .towerdefense import TowerDefenseGame
from .alieninvasion import AlienInvasionGame
from .breakoutclone import BreakoutcloneGame
from .bloodfootball import BloodFootballGame
from .twentyfourpoint import TwentyfourPointGame
from .flipcardbymemory import FlipCardByMemoryGame
from .twozerofoureight import TwoZeroFourEightGame
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions cpgames/core/games/bloodfootball/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
'''initialize'''
from .bloodfootball import BloodFootballGame
160 changes: 160 additions & 0 deletions cpgames/core/games/bloodfootball/bloodfootball.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
'''
Function:
简单的热血足球小游戏
Author:
Charles
微信公众号:
Charles的皮卡丘
'''
import os
import random
import pygame
from ...utils import QuitGame
from ..base import PygameBaseGame
from .modules import StartInterface, Player, Ball


'''配置类'''
class Config():
# 根目录
rootdir = os.path.split(os.path.abspath(__file__))[0]
# FPS
FPS = 50
# 屏幕大小
SCREENSIZE = (769, 563)
SCREENSIZE_GAMING = (1200, 800)
# 标题
TITLE = '热血足球 —— Charles的皮卡丘'
# 一些颜色
WHITE = (255, 255, 255)
RED = (255, 0, 0)
LIGHTGREEN = (0, 100, 0)
# 背景音乐路径
BGM_PATH = os.path.join(rootdir, 'resources/audios/bgm.flac')
# 游戏图片路径
IMAGE_PATHS_DICT = {
'players': [
os.path.join(rootdir, 'resources/images/player1.png'),
os.path.join(rootdir, 'resources/images/player2.png'),
os.path.join(rootdir, 'resources/images/player3.png'),
os.path.join(rootdir, 'resources/images/player4.png'),
],
'balls': [
os.path.join(rootdir, 'resources/images/ball1.png'),
os.path.join(rootdir, 'resources/images/ball2.png'),
os.path.join(rootdir, 'resources/images/ball3.png'),
],
'doors': [
os.path.join(rootdir, 'resources/images/door1.bmp'),
os.path.join(rootdir, 'resources/images/door2.bmp'),
],
'background_start': os.path.join(rootdir, 'resources/images/background_start.jpg'),
}
# 字体路径
FONT_PATHS_DICT = {
'default20': {'name': os.path.join(rootdir.replace('bloodfootball', 'base'), 'resources/fonts/simkai.ttf'), 'size': 20},
'default30': {'name': os.path.join(rootdir.replace('bloodfootball', 'base'), 'resources/fonts/simkai.ttf'), 'size': 30},
'default50': {'name': os.path.join(rootdir.replace('bloodfootball', 'base'), 'resources/fonts/simkai.ttf'), 'size': 50},
}


'''简单的热血足球小游戏'''
class BloodFootballGame(PygameBaseGame):
game_type = 'bloodfootball'
def __init__(self, **kwargs):
self.cfg = Config
super(BloodFootballGame, self).__init__(config=self.cfg, **kwargs)
'''运行游戏'''
def run(self):
# 初始化
screen, resource_loader, cfg = self.screen, self.resource_loader, self.cfg
resource_loader.playbgm()
# 游戏开始界面
StartInterface(screen, resource_loader, cfg)
# 游戏主循环
screen = pygame.display.set_mode(cfg.SCREENSIZE_GAMING)
score_group1, score_group2 = 0, 0
font = resource_loader.fonts['default30']
while True:
win_group = self.playonegame(screen, resource_loader, cfg, font.render(f'{score_group1} {score_group2}', False, cfg.WHITE))
assert win_group in [1, 2]
if win_group == 1: score_group1 += 1
else: score_group2 += 1
'''进行一场游戏'''
def playonegame(self, screen, resource_loader, cfg, score_board):
# 初始化双方球员
players_group1, players_group2 = pygame.sprite.Group(), pygame.sprite.Group()
# --第一组
position = random.randint(250, 500), random.randint(350-25, 450-25)
player_controlled = Player(resource_loader.images['players'][0], position, (1, 0), False, 'common', 1)
players_group1.add(player_controlled)
position = random.randint(250, 500), random.randint(50-25, 350-25)
players_group1.add(Player(resource_loader.images['players'][1], position, (1, 0), True, 'upperhalf', 1))
position = random.randint(250, 500), random.randint(450-25, 750-25)
players_group1.add(Player(resource_loader.images['players'][1], position, (1, 0), True, 'bottomhalf', 1))
position = (85, 390)
players_group1.add(Player(resource_loader.images['players'][1], position, (0, 1), True, 'goalkeeper', 1))
# --第二组
position = random.randint(700, 950), random.randint(350-25, 450-25)
players_group2.add(Player(resource_loader.images['players'][2], position, (-1, 0), True, 'common', 2))
position = random.randint(700, 950), random.randint(50-25, 350-25)
players_group2.add(Player(resource_loader.images['players'][3], position, (-1, 0), True, 'upperhalf', 2))
position = random.randint(700, 950), random.randint(450-25, 750-25)
players_group2.add(Player(resource_loader.images['players'][3], position, (-1, 0), True, 'bottomhalf', 2))
position = (1070, 390)
players_group2.add(Player(resource_loader.images['players'][3], position, (0, 1), True, 'goalkeeper', 2))
# 初始化足球
ball = Ball(resource_loader.images['balls'], (600, 400))
# 游戏主循环
clock = pygame.time.Clock()
while True:
# --基础背景绘制
screen.fill(cfg.LIGHTGREEN)
pygame.draw.circle(screen, cfg.WHITE, (600, 400), 80, 5)
pygame.draw.rect(screen, cfg.WHITE, (10, 10, 600, 790), 5)
pygame.draw.rect(screen, cfg.WHITE, (600, 10, 590, 790), 5)
pygame.draw.rect(screen, cfg.WHITE, (10, 150, 300, 500), 5)
pygame.draw.rect(screen, cfg.WHITE, (890, 150, 300, 500), 5)
screen.blit(resource_loader.images['doors'][0].convert(), (8, 305))
screen.blit(resource_loader.images['doors'][1].convert(), (1121, 305))
screen.blit(score_board, (565, 15))
# --事件监听
for event in pygame.event.get():
if event.type == pygame.QUIT:
QuitGame()
elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
QuitGame()
pressed_keys = pygame.key.get_pressed()
direction = [0, 0]
if pressed_keys[pygame.K_w]: direction[1] -= 1
if pressed_keys[pygame.K_d]: direction[0] += 1
if pressed_keys[pygame.K_s]: direction[1] += 1
if pressed_keys[pygame.K_a]: direction[0] -= 1
if direction != [0, 0]: player_controlled.setdirection(direction)
if pressed_keys[pygame.K_SPACE] and player_controlled == ball.taken_by_player:
ball.kick(player_controlled.direction)
# --更新玩家
for item in players_group1:
if pygame.sprite.collide_mask(item, ball) and ball.taken_by_player != item:
ball.is_moving = True
ball.taken_by_player = item
for item in players_group2:
if pygame.sprite.collide_mask(item, ball) and ball.taken_by_player != item:
ball.is_moving = True
ball.taken_by_player = item
for item in players_group1:
item.update(cfg.SCREENSIZE_GAMING, ball)
for item in players_group2:
item.update(cfg.SCREENSIZE_GAMING, ball)
# --更新球
ball.update(cfg.SCREENSIZE_GAMING)
# --更新屏幕
ball.draw(screen)
players_group1.draw(screen)
players_group2.draw(screen)
clock.tick(cfg.FPS)
pygame.display.update()
# --计算得分
if ball.rect.bottom > 305 and ball.rect.top < 505:
if ball.rect.right > 1121: return 1
elif ball.rect.left < 75: return 2
4 changes: 4 additions & 0 deletions cpgames/core/games/bloodfootball/modules/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
'''initialize'''
from .ball import Ball
from .player import Player
from .startinterface import StartInterface
89 changes: 89 additions & 0 deletions cpgames/core/games/bloodfootball/modules/ball.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
'''
Function:
定义足球类
Author:
Charles
微信公众号:
Charles的皮卡丘
'''
import math
import pygame


'''定义足球类'''
class Ball(pygame.sprite.Sprite):
def __init__(self, images, position):
pygame.sprite.Sprite.__init__(self)
self.images = images
self.image = self.images[0]
self.rect = self.image.get_rect()
self.rect.centerx, self.rect.centery = position
self.mask = pygame.mask.from_surface(self.image)
# 球的速度
self.speed = 0
# 球的方向
self.direction = [0, 0]
# 控制球的球员
self.taken_by_player = None
# 用于切换球动作的变量
self.action_pointer = 0
self.count = 0
self.switch_frequency = 3
# 是否在运动状态
self.is_moving = False
'''更新'''
def update(self, screen_size):
# 静止状态
if not self.is_moving: return
# 切换球动作实现动画效果
self.count += 1
if self.count == self.switch_frequency:
self.count = 0
self.action_pointer = (self.action_pointer + 1) % len(self.images)
self.image = self.images[self.action_pointer]
# 如果球是被球员控制的
if self.taken_by_player is not None:
self.setdirection(self.taken_by_player.direction)
if self.taken_by_player.direction[0] < 0:
self.rect.left, self.rect.top = self.taken_by_player.rect.left - 15, self.taken_by_player.rect.top + 30
elif self.taken_by_player.direction[0] > 0:
self.rect.left, self.rect.top = self.taken_by_player.rect.left + 30, self.taken_by_player.rect.top + 30
elif self.taken_by_player.direction[1] < 0:
self.rect.left, self.rect.top = self.taken_by_player.rect.left + 15, self.taken_by_player.rect.top - 15
elif self.taken_by_player.direction[1] > 0:
self.rect.left, self.rect.top = self.taken_by_player.rect.left + 10, self.taken_by_player.rect.top + 50
return
# 根据方向移动球
ori_position = self.rect.left, self.rect.right, self.rect.top, self.rect.bottom
self.speed = max(self.speed - 1.7 * 0.05, 0.0)
if self.speed == 0.0: self.is_moving = False
vector = [self.speed * self.direction[0], self.speed * self.direction[1]]
vector[0] = vector[0] / math.pow(self.direction[0] ** 2 + self.direction[1] ** 2, 0.5)
vector[1] = vector[1] / math.pow(self.direction[0] ** 2 + self.direction[1] ** 2, 0.5)
self.rect.left = min(max(0, self.rect.left + vector[0]), screen_size[0] - 48)
if self.rect.left == 0 or self.rect.left == screen_size[0] - 48:
self.direction = self.direction[0] * -0.8, self.direction[1]
self.rect.top = min(max(0, self.rect.top + vector[1]), screen_size[1] - 48)
if ori_position[1] > 1121 or ori_position[0] < 75:
if self.rect.bottom > 305 and self.rect.top < 505:
if self.direction[1] > 0:
self.rect.bottom = 305
self.direction = self.direction[0], self.direction[1] * -0.8
elif self.direction[1] < 0:
self.rect.top = 505
self.direction = self.direction[0], self.direction[1] * -0.8
if self.rect.top == 0 or self.rect.top == screen_size[1] - 48:
self.direction = self.direction[0], self.direction[1] * -0.8
'''设置方向'''
def setdirection(self, direction):
self.is_moving = True
self.direction = direction
'''踢球'''
def kick(self, direction):
self.speed = 12
self.direction = direction
self.taken_by_player = None
self.is_moving = True
'''在屏幕上显示'''
def draw(self, screen):
screen.blit(self.image, self.rect)
Loading

0 comments on commit 11a02f9

Please sign in to comment.