惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
爱范儿
爱范儿
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
雷峰网
雷峰网
V
V2EX
博客园_首页
Engineering at Meta
Engineering at Meta
博客园 - 聂微东
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Apple Machine Learning Research
Apple Machine Learning Research
GbyAI
GbyAI
H
Help Net Security
A
About on SuperTechFans
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Blog — PlanetScale
Blog — PlanetScale
W
WeLiveSecurity
云风的 BLOG
云风的 BLOG
D
Docker
Security Archives - TechRepublic
Security Archives - TechRepublic
Help Net Security
Help Net Security
N
News and Events Feed by Topic
Simon Willison's Weblog
Simon Willison's Weblog
G
Google Developers Blog
A
Arctic Wolf
T
The Blog of Author Tim Ferriss
博客园 - 叶小钗
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Google DeepMind News
Google DeepMind News
博客园 - 三生石上(FineUI控件)
aimingoo的专栏
aimingoo的专栏
Hacker News: Ask HN
Hacker News: Ask HN
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 司徒正美
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy International News Feed
T
Troy Hunt's Blog
T
Tenable Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Recorded Future
Recorded Future
F
Fortinet All Blogs
D
DataBreaches.Net
B
Blog
T
Threat Research - Cisco Blogs
MyScale Blog
MyScale Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
The GitHub Blog
The GitHub Blog
Security Latest
Security Latest
M
MIT News - Artificial intelligence

博客园 - 飘啊飘

一个精简的vi源码(2000行) dtruss 粗糙的翻译 gdb常用命令[转] gdb中信号的处理[转] 在linux中通过进程名获得进程id busybox0.60.3源码学习开始 python中使用struct模块处理二进制数据 使用PYGAME开发的坦克游戏[代码][思路] 一篇很好的讲/etc/inittab的文章[转] 哲学家吃空心粉问题 《代码整洁之道》笔记之函数 使用面向对象概念优化条件判断语句的一个小应用 python生成文件树的代码 深入理解软件包的配置、编译与安装[转] pygame学习之对象移动 pywin32重启电脑 - 飘啊飘 - 博客园 解决LINUX和WINDOWS时间不一置 通过状态机实现的一个配置读取函数 UNIX基础知识--《APUE》第一章笔记
pygame做的贪吃蛇
飘啊飘 · 2011-12-19 · via 博客园 - 飘啊飘
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
'''
Created on 2011-11-25
author: shiweifu
  mail: shiweifu@126.com
'''
import struct
import sys
import os
import pygame
from random import randint


import pygame.sprite as Sprite
from pygame.locals import *

debug = True

FONT_NAME = "Arial"
FONT_SIZE = 25

SCREENRECT = Rect(0, 0, 640, 480)
RECT_LEN = 15

class Box:
    def __init__(self, x, y, f):
        self.x = x
        self.y = y
        self.flag = f
        if self.flag:
            self.surface = get_font_surface("b")
        else:
            self.surface = pygame.Surface((15,15))

    def __str__(self):
        return "x:%s y:%s" % (str(self.x),str(self.y))


class Link(object):
    
    def __init__(self, x, y, surface):
        self.x = x
        self.y = y
        self.surface = surface
    
    def __str__(self):
        return "x:%s y:%s" % (str(self.x),str(self.y))
        
class Player:
    def __init__(self,x,y):
        self.snake_body = []
        self.append(x,y)

    def append(self,x,y):
        l = Link(x,y,get_font_surface("s",(255,0,0)))        
        self.snake_body.append(l)

    def abc(self):
        print("test")
    
    def move_left(self):
        i = len(self.snake_body) - 1

        while i >= 1:
#            debug_output("i:"+str(i))
            self.snake_body[i].x = self.snake_body[i-1].x
            self.snake_body[i].y = self.snake_body[i-1].y
            i -= 1
        self.snake_body[0].x -= 1        


    def move_right(self):
        i = len(self.snake_body) - 1

        while i >= 1:
#            debug_output("i:"+str(i))
            self.snake_body[i].x = self.snake_body[i-1].x
            self.snake_body[i].y = self.snake_body[i-1].y
            i -= 1
        self.snake_body[0].x += 1

    def move_up(self):
        i = len(self.snake_body) - 1

        while i >= 1:
            self.snake_body[i].x = self.snake_body[i-1].x
            self.snake_body[i].y = self.snake_body[i-1].y
            i -= 1
        self.snake_body[0].y -= 1
    
    def move_down(self):
        i = len(self.snake_body) - 1

        while i >= 1:
            self.snake_body[i].x = self.snake_body[i-1].x
            self.snake_body[i].y = self.snake_body[i-1].y
            i -= 1
        self.snake_body[0].y += 1

    def move(self,target):
        if target == "up":
            self.move_up()
        if target == "down":
            self.move_down()
        if target == "left":
            self.move_left()
        if target == "right":
            self.move_right() 
                
