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年2月7日
    55
  • 电子元器件分销10亿俱乐部[通俗易懂]

    电子元器件分销10亿俱乐部[通俗易懂]2015中国本土元器件分销商10亿俱乐部成员:1、科通集团简介:科通集团创建于1995年,是中国最大的IC元器件分销商。总部设于深圳,并在香港和上海有分公司。科通为国内的OEM厂商、ODM厂商和EMS厂商提供范围广泛的电子元器件,其应用涉及无线通信、电信设备、企业网络、数字媒体、家庭娱乐、汽车电子、工业控制等等众多领域。销售额:2015年营收-约84亿元员工人数:约400人产品线:Xili…

    2022年6月22日
    72
  • ETH硬分叉降低了显卡矿机的挖矿收益吗?

    ETH硬分叉降低了显卡矿机的挖矿收益吗?“北京时间2019年3月1日凌晨3:52分,ETH完成了君士坦丁堡硬分叉升级。这场从2018年8月份就开始计划的硬分叉,几经波折,但最终是平稳顺利的。虽然对比其他主流币种轰轰烈烈的硬分叉,ETH这次硬分叉显得过于平淡,但还是有很多矿工朋友想知道它是否降低了ETH的挖矿收益,未来挖矿收益如何变化,显卡矿机未来的出路在哪里?”笔者根据最近一年的ETH挖矿难度、挖矿收益、币价等变化情况,做了一些…

    2022年6月9日
    44
  • centos创建samba共享_centos设置samba

    centos创建samba共享_centos设置samba这篇指南介绍了如何在CentOS7中配置匿名和安全的Samba服务器。Samba是一个开源/自由软件套件,提供无缝的文件和打印服务SMB/CIFS客户端.Samba是免费的,不像其他的SMB/CIFS的实现着,Samba允许在Linux/Unix服务器和给予Windows的客户端之间

    2022年9月25日
    0
  • pandas 0.23.4 :’pd.ewma’没有这个模块,改用`Series.ewm` 或 降低版本到 pandas 0.21.0

    pandas 0.23.4 :’pd.ewma’没有这个模块,改用`Series.ewm` 或 降低版本到 pandas 0.21.0问题:在进行画出指数平滑移动平均线,遇到如下问题:#pd.ewma(com=None,span=one)#指数平均线。com:数据;span:时间间隔AttributeError:module’pandas’hasnoattribute’ewma’解决办法:在pandas0.23.4版本中,已经不存在这种方法,回退到之前版本pandas0.21.0就一切完美…

    2025年7月12日
    0
  • uva 10825 – Anagram and Multiplication(暴力)

    uva 10825 – Anagram and Multiplication(暴力)

    2022年1月9日
    53

发表回复

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

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