RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1005591
Accepted
DiHASTRO
DiHASTRO
Asked:2020-07-22 23:29:53 +0000 UTC2020-07-22 23:29:53 +0000 UTC 2020-07-22 23:29:53 +0000 UTC

蛇没有苹果。游戏。蟒蛇 3

  • 772

您好,代码如下:

import pygame
import random

win = pygame.display.set_mode((500, 500))

pygame.display.set_caption('Змейка')

clock = pygame.time.Clock()

x = 250
y = 250

appleWidth = 10
appleHeight = 10

width = 7
height = 7
speed = 7

direction = "right"

parts = []
apple = {}
snake = ''
isApple = False

def drawWindow():
    part = []
    win.fill((0, 0, 0))
    snake = pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
    part.append(snake)
    part.append(x)
    part.append(y)
    parts.append(part)
    pygame.display.update()

def move():
    global x, y

    if direction == 'right':
        x += speed
    elif direction == 'left':
        x -= speed
    elif direction == 'up':
        y -= speed
    elif direction == 'down':
        y += speed

    if x < 0:
        x = 500
    elif x > 500:
        x = 0
    if y < 0:
        y = 500
    elif y > 500:
        y = 0

def create_apple():
    global isApple 
    global apple

    xApple = random.randint(0, 500)
    yApple = random.randint(0, 500)
    for part in parts:
        if parts[part][1] == x and parts[part][2] == y:
            create_apple()
            break

    pygame.draw.rect(win, (0, 255, 0), (xApple, yApple, appleWidth, appleHeight))
    isApple = True
    apple['x'] = x
    apple['y'] = y

run = True
while run:
    pygame.time.delay(30)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    if isApple == False:
        create_apple()

    keys = pygame.key.get_pressed()

    move()

    if direction == 'right' or direction == 'left':
        if keys[pygame.K_w]:
            direction = "up"
        elif keys[pygame.K_s]:
            direction = "down"
    else:
        if keys[pygame.K_a]:
            direction = "left"
        elif keys[pygame.K_d]:
            direction = "right"


    drawWindow()

底线是:这些是我创造一条蛇的尝试。我正在尝试通过 create_apple() 函数创建一个蛇苹果。我运行代码,但苹果没有出现。可能是什么问题呢?如何解决?

python-3.x
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    Dareten
    2020-07-23T02:56:37Z2020-07-23T02:56:37Z

    代码中的错误不止一个,所以我只是重写了要点:

    import pygame
    import random
    
    win = pygame.display.set_mode((500, 500))
    
    pygame.display.set_caption('Змейка')
    
    clock = pygame.time.Clock()
    
    x = 250
    y = 250
    
    appleWidth = 10
    appleHeight = 10
    
    xApple = 0
    yApple = 0
    
    width = 7
    height = 7
    speed = 7
    
    direction = "right"
    
    parts = []
    apple = {}
    snake = ''
    isApple = False
    
    def drawWindow():
        try:
            a = pygame.draw.rect(win, (0, 0, 0), (parts[-1][1], parts[-1][2], width, height))
        except:
            pass
    
        part = []
        snake = pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
        part.append(snake)
        part.append(x)
        part.append(y)
        parts.append(part)
        pygame.display.update()
    
    def move():
        global x, y
    
        if direction == 'right':
            x += speed
        elif direction == 'left':
            x -= speed
        elif direction == 'up':
            y -= speed
        elif direction == 'down':
            y += speed
    
        if x < 0:
            x = 500
        elif x > 500:
            x = 0
        if y < 0:
            y = 500
        elif y > 500:
            y = 0
    
    def create_apple():
        global isApple, apple, win, xApple, yApple
    
        xApple = random.randint(0, 500)
        yApple = random.randint(0, 500)
    
        pygame.draw.rect(win, (0, 255, 0), (xApple, yApple, appleWidth, appleHeight))
        isApple = True
        apple['x'] = xApple
        apple['y'] = yApple
    
    run = True
    while run:
        pygame.time.delay(30)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
    
        if isApple == False:
            create_apple()
    
        keys = pygame.key.get_pressed()
    
        move()
    
        if direction == 'right' or direction == 'left':
            if keys[pygame.K_w]:
                direction = "up"
            elif keys[pygame.K_s]:
                direction = "down"
        else:
            if keys[pygame.K_a]:
                direction = "left"
            elif keys[pygame.K_d]:
                direction = "right"
    
    
        drawWindow()
        if abs(xApple - parts[-1][1]) < 7 and abs(yApple - parts[-1][2]) < 7:
            create_apple()
    

    让我们从绊脚石开始,这是行win.fill((0, 0, 0))。此功能在蛇每次移动前清屏,苹果也随之消失。为了解决这个问题,我将它更改为另一个函数,而不是完全清除它,而是绘制前一个蛇帧,但已经是黑色(该函数在尝试中被框起来,因为第一次访问数组时发生错误,前一帧还不存在)

    其次,现在苹果坐标是全局变量,这是必要的,这样吃苹果的检查在 body 中持续进行while run:,而不是在苹果创建函数本身中。检查本身查看蛇是否在距离苹果为 7 的圆内,如果是,则创建一个新苹果。我把删除旧苹果框架的功能留给了你,因为它与蛇的差别不大。

    否则,我看不到任何特殊问题(除了不准确地使用类似的 x 和 xApple 变量,因为你对它们感到困惑)

    • 2

相关问题

  • 如何在 Django 中正确解析 request.POST?

  • 如何处理实时屏幕图像,本质上是做视频截屏

  • 将类的实例添加到列表中

  • 整页截图

  • 'function' 类型的参数不可迭代(python3 套接字)

  • 如何根据单击的按钮在模板中显示内容?

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    根据浏览器窗口的大小调整背景图案的大小

    • 2 个回答
  • Marko Smith

    理解for循环的执行逻辑

    • 1 个回答
  • Marko Smith

    复制动态数组时出错(C++)

    • 1 个回答
  • Marko Smith

    Or and If,elif,else 构造[重复]

    • 1 个回答
  • Marko Smith

    如何构建支持 x64 的 APK

    • 1 个回答
  • Marko Smith

    如何使按钮的输入宽度?

    • 2 个回答
  • Marko Smith

    如何显示对象变量的名称?

    • 3 个回答
  • Marko Smith

    如何循环一个函数?

    • 1 个回答
  • Marko Smith

    LOWORD 宏有什么作用?

    • 2 个回答
  • Marko Smith

    从字符串的开头删除直到并包括一个字符

    • 2 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5