#python pygame 微生物
Explore tagged Tumblr posts
chaincubelog · 10 years ago
Text
pygameで微生物のようなもの。
きょうは雨。なんとなくこんなもの作ってた。環境はlinuxmint(ubuntu)。これの続き作るかどうかはわからないけどたくさん泳がせてみたいよね。
#!/usr/bin/env python # -*- coding: utf-8 -*- import sys import random import pygame from pygame.locals import * SCR_RECT = Rect(0, 0, 400, 400) SCR_COLOR = (0, 0, 110) D_SIZE = (2, 2) D_COLOR = (255, 0, 0) class Dude(object): def __init__(self, surface): self.surface = surface self.rect = self.surface.get_rect() def draw(self, display): self.surface.fill(D_COLOR) display.blit(self.surface, self.rect) def move(self, pos): x = pos[0] + random.randrange(-1, 1) y = pos[1] + random.randrange(-1, 1) if self.rect.left < 0 or self.rect.right > SCR_RECT.width: self.vx = -self.vx if self.rect.top < 0 or self.rect.bottom > SCR_RECT.height: self.vy = -self.vy self.rect.topleft = x, y#self.rect.move_ip(x, y) self.rect = self.rect.clamp(SCR_RECT) def makeSurface(size): return pygame.Surface(size).convert_alpha() def makeTitle(): title = str(sys.argv[0]) if title[0:2] == './': title = title[2:] return title def main(): pygame.init() pygame.display.set_mode(SCR_RECT.size) pygame.display.set_caption(makeTitle()) screen = pygame.display.get_surface() dude = Dude(makeSurface(D_SIZE)) loop = True clock = pygame.time.Clock() x,y = 200, 200 while loop: for e in pygame.event.get(): if e.type == pygame.QUIT: loop = False if e.type == pygame.KEYDOWN: if e.key == pygame.K_ESCAPE: loop = False if e.key == pygame.K_UP: print 'up_key' if e.key == pygame.K_DOWN: print 'down_key' if e.key == pygame.K_LEFT: print 'left_key' if e.key == pygame.K_RIGHT: print 'right_key' if e.key == pygame.K_SPACE: pass clock.tick(30) screen.fill(SCR_COLOR) dude.draw(screen) dude.move((x, y)) pygame.display.update() pygame.quit() if __name__ == '__main__' : main()
1 note · View note