首页 > 解决方案 > Pygame eroor Rect 论证无效

问题描述

在这段代码中,我在网上写了 153,我想在其中绘制矩形,但代码说矩形参数无效但参数有效我不知道为什么会发生错误,因为我不明白为什么会发生这个问题有人可以帮忙吗???

import pygame
import random
import time


def basic_defaults():
    pygame.init()
    snake_x = 200
    snake_y = 175
    font_for_game_title = pygame.font.Font(None, 36)
    screen = pygame.display.set_mode((800, 600)) #Making Screen for the game
    screen.blit(font_for_game_title.render("Snake Game", True, (0, 255, 255)), (300, 10))# Making title appear
    Clock = pygame.time.Clock()
    x=2
    counter = 0
    score = 0
    coordins = []
    eaten = 0
    return screen, snake_x, snake_y, Clock, x, counter, score, coordins, eaten


def snake_movements(x):
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                x=1
            elif event.key == pygame.K_DOWN:
                x=2
            elif event.key == pygame.K_LEFT:
                x=3
            elif event.key == pygame.K_RIGHT:
                x=4
            else:
                pass
    return x


def food():
    food_x = random.randint(20, 780)
    food_y = random.randint(20, 280)
    return food_x, food_y










def game_loop():
    screen, snake_x, snake_y, clocko, x, counter, score, coordins, eaten = basic_defaults()
    running = True
    while (running):

        #for moving of snake in the game [code starts]

        screen.fill((0, 0, 0))
        x=snake_movements(x)
        if x == 1:
            snake_y -= 20
        elif x == 2:
            snake_y += 20
        elif x == 3:
            snake_x -= 20
        elif x == 4:
            snake_x += 20
        pygame.draw.rect(screen, (102, 255, 51), (snake_x, snake_y, 10, 10), 0)

        #for moving of snake in the game [code ends]

        #for food appering and disappearing after certain amount of time[code starts]

        timesec = 35       #in secs
        if (counter)%timesec == 0:
            food_x,food_y=food()
        else:
            pass
        pygame.draw.rect(screen, (0, 255, 255), (food_x, food_y, 11, 11), 0)
        counter += 1

        # for food appering and disappearing after certain amount of time[code ends]

        # for detecting snake is eating food or not [code starts]

        if snake_x + 10 >= food_x and snake_x + 10 <=  food_x + 10 and snake_y >= food_y and snake_y <= food_y + 10:
            food_x, food_y = food()
            counter = 1
            score += 1
            eaten = 1
        elif snake_x >= food_x and snake_x <= food_x + 10 and snake_y >= food_y and snake_y <= food_y + 10:
            food_x, food_y = food()
            counter = 1
            score += 1
            eaten = 1
        elif snake_x >= food_x and snake_x <= food_x + 10 and snake_y + 10 >= food_y and snake_y + 10 <= food_y + 10:
            food_x, food_y = food()
            counter = 1
            score += 1
            eaten = 1

        elif snake_x + 10 >= food_x and snake_x + 10 <= food_x + 10 and snake_y + 10 >= food_y and snake_y + 10 <= food_y + 10:
            food_x, food_y = food()
            counter = 1
            score += 1
            eaten = 1

        else:
            eaten = 0
            pass

        # for detecting snake is eating food or not [code ends]

        # to determine boundaries for snake to not go beyond [code starts]

        if snake_x > 790 or snake_x < 0:
            running = False
        if snake_y < 0 or snake_y > 590:
            running = False

        # to determine boundaries for snake to not go beyond [code ends]

        # code to calculate score [code starts]

        font_for_score = pygame.font.Font(None, 25)
        screen.blit(font_for_score.render("Score: ", True, (0, 147, 255)), (645, 10))
        screen.blit(font_for_score.render(str(score), True, (0, 147, 255)), (705, 10))

        # code to calculate score [code ends]

        # code to extend tail of snake [code starts]
        if eaten == 1:
            coordins = []
            for i in range(score + 1):
                coordins.append("None")
        if score > 0:
            dolo = score + 1
            dhillo = (counter-1)%dolo
            coordins[dhillo] = [snake_x, snake_y]
            for i in coordins:
                snake_xo = i[0]
                snake_yo = i[1]
                pygame.draw.rect(screen, (102, 255, 51), (snake_xo, snake_yo, 10, 10), 0)        

        # code to extend tail of snake [code ends]



        pygame.display.update()
        time.sleep(1)
        clocko.tick(30)
game_loop() 

pcharm 中的错误代码 请帮忙,因为我需要完成蛇游戏的代码,因为代码接近完成,所以请帮忙。提前致谢

标签: python-3.xpygamerect

解决方案


问题是当你吃苹果时,你的分数会增加并达到 1.
coordinscontains ["None, "None"]
然后coordins[0]更新到蛇的位置,但不是coordins[1]
在这之后你循环coordins。您可以毫无问题
地绘制,因为您将其替换为坐标。 不过依旧。become和become ,因为它们是第一个字符。 然后你尝试绘制矩形,但坐标是,而不是 int,它会崩溃。 为了解决这个问题,您不仅需要完全更新一个元素。 希望这有帮助coordins[0]"None"
coordins[1]"None"
snake_xo"N"snake_yo"o""None"
str
coordins


推荐阅读