zoukankan      html  css  js  c++  java
  • Python贪吃蛇

    #基于pygame设计的贪吃蛇。
    import
    pygame,sys,random SCREENx = 900 SCREENy = 900 class Snake():
    #蛇类
    def __init__(self): self.dirction = pygame.K_RIGHT self.body = [] for x in range(2): self.addnode() def addnode(self): left, top = (0, 0) if self.body: left, top = (self.body[0].left, self.body[0].top) node = pygame.Rect(left, top, 25, 25) if self.dirction == pygame.K_LEFT: node.left -= 25 elif self.dirction == pygame.K_RIGHT: node.left += 25 elif self.dirction == pygame.K_UP: node.top -= 25 elif self.dirction == pygame.K_DOWN: node.top += 25 self.body.insert(0, node) def delnode(self): self.body.pop() def isdead(self): if self.body[0].x not in range(SCREENx): return True if self.body[0].y not in range(SCREENy): return True if self.body[0] in self.body[1:]: return True return False def move(self): self.addnode() self.delnode() def changedirection(self, curkey): LR = [pygame.K_LEFT, pygame.K_RIGHT] UD = [pygame.K_UP, pygame.K_DOWN] if curkey in LR + UD: if (curkey in LR) and (self.dirction in LR): return if (curkey in UD) and (self.dirction in UD): return self.dirction = curkey class Food:
    #食物类
    def __init__(self): self.rect = pygame.Rect(-25, 0, 25, 25) def remove(self): self.rect.x = -25 def set(self): if self.rect.x == -25: allpos = [] for pos in range(25, SCREENx - 25, 25): allpos.append(pos) self.rect.left = random.choice(allpos) self.rect.top = random.choice(allpos) #print(self.rect) def show_text(screen, pos, text, color, font_bold=False, font_size=60, font_italic=False): cur_font = pygame.font.SysFont("宋体", font_size) cur_font.set_bold(font_bold) cur_font.set_italic(font_italic) text_fmt = cur_font.render(text, 1, color) screen.blit(text_fmt, pos) def main(size=None): pygame.init() Ssize = (SCREENx, SCREENy) screen = pygame.display.set_mode(Ssize) pygame.display.set_caption('Snake') clock = pygame.time.Clock() scores = 0 isdead = False snake = Snake() food = Food() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == pygame.KEYDOWN: snake.changedirection(event.key) if event.key == pygame.K_SPACE and isdead: return main() screen.fill((10, 10, 10)) if not isdead: snake.move() for rect in snake.body: pygame.draw.rect(screen, (255, 255, 255), rect, 0) isdead = snake.isdead() if isdead: show_text(screen, (225, 400), 'press space to try again...', (225, 225, 225), False, 50) if food.rect == snake.body[0]: scores += 50 food.remove() snake.addnode() food.set() pygame.draw.rect(screen, (255, 165, 50), food.rect, 0) show_text(screen, (50, SCREENy-100), str(scores), (225, 225, 225)) pygame.display.update() clock.tick(15) if __name__ == '__main__': main()
  • 相关阅读:
    显卡,显卡驱动,nvcc, cuda driver,cudatoolkit,cudnn到底是什么?
    安装cuda之后没有安装nvidia显卡驱动可能会出现ImportError: libcuda.so.1: cannot open shared object file: No such file or directory
    ubuntu安装
    老男孩Linux查看硬盘使用个情况及其挂载点
    Anaconda使用conda activate激活环境出错CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
    Linux,ubuntu设置anaconda的环境变量
    Linux 磁盘管理
    anaconda路径改变后对其中文件的修改
    Linux 文件基本属性
    川藏游记发在别处的
  • 原文地址:https://www.cnblogs.com/Rhythm-/p/9275481.html
Copyright © 2011-2022 走看看