python贪吃蛇代码_python 贪吃蛇

python贪吃蛇代码_python 贪吃蛇importpygamefrompygame.localsimport*fromsysimportexitfrompygame.colorimportTHECOLORSimportrandomcellsize=20width=640height=480cellw=int(width/cellsize)cellh=int(height/ce…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

import pygame
from pygame.locals import *
from sys import exit
from pygame.color import THECOLORS
import random
import sys
cellsize = 20
width = 640
height = 480
cellw = int(width/cellsize)
cellh = int(height/cellsize)
speed = 15
right = 'right'
left = 'left'
up = 'up'
down = 'down'
rctwidth = 0
rctwidth2 = 1
color = 0,255,0
color3 = 0,250,0
direction = right
ap = { 'x': 0, 'y': 0}
color1 = 199, 21, 133
count = 3

def main():
    global speed, speedCLOCK,ap,count, screen
    pygame.init()
    count = 3
    speedCLOCK = pygame.time.Clock()
    pygame.display.set_caption("Hello, World!")
    screen = pygame.display.set_mode((width, height))
    screen.fill((0, 0, 0))
    pygame.display.flip()
    while True:
        for event in pygame.event.get():
            keys = pygame.key.get_pressed()
            if keys[K_BACKSPACE]:
                pygame.quit()
                sys.exit()
        ap = randomplace()
        snakeoriginalbody()
        gameover()
        print(randomplace())
        return

def snakeoriginalbody():
    k = 10
    startx = random.randint( 5, cellw - 6)
    starty = random.randint( 5, cellh - 6)
    snake = [ { 'x': startx, 'y': starty},
              { 'x': startx - 1, 'y': starty },
              { 'x': startx - 2, 'y': starty }]
    while True:
        movdirection( snake)
        drawsnake(snake)
        pygame.display.update()
        if snake[0]['x'] < 0 or snake[0]['x'] == cellw :
            return
        if snake[0]['y'] < 0 or snake[0]['y'] ==cellh :
            return
        k = count-1
        while k >1 :
            if snake[0]['x'] == snake[k]['x'] and snake[0]['y'] == snake[k]['y']:
                return
            k -= 1

def movdirection( snake):
    global direction,ap,count
    keys = pygame.key.get_pressed()
    if keys[K_RIGHT]:
        direction = right
    if keys[K_LEFT]:
        direction = left
    if keys[K_UP]:
        direction = up
    if keys[K_DOWN]:
        direction = down
    if keys[K_BACKSPACE]:
        pygame.quit()
        sys.exit()
    if direction == right :
        newsnkehead = { 'x': snake[0]['x'] + 1, 'y': snake[0]['y']}
    if direction == left :
        newsnkehead = { 'x': snake[0]['x'] - 1, 'y': snake[0]['y']}
    if direction == up :
        newsnkehead = { 'x': snake[0]['x'], 'y': snake[0]['y'] - 1}
    if direction == down :
        newsnkehead = { 'x': snake[0]['x'], 'y': snake[0]['y'] + 1}
    snake.insert(0, newsnkehead)
    if snake[0]['x'] == ap['x'] and snake[0]['y'] == ap['y']:
        ap = randomplace()
        count +=1
        return
    else:
        snake.pop(count)

def drawsnake(snake):
    global screen
    screen = pygame.display.set_mode((width, height))
    for coord in snake:
            pos = coord['x'] * cellsize, coord['y'] * cellsize, cellsize, cellsize
            pygame.draw.rect(screen, color, pos, rctwidth)
            pygame.draw.rect(screen, color3, pos, rctwidth2)
            pos = ap['x'] * cellsize, ap['y'] * cellsize,cellsize,cellsize
            pygame.draw.rect(screen, color1, pos, rctwidth)  
            over = pygame.font.SysFont('arial', 60)
            textImage = over.render("socre %.d" % (count - 3), True, color)
            screen.blit(textImage, (450, 60))
    over = pygame.font.SysFont('arial', 60)
    textImage = over.render("socre %.d" % (count - 3), True, color)
    screen.blit(textImage, (450, 60))
    speedCLOCK.tick(speed)
    pygame.display.flip()

def randomplace():
    return{ 'x': random.randint( 0, cellw - 1), 'y': random.randint( 0, cellh - 1)}
def gameover():
    screen = pygame.display.set_mode((width, height))
    over = pygame.font.SysFont('arial', 60)
    over2 = pygame.font.SysFont('arial',30)
    textImage = over.render("game over", True, color)
    screen.blit(textImage, (200, 200))
    textImage2 = over2.render("press L-CTRL to restar", True, color)
    screen.blit(textImage2, (200, 300))
    pygame.display.update()
    while True:
        for event in pygame.event.get():
            keys = pygame.key.get_pressed()
            if  keys[K_BACKSPACE]:
                pygame.quit()
                sys.exit()
            if  keys[K_LCTRL]:
                main()
main()

自学python 第一次尝试编程贪吃蛇,尚有不足之处!还要继续学习!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/169544.html原文链接:https://javaforall.net

(0)
上一篇 2022年8月10日 下午11:46
下一篇 2022年8月10日 下午11:46


相关推荐

  • fastcgi java_fastcgi是什么

    fastcgi java_fastcgi是什么1.概述FastCGI全称快速通用网关接口(FastCommonGatewayInterface)。FastCGI实际上是增加了一些扩展功能的CGI、是CGI的改进,描述了客户端和Web服务器程序之间传输数据的一种标准。FastCGI像是一个常驻(long-live)型的CGI,FastCGI致力于减少Web服务器与CGI程序之间进行互动的开销,从而使Web服务器可以同时处理更多的…

    2022年7月27日
    7
  • PDF补丁丁( PDFPatcher.)

    PDF补丁丁( PDFPatcher.)PDF 补丁丁是一个用于修改 PDF 文件信息的工具 它具有以下功能 修改 PDF 信息 修改文档属性 页码编号 页面链接 页面尺寸 删除自动打开网页等动作 去除复制及打印限制 设置阅读器初始模式 贴心 PDF 书签编辑器 可批量修改 PDF 书签属性 颜色 样式 目标页码 缩放比例等 在书签中执行查找替换 支持正则表达式及 XPath 匹配 生成 PDF 书签 无需手工输入 自动识别正文标题或目录 为 PDF 文档生成书签 制作 PDF 文件 合并已有 PDF 文件或图片 生成新的 PDF 文件 可挂上书签 拆分或合并 PDF 文件

    2026年3月18日
    3
  • 5 分钟在本地跑起你的专属“龙虾” OpenClaw AI 助手

    5 分钟在本地跑起你的专属“龙虾” OpenClaw AI 助手

    2026年3月17日
    2
  • ios学习7_iPhone屏幕尺寸、分辨率及适配[通俗易懂]

    ios学习7_iPhone屏幕尺寸、分辨率及适配[通俗易懂]1.iPhone尺寸规格设备iPhone宽Width高Height对角线Diagonal逻辑分辨率(point)ScaleFactor设备分辨率(pixel)PPI3GS2.4inches

    2022年5月14日
    44
  • Latex中单引号,双引号「建议收藏」

    Latex中单引号,双引号「建议收藏」写论文时候,需要写出英文的单引号和双引号单引号就是”这个符号双引号的话,如果输入两个英文输入下的””,比如”paper”,LaTeX编译显示的话就是两个向下的引号。正确应该是第一个向上方向的引号用“这个符号,就是输入两个下图的键盘上的符号,所以正确应该为:“paper”…

    2022年6月25日
    35
  • SpringBoot——创建SpringBoot项目HelloWorld

    SpringBoot——创建SpringBoot项目HelloWorldSpringBoot——创建SpringBoot项目HelloWorld

    2022年4月23日
    54

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号