Nikitenko Maxim (Sets88)

Personal page

Python - Game - memory test

Just a simple game - temporary memory test, where you have to find last added circle, so more circles you found, more difficult it becomes.

Wrote it for couple of hours, I have'nt tested it well, so as they say "as is".

To run application you have to have Python and pygame module installed



Source code:
#! /usr/bin/python

import pygame
from pygame.locals import *
import random
import math
import sys


class Game():
    def __init__(self, screenSize, minrad=20, maxrad=60):
        self.balls = []
        self.minrad = minrad
        self.maxrad = maxrad
        self.screenSize = screenSize
        self.shadeX = screenSize[0] * 2
        self.top = 0
        self.last = 0

    def isCrossing(self, b):
        for ball in self.balls:
            if ball.isCrossing(b):
                return True
        return False

    def addBall(self):
        while 1:
            rad = random.randint(self.minrad, self.maxrad)
            x = random.randint(rad, self.screenSize[0] - rad)
            y = random.randint(rad, self.screenSize[1] - rad)
            b = Ball(x, y, rad)
            if not self.isCrossing(b):
                self.balls.append(b)
                break

    def getLast(self):
        return self.balls[len(self.balls) - 1]

    def count(self):
        return len(self.balls)

    def draw(self, screen):
        x, y = screen.get_size()
        for ball in self.balls:
            if ball == self.getLast() and self.shadeX > 0:
                if self.shadeX < x:
                    pygame.draw.circle(screen, ball.color, (ball.x, ball.y), ball.radius)
            else:
                pygame.draw.circle(screen, ball.color, (ball.x, ball.y), ball.radius)
        if self.shadeX > 0:
            x1 = self.shadeX - x
            pygame.draw.rect(screen, (255, 255, 255), (x1, 0, self.shadeX, y))
            font = pygame.font.SysFont("monospace", 155)
            if (self.count() == 1 and self.last > 0):
                label = font.render("Game Over", 1, (255, 0, 0))
                screen.blit(label, (self.shadeX - x, 100))
                label = font.render("Score: %d" % self.last, 1, (255, 0, 0))
                screen.blit(label, (self.shadeX - x, 200))
                label = font.render("Top: %d" % self.top, 1, (255, 0, 0))
                screen.blit(label, (self.shadeX - x, 300))
            else:
                label = font.render("Level %d" % self.count(), 1, (255, 0, 0))
                screen.blit(label, (self.shadeX - x, 100))
            if (self.shadeX - x > 100 and self.shadeX - x < 200):
                self.shadeX = self.shadeX - 1
            else:
                self.shadeX = self.shadeX - 30

    def reset(self):
        self.last = self.count()
        if self.count() > self.top:
            self.top = self.count()
        self.balls = []
        self.addBall()
        self.shadeX = self.screenSize[0] * 2


class Ball():
    def __init__(self, x, y, radius, color=(255, 255, 255)):
        self.x = x
        self.y = y
        self.radius = radius
        self.color = color

    def isCrossing(self, ball):
        r1 = self.radius
        r2 = ball.radius
        r = math.sqrt((self.x - ball.x) ** 2 + (self.y - ball.y) ** 2)
        if r1 + r2 < r or r1 + r < r2 or r2 + r < r1:
            if r < self.radius and ball.radius < r:
                return True
            return False
        else:
            return True


def main():
    pygame.init()
    pygame.display.set_caption("Mind Game")
    screen = pygame.display.set_mode((1024, 768))
    background = pygame.Surface(screen.get_size())
    background = background.convert()
    background.fill((0, 0, 0))
    clock = pygame.time.Clock()

    game = Game(screen.get_size())
    game.addBall()
    sizex, sizey = screen.get_size()
    while 1:
        screen.blit(background, (0, 0))
        clock.tick(60)

        for event in pygame.event.get():
            if event.type == QUIT:
                sys.exit()
            elif event.type == MOUSEBUTTONDOWN:
                x, y = pygame.mouse.get_pos()
                if game.getLast().isCrossing(Ball(x, y, 1)):
                    game.shadeX = sizex * 2
                    game.addBall()
                elif (game.isCrossing(Ball(x, y, 1))):
                    game.reset()
        game.draw(screen)
        pygame.display.update()

if __name__ == "__main__":
    main()
Яндекс.Метрика