def get_font_surface(s,color = (0,255,0),font_size=25):
    font = pygame.font.SysFont(FONT_NAME,font_size)
    #color = (0,255,0)
    backcolor = (0,0,0)
    r = font.render(s,True,color,backcolor)
    return r

def create_map(width,height,rect_len):
    y = 0
    map_pos = []
    snake_map = []
    flag =True
    while y < height:
        x = 0        
        flag = False
        while x < width:        
            if x < rect_len or x+rect_len >= width:
                flag = True
            elif y < rect_len or y+rect_len >= height:
                flag = True
            else:
                flag = False

            snake_map.append(Box(x/rect_len,y/rect_len,flag))
            x += 15
        y += 15

    return snake_map

def debug_output(s, f=sys.stdout):
    if debug == True:
        f.write("debug info: " + s + "\n")
        f.flush()

class Game:
    
    WIDTH = 640
    HEIGHT = 480
    BOX_SIZE = 15
    
    def __init__(self, caption="CooooolSnake"):
    
        pygame.init()
        pygame.display.set_caption(caption)
        self.screen = pygame.display.set_mode(SCREENRECT.size, 0, 32)    
        pygame.mouse.set_visible(0)
        self.background = pygame.Surface(SCREENRECT.size)
        
        self.map_size = (Game.WIDTH/15-1,Game.HEIGHT/15-1)
        
        self.snake_map = create_map(Game.WIDTH,Game.HEIGHT,self.BOX_SIZE)
        
        self.stages = []
        
        self.__crate_player__()

    def get_new_pos(self):
        done = False
        pos = None
        while not done:
            x = randint(2,self.map_size[0]-1)
            y = randint(2,self.map_size[1]-2)

            for link in self.player.snake_body:
                if x == link.x and y == link.y:
                    continue
            pos = (x,y)
            done = True

        return pos
               

    def __crate_player__(self):
        x = randint(2,self.map_size[0]-1)
        y = randint(2,self.map_size[1]-2)
        self.player = Player(x,y)

    def is_over(self):
        head = self.player.snake_body[0]
        i = 0
        
        for link in self.player.snake_body[1:]:
            if head.x == link.x and head.y == link.y:
                return True 
        
        for box in self.snake_map:
            if box.flag:
                if head.x == box.x and head.y == box.y:
                    return True
        return False
        
    def update_snake(self):
        
        for link in self.player.snake_body:
            self.background.blit(link.surface,(link.x*Game.BOX_SIZE,link.y*Game.BOX_SIZE))
            
    def update_map(self):
        for box in self.snake_map:
            self.background.blit(box.surface,(box.x*Game.BOX_SIZE,box.y*Game.BOX_SIZE))

    def add_stage(self):
        x,y = self.get_new_pos()
        s = get_font_surface("s",(255,0,0))
        l = Link(x,y,s)
        self.stages.append(l)
    
    def update_stage(self):
        head = self.player.snake_body[0]
        for stage in self.stages:
            if stage.x == head.x and stage.y == head.y:
                self.player.append(stage.x,stage.y)
                self.stages.remove(stage)
                
        for stage in self.stages:
            self.background.blit(stage.surface,(stage.x*Game.BOX_SIZE,stage.y*Game.BOX_SIZE))

    def refresh_background(self):
        self.background.fill((0,0,0))
        self.update_map()
        self.update_snake()
        
        self.update_stage()
        
        self.screen.blit(self.background, (0, 0))
        
    def over(self):        
        logo = get_font_surface("YOU ARE LOST!",(0,0,255),60)
        self.background.blit(logo,(100,200))

        clock = pygame.time.Clock()
        done = False

        count = 0

        while not done:
            clock.tick(10)
            count += 1
            if count == 5:
                done = True         
            self.screen.blit(self.background, (0, 0))
            pygame.display.flip()
    
    def loop(self):
        pygame.display.update()
        clock = pygame.time.Clock()
        target = ["up","down","left","right"][randint(0,3)]
        
        count = 0
        stage_time = randint(0,50)

        
        done = False
        while not done:
            for e in pygame.event.get():
                if e.type == pygame.KEYDOWN:
                    keystate = pygame.key.get_pressed()
                    if keystate[K_UP]:
                        if target != "down":                        
                            target = "up"

                    elif keystate[K_DOWN]:
                        if target != "up":                        
                            target = "down"

                    elif keystate[K_LEFT]:
                        if target != "right":                        
                            target = "left"

                    elif keystate[K_RIGHT]:
                        if target != "left":                        
                            target = "right"
                            
                if e.type == QUIT:
                    done = True
            self.player.move(target)
            if count == stage_time:
                self.add_stage()
                stage_time = randint(0,50)
                count = 0
 
            if self.is_over():
                done = True
                
            clock.tick(10)
            
            count += 1
            
            self.refresh_background()
            
            if done:
                self.over()
            
            pygame.display.flip()

if __name__ == '__main__':
    game = Game()
    game.loop()
    pass