Python之freegames 零代码的22个小游戏集合

  • Post category:Python

《Python之freegames 零代码的22个小游戏集合》是一篇介绍如何使用Python和Pygame库制作22个小游戏的文章。攻略大致分为以下几个部分:

1. 环境搭建

在开始写游戏之前,需要安装Python 3和Pygame库。作者推荐使用Anaconda环境。安装完成后,可以使用以下命令来检查Pygame是否已经安装成功:

import pygame
pygame.init()

如果没有报错,说明Pygame已经安装成功。

2. 了解Pygame

Pygame是Python制作2D游戏的库,使用它可以方便地实现游戏中的各种功能。在攻略中,作者介绍了几个常用的Pygame函数,包括pygame.init()pygame.display.set_mode()pygame.display.set_caption()pygame.time.Clock()pygame.event.get()等。

3. 制作游戏

在这个部分,作者介绍了22个小游戏的具体实现方法。对于每一个游戏,作者都提供了代码和详细的解释。这些游戏包括贪吃蛇、扫雷、弹球、俄罗斯方块、霓虹飞碟等等,种类繁多。

这里以“弹球”和“时钟模拟器”两个游戏为例:

弹球

作者提供的弹球游戏代码如下:

import pygame, random
from pygame.locals import *

def get_random_color():
    return (random.randint(0,255), random.randint(0,255), random.randint(0,255))

class Ball:

    def __init__(self, surface, x, y, size, speed):
        self.surface = surface
        self.x = x
        self.y = y
        self.size = size
        self.speed = speed
        self.color = get_random_color()
        self.direction = random.randint(20,70)

    def move(self):
        self.x += self.speed * math.cos(math.radians(self.direction))
        self.y += self.speed * math.sin(math.radians(self.direction))
        if self.x < 0 or self.x > self.surface.get_width():
            self.direction = 180 - self.direction
        if self.y < 0 or self.y > self.surface.get_height():
            self.direction = -self.direction

    def draw(self):
        pygame.draw.circle(self.surface, self.color, (int(self.x), int(self.y)), self.size, 0)

    def check_collision(self, other):
        dx = self.x - other.x
        dy = self.y - other.y
        distance = math.sqrt(dx*dx + dy*dy)
        if distance < self.size + other.size:
            self.color = other.color = get_random_color()
            self.speed, other.speed = other.speed, self.speed

pygame.init()

screen = pygame.display.set_mode((640,480))
pygame.display.set_caption('Ball Game')

clock = pygame.time.Clock()

balls = [Ball(screen, random.randint(0, screen.get_width()), \
              random.randint(0, screen.get_height()), \
              random.randint(10,50), \
              random.randint(1,10)) for i in range(25)]

while True:
    clock.tick(30)
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    screen.fill((255,255,255))

    for ball in balls:
        ball.move()
        ball.draw()
        for other_ball in balls:
            if ball != other_ball:
                ball.check_collision(other_ball)

    pygame.display.update()

这个游戏的实现方法比较复杂,但是作者的注释非常详细。具体来说,这个游戏实现了以下功能:

  • 在屏幕上产生25个随机大小、随机颜色、随机速度的球。
  • 球的初始方向也是随机的。
  • 球具有反弹功能,当碰到屏幕边缘时就会反弹。
  • 球之间可以互相碰撞,并且碰撞后会改变颜色和速度。

时钟模拟器

作者提供的时钟模拟器代码如下:

import pygame, math
from pygame.locals import *

pygame.init()

screen = pygame.display.set_mode((240,240))
pygame.display.set_caption('Clock')

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    screen.fill((255,255,255))

    pygame.draw.circle(screen, (0,0,0), (120,120), 100, 2)

    for i in range(12):
        x = 120 + 80 * math.cos(math.radians(i*30))
        y = 120 + 80 * math.sin(math.radians(i*30))
        pygame.draw.circle(screen, (0,0,0), (int(x),int(y)), 5, 0)

    time = pygame.time.get_ticks()
    seconds = (time % 60000) / 1000
    minutes = (time / 60000) % 60
    hours = (time / 3600000) % 24

    seconds_angle = seconds * 6
    minutes_angle = minutes * 6 + seconds * 0.1
    hours_angle = hours * 30 + minutes * 0.5

    seconds_x = 120 + 60 * math.cos(math.radians(seconds_angle - 90))
    seconds_y = 120 + 60 * math.sin(math.radians(seconds_angle - 90))
    pygame.draw.line(screen, (0,0,0), (120,120), (int(seconds_x),int(seconds_y)), 2)

    minutes_x = 120 + 50 * math.cos(math.radians(minutes_angle - 90))
    minutes_y = 120 + 50 * math.sin(math.radians(minutes_angle - 90))
    pygame.draw.line(screen, (255,0,0), (120,120), (int(minutes_x),int(minutes_y)), 5)

    hours_x = 120 + 30 * math.cos(math.radians(hours_angle - 90))
    hours_y = 120 + 30 * math.sin(math.radians(hours_angle - 90))
    pygame.draw.line(screen, (0,0,255), (120,120), (int(hours_x),int(hours_y)), 8)

    pygame.display.update()

这个游戏实现的是一个模拟时钟。具体来说,这个游戏实现了以下功能:

  • 在屏幕上显示一个圆形时钟。
  • 在圆形时钟的刻度位置上画上小圆点,表示每个小时的刻度点。
  • 实时更新时钟的指针位置,并且比例和真实时间相对应。
  • 时钟指针的长度和颜色有所区分。

4. 小结

在这篇攻略中,作者介绍了如何使用Python和Pygame库制作22个小游戏。通过学习这些游戏的代码,读者可以逐步掌握Pygame的各种用法,并且慢慢开始尝试自己制作游戏。