Pygame 错误 Rect 参数无效
Pygame eroor Rect arguement is invalid
在这段代码中,我在网上写了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()
Error code in pcharm
请帮忙,因为我需要完成这段贪吃蛇游戏的代码,因为代码即将完成,所以请帮忙。提前致谢
问题是当你吃苹果时,你的分数会增加并达到1。
coordins
包含 ["None, "None"]
.
然后 coordins[0]
更新到蛇位置,而不是 coordins[1]
在此之后你循环 coordins
.
您可以毫无问题地绘制 coordins[0]
,因为您将它从 "None"
替换为坐标。
然而 coordins[1]
仍然是 "None"
.
snake_xo
变成"N"
,snake_yo
变成"o"
,因为它们是"None"
.
的第一个字符
然后你尝试绘制矩形,但坐标是 str
,不是 int,它崩溃了。
为了解决这个问题,您需要完全更新 coordins
而不仅仅是一个元素。
希望这对您有所帮助
在这段代码中,我在网上写了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()
Error code in pcharm 请帮忙,因为我需要完成这段贪吃蛇游戏的代码,因为代码即将完成,所以请帮忙。提前致谢
问题是当你吃苹果时,你的分数会增加并达到1。
coordins
包含 ["None, "None"]
.
然后 coordins[0]
更新到蛇位置,而不是 coordins[1]
在此之后你循环 coordins
.
您可以毫无问题地绘制 coordins[0]
,因为您将它从 "None"
替换为坐标。
然而 coordins[1]
仍然是 "None"
.
snake_xo
变成"N"
,snake_yo
变成"o"
,因为它们是"None"
.
的第一个字符
然后你尝试绘制矩形,但坐标是 str
,不是 int,它崩溃了。
为了解决这个问题,您需要完全更新 coordins
而不仅仅是一个元素。
希望这对您有所帮助