如何解决谁能告诉我如何在我的代码中增加蛇的大小?
下面是我的代码。每次我尝试增加长度时,它似乎都没有增加,当我运行游戏时。
当我运行游戏时,它有时会使蛇的头部闪烁/闪烁某些次,当蛇吃掉食物时,它仍然不会增加长度,而是在某些时候很少眨眼
我已经附加了代码和我应用的逻辑。请问这个问题有什么解决办法吗?
这是我的逻辑:
import pygame
import time
import sys
import random
a = print("1) Easy")
b = print("2) Medium")
c = print("3) Hard")
while True:
difficulty = input("Enter Difficulty Level: ")
if difficulty == "1":
speed = 5
break
elif difficulty == "2":
speed = 6
break
elif difficulty == "3":
speed = 8
else:
print("Choose from Above Options Only!")
# Initialise Game
pygame.init()
clock = pygame.time.Clock()
# Screen and Window Size:
screen_width = 800
screen_height = 700
screen = pygame.display.set_mode((screen_width,screen_height))
caption = pygame.display.set_caption("Snake Game")
icon = pygame.image.load("snake.png")
pygame.display.set_icon(icon)
# Colors
red = (255,0)
blue = (0,255)
green = (0,255,0)
white = (255,255)
# Snake Editing
x1 = 350
y1 = 300
snake = pygame.Rect([x1,y1,20,20])
x1_change = 0
y1_change = 0
snake_size = 15
snk_list = []
snk_length = 1
# Snake Food
food_x = random.randint(30,screen_width - 40)
food_y = random.randint(30,screen_height - 40)
food_height = 15
food_width = 15
# Game State
game_over = True
# Game over
font = pygame.font.SysFont("freelansbold.tff",64)
# score Counter
score = 0
score_font = pygame.font.SysFont("chiller",50)
# TO INCREASE SNAKE LENGTH LOGIC:
def plot_snake(gameWindow,color,snk_list,snake_size):
for x,y in snk_list:
pygame.draw.rect(gameWindow,[x,y,snake_size,snake_size])
def game_over_text(text,color):
x = font.render(text,True,(240,0))
screen.blit(x,[screen_width//2 - 135,screen_height//2 - 25])
def score_show():
text = score_font.render("score: " + str(score),(255,255))
screen.blit(text,(20,10))
def main_loop():
global x1,x1_change,y1_change,game_over,food_x,food_y,score,speed,snk_length
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# User Input
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT or event.key == pygame.K_a:
x1_change = speed * -1
y1_change = 0
elif event.key == pygame.K_RIGHT or event.key == pygame.K_d:
x1_change = speed
y1_change = 0
elif event.key == pygame.K_UP or event.key == pygame.K_w:
y1_change = speed * -1
x1_change = 0
elif event.key == pygame.K_DOWN or event.key == pygame.K_s:
y1_change = speed
x1_change = 0
# Game Over Checking
if x1 >= screen_width or x1 < 0 or y1 >= screen_height or y1 < 0:
game_over = False
x1 += x1_change
y1 += y1_change
if abs(x1 - food_x) < 7 and abs(y1 - food_y) < 7:
score += 1
food_x = random.randint(30,screen_width - 40)
food_y = random.randint(30,screen_height - 40)
speed += 0.2
snk_length += 5
# Drawing On Screen
screen.fill((0,0))
pygame.draw.rect(screen,red,[x1,15,15])
pygame.draw.rect(screen,green,[food_x,food_width,food_height])
score_show()
pygame.display.flip()
#SNAKE LENGTH LOGIC
head = []
head.append(x1)
head.append(y1)
snk_list.append(head)
if len(snk_list) > snk_length:
del snk_list[0]
plot_snake(screen,snake_size)
# Final Initialisation
pygame.display.flip()
clock.tick(70)
# Main Game Loop
while game_over:
main_loop()
# Game_Over
screen.fill((0,0))
game_over_text("Game Over!!!",0))
pygame.display.flip()
time.sleep(2)
pygame.quit()
quit()
解决方法
蛇的移动距离必须是“snake_size”:
def main_loop():
# [...]
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# User Input
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT or event.key == pygame.K_a:
x1_change = -snake_size
y1_change = 0
elif event.key == pygame.K_RIGHT or event.key == pygame.K_d:
x1_change = snake_size
y1_change = 0
elif event.key == pygame.K_UP or event.key == pygame.K_w:
y1_change = -snake_size
x1_change = 0
elif event.key == pygame.K_DOWN or event.key == pygame.K_s:
y1_change = snake_size
x1_change = 0
但是使用 pygame.time.Clock
来控制每秒帧数,从而控制游戏速度。
def main_loop():
# [...]
clock.tick(speed)
使用 pygame.Rect
和 collidrect
找出蛇和食物的碰撞。另见How to detect collisions between two rectangular objects or images in pygame。检测到碰撞时增加 snk_length
:
def main_loop():
# [...]
global snk_length
# [...]
snake_rect = pygame.Rect(x1,y1,snake_size,snake_size)
food_rect = pygame.Rect(food_x,food_y,snake_size)
if snake_rect.colliderect(food_rect):
snk_length += 1
score += 1
food_x = random.randint(30,screen_width - 40)
food_y = random.randint(30,screen_height - 40)
speed += 1
按照How do I chain the movement of a snake's body?的说明进行操作。将蛇的新头部位置放在snk_list
的头部,但删除尾部的元素:
def main_loop():
# [...]
#SNAKE LENGTH LOGIC
snk_list.insert(0,[x1,y1])
if len(snk_list) > snk_length:
del snk_list[-1]
完成main_loop
:
def main_loop():
global x1,x1_change,y1_change,game_over,food_x,score,speed,snk_list,snake_size
global snk_length
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# User Input
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT or event.key == pygame.K_a:
x1_change = -snake_size
y1_change = 0
elif event.key == pygame.K_RIGHT or event.key == pygame.K_d:
x1_change = snake_size
y1_change = 0
elif event.key == pygame.K_UP or event.key == pygame.K_w:
y1_change = -snake_size
x1_change = 0
elif event.key == pygame.K_DOWN or event.key == pygame.K_s:
y1_change = snake_size
x1_change = 0
# Game Over Checking
if x1 >= screen_width or x1 < 0 or y1 >= screen_height or y1 < 0:
game_over = False
x1 += x1_change
y1 += y1_change
snake_rect = pygame.Rect(x1,screen_height - 40)
speed += 1
# Drawing On Screen
screen.fill((0,0))
pygame.draw.rect(screen,red,15,15])
pygame.draw.rect(screen,green,[food_x,food_width,food_height])
score_show()
pygame.display.flip()
#SNAKE LENGTH LOGIC
snk_list.insert(0,y1])
if len(snk_list) > snk_length:
del snk_list[-1]
plot_snake(screen,snake_size)
# Final Initialisation
pygame.display.flip()
clock.tick(speed)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。