求助大侠帮忙将游戏中的蛇速度调快一点,谢谢!
import pygamefrom pygame.locals import *
import random
pygame.init()
clock = pygame.time.Clock()
Directions = [(1, 0), (0, 1), (-1, 0), (0, -1)]#RIGHT, UP, LEFT, DOWN
#bg color sets:
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
#elements color sets:
GRAY = (128, 128, 128) #Gray for walls
GREEN = (0, 255, 0) #Green for food
#Snake use White also
#state of block:
AVAILABLE = 0
WALL = 1
FOOD = 2
def draw(blocks, color):
if type(blocks) != list:
blocks =
for (x, y) in blocks:
pygame.draw.rect(screen, color, (30*x+1, 30*(23-y)+1, 28, 28))
def vector_sum(vector_x, vector_y, sub=False):
if type(vector_x) != tuple or type(vector_y) != tuple:
raise TypeError('unsupported operand type(s) for +: \'{}\' and \'{}\''.format(type(vector_x),type(vector_y)))
if len(vector_x) != len(vector_y):
raise Exception('different dimensions')
return tuple(vector_x-vector_yif sub else vector_x+vector_y for i in range(len(vector_x)))
class Map:
def __init__(self, width=24, height=24, wall_set=None):
self.blocks = []
for i in range(height):
self.blocks.append([])
for j in range(width):
self.blocks.append(AVAILABLE)
self.size = width, height
if not wall_set:
for i in range(height):
if i == 0 or i == height-1:
self.blocks = * width
else:
self.blocks = + *(width-2) +
else:
for wall in wall_set:
self.blocks]] = WALL
update_blocks = []
for i in range(height):
for j in range(width):
if self.blocks != WALL:
update_blocks.append((j, i))
self.update_area = [(30*x+1, 30*(23-y)+1, 28, 28)for (x, y) in update_blocks]
def __getitem__(self, index):
width, height = self.size
if index < 0 or index >= width or index < 0 or index >= height:
return WALL
return self.blocks]]
def __setitem__(self, index, value):
width, height = self.size
if index < 0 or index >= width or index < 0 or index >= height:
return
self.blocks]] = value
def clear(self):
width, height = self.size
for x in range(width):
for y in range(height):
if self != WALL:
draw((x, y), BLACK)
class Snake:
def __init__(self, length=3):
self.passed = None
def available_head_check(pos):
if game_map == WALL:
return False
for direction in Directions:
sum_direction = (0, 0)
for i in range(3):
sum_direction = vector_sum(sum_direction, direction)
if game_map == WALL:
return False
return True
available_choices = list(filter(available_head_check, [(i, j)for i in range(24) for j in range(24)]))
head_pos = random.choice(available_choices)
self.blocks =
def available_body_check(pos):
return pos in available_choices and pos not in self.blocks
while length > 1:
choices = list(filter(lambda x:available_body_check(vector_sum(self.blocks[-1], x, sub=True)), Directions))
temp_head = random.choice(choices)
if len(self.blocks) == 1:
self.head = temp_head
self.blocks.append(vector_sum(self.blocks[-1], temp_head, sub=True))
length -= 1
def move(self, head):
if head == None or vector_sum(self.head, head) == (0, 0):
head = self.head
self.head = head
new_head_pos = vector_sum(self.blocks, head)
if game_map == WALL or new_head_pos in self.blocks:
global stage
stage = 2
return True
if game_map == FOOD:
self.blocks = + self.blocks
game_map = AVAILABLE
global food
food = None
else:
passed = self.blocks[-1]
draw(passed, BLACK)
self.blocks = + self.blocks[:-1]
def draw(self):
draw(self.blocks, WHITE)
def available_food_check(pos):
if snake:
if pos in snake.blocks:
return False
if game_map == WALL:
return False
dead = 0
for direction in Directions:
temp = vector_sum(pos, direction)
if game_map == WALL:
dead += 1
if snake and temp in snake.blocks:
dead += 1
if dead <= 2:
return True
else:
return False
title_font = pygame.font.Font(None, 60)
font = pygame.font.Font(None, 24)
title = title_font.render('Snake Game', True, WHITE)
display_helps = [font.render('Wall', True, WHITE),
font.render('Food', True, WHITE),
font.render('Snake', True, WHITE)]
control_helps = to move up', True, WHITE),
font.render('Press to move down', True, WHITE),
font.render('Press to move right', True, WHITE),
font.render('Press to move left', True, WHITE),
font.render('Press to pause', True, WHITE)]
start_help = font.render('Press to start', True, WHITE)
gameover = title_font.render('GAME OVER', True, WHITE)
screen = pygame.display.set_mode((720, 720))
pygame.display.set_caption('Snake Game Demo')
game_map = Map()
stage = 0 #0 for beginning; 1 for gaming; 2 for end
snake = None
food = None
head = None
pause = False
screen.fill(BLACK)
screen.blit(title, (360-title.get_width()//2, 150))
for i in range(len(control_helps)):
screen.blit(control_helps, (100, 264+48*i-control_helps.get_height()//2))
display_colors =
for i in range(len(display_helps)):
pygame.draw.rect(screen, display_colors, (501, 266+80*i, 28, 28))
screen.blit(display_helps, (560, 280+80*i-display_helps.get_height()//2))
screen.blit(start_help, (360-start_help.get_width()//2, 560))
pygame.display.flip()
t = 0
while True:
if stage == 1 and not food:
available_choices = list(filter(available_food_check, [(i, j) for i in range(24) for j in range(24)]))
food = random.choice(available_choices)
game_map = FOOD
draw(food, GREEN)
for event in pygame.event.get():
if event.type == QUIT:
exit()
if event.type == KEYDOWN:
key = event.key
if stage == 1:
if not pause:
if key == K_RIGHT or key == K_d:
head = Directions
elif key == K_UP or key == K_w:
head = Directions
elif key == K_LEFT or key == K_a:
head = Directions
elif key == K_DOWN or key == K_s:
head = Directions
if key == K_ESCAPE:
pause = not pause
if stage == 0:
if key == K_RETURN:
game_map.clear()
screen.fill(BLACK)
snake = Snake()
snake.draw()
for i in range(24):
for j in range(24):
if game_map == WALL:
draw((i, j), GRAY)
pygame.display.flip()
stage = 1
if stage == 1:
if pause:
continue
if not snake.move(head):
head = None
snake.draw()
pygame.display.flip()
clock.tick(1.7)
if stage == 2:
screen.blit(gameover, (360-gameover.get_width()//2, 240))
screen.blit(start_help, (360-start_help.get_width()//2, 480))
pygame.display.flip()
food = None
stage = 0 要增加蛇在游戏中的速度,你只需要调整`pygame.time.Clock().tick()`函数中的参数,以提高帧率。例如,找到以下代码片段:
clock = pygame.time.Clock()
# 这里调整游戏速度,例如:
clock.tick(10)
将 `clock.tick(10)` 中的参数 `10` 增大,例如 `15` 或 `20`,这样蛇的速度就会变快。请记住,数值越大,蛇移动得越快。通过这种方式,你可以调整游戏速度以适应你的需要。完成后的代码片段应该看起来像这样:
clock = pygame.time.Clock()
# 调整此值以改变游戏速度
clock.tick(20)
请将上述代码更新到你的游戏循环中,以调整蛇的速度。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 得使用代码格式提交代码,否则一些字符会被吃掉。 小甲鱼的二师兄 发表于 2024-12-1 14:15
得使用代码格式提交代码,否则一些字符会被吃掉。
如何使用代码格式提交代码? 本帖最后由 瓜提胖 于 2024-12-2 16:40 编辑
小甲鱼303 发表于 2024-12-1 18:11
如何使用代码格式提交代码?
这样子
或者使用 瓜提胖 发表于 2024-12-2 16:37
或者使用
谢谢
import pygame
from pygame.locals import *
import random
pygame.init()
clock = pygame.time.Clock()
Directions = [(1, 0), (0, 1), (-1, 0), (0, -1)]#RIGHT, UP, LEFT, DOWN
#bg color sets:
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
#elements color sets:
GRAY = (128, 128, 128) #Gray for walls
GREEN = (0, 255, 0) #Green for food
#Snake use White also
#state of block:
AVAILABLE = 0
WALL = 1
FOOD = 2
def draw(blocks, color):
if type(blocks) != list:
blocks =
for (x, y) in blocks:
pygame.draw.rect(screen, color, (30*x+1, 30*(23-y)+1, 28, 28))
def vector_sum(vector_x, vector_y, sub=False):
if type(vector_x) != tuple or type(vector_y) != tuple:
raise TypeError('unsupported operand type(s) for +: \'{}\' and \'{}\''.format(type(vector_x),type(vector_y)))
if len(vector_x) != len(vector_y):
raise Exception('different dimensions')
return tuple(vector_x-vector_yif sub else vector_x+vector_y for i in range(len(vector_x)))
class Map:
def __init__(self, width=24, height=24, wall_set=None):
self.blocks = []
for i in range(height):
self.blocks.append([])
for j in range(width):
self.blocks.append(AVAILABLE)
self.size = width, height
if not wall_set:
for i in range(height):
if i == 0 or i == height-1:
self.blocks = * width
else:
self.blocks = + *(width-2) +
else:
for wall in wall_set:
self.blocks]] = WALL
update_blocks = []
for i in range(height):
for j in range(width):
if self.blocks != WALL:
update_blocks.append((j, i))
self.update_area = [(30*x+1, 30*(23-y)+1, 28, 28)for (x, y) in update_blocks]
def __getitem__(self, index):
width, height = self.size
if index < 0 or index >= width or index < 0 or index >= height:
return WALL
return self.blocks]]
def __setitem__(self, index, value):
width, height = self.size
if index < 0 or index >= width or index < 0 or index >= height:
return
self.blocks]] = value
def clear(self):
width, height = self.size
for x in range(width):
for y in range(height):
if self != WALL:
draw((x, y), BLACK)
class Snake:
def __init__(self, length=3):
self.passed = None
def available_head_check(pos):
if game_map == WALL:
return False
for direction in Directions:
sum_direction = (0, 0)
for i in range(3):
sum_direction = vector_sum(sum_direction, direction)
if game_map == WALL:
return False
return True
available_choices = list(filter(available_head_check, [(i, j)for i in range(24) for j in range(24)]))
head_pos = random.choice(available_choices)
self.blocks =
def available_body_check(pos):
return pos in available_choices and pos not in self.blocks
while length > 1:
choices = list(filter(lambda x:available_body_check(vector_sum(self.blocks[-1], x, sub=True)), Directions))
temp_head = random.choice(choices)
if len(self.blocks) == 1:
self.head = temp_head
self.blocks.append(vector_sum(self.blocks[-1], temp_head, sub=True))
length -= 1
def move(self, head):
if head == None or vector_sum(self.head, head) == (0, 0):
head = self.head
self.head = head
new_head_pos = vector_sum(self.blocks, head)
if game_map == WALL or new_head_pos in self.blocks:
global stage
stage = 2
return True
if game_map == FOOD:
self.blocks = + self.blocks
game_map = AVAILABLE
global food
food = None
else:
passed = self.blocks[-1]
draw(passed, BLACK)
self.blocks = + self.blocks[:-1]
def draw(self):
draw(self.blocks, WHITE)
def available_food_check(pos):
if snake:
if pos in snake.blocks:
return False
if game_map == WALL:
return False
dead = 0
for direction in Directions:
temp = vector_sum(pos, direction)
if game_map == WALL:
dead += 1
if snake and temp in snake.blocks:
dead += 1
if dead <= 2:
return True
else:
return False
title_font = pygame.font.Font(None, 60)
font = pygame.font.Font(None, 24)
title = title_font.render('Snake Game', True, WHITE)
display_helps = [font.render('Wall', True, WHITE),
font.render('Food', True, WHITE),
font.render('Snake', True, WHITE)]
control_helps = to move up', True, WHITE),
font.render('Press to move down', True, WHITE),
font.render('Press to move right', True, WHITE),
font.render('Press to move left', True, WHITE),
font.render('Press to pause', True, WHITE)]
start_help = font.render('Press to start', True, WHITE)
gameover = title_font.render('GAME OVER', True, WHITE)
screen = pygame.display.set_mode((720, 720))
pygame.display.set_caption('Snake Game Demo')
game_map = Map()
stage = 0 #0 for beginning; 1 for gaming; 2 for end
snake = None
food = None
head = None
pause = False
screen.fill(BLACK)
screen.blit(title, (360-title.get_width()//2, 150))
for i in range(len(control_helps)):
screen.blit(control_helps, (100, 264+48*i-control_helps.get_height()//2))
display_colors =
for i in range(len(display_helps)):
pygame.draw.rect(screen, display_colors, (501, 266+80*i, 28, 28))
screen.blit(display_helps, (560, 280+80*i-display_helps.get_height()//2))
screen.blit(start_help, (360-start_help.get_width()//2, 560))
pygame.display.flip()
t = 0
while True:
if stage == 1 and not food:
available_choices = list(filter(available_food_check, [(i, j) for i in range(24) for j in range(24)]))
food = random.choice(available_choices)
game_map = FOOD
draw(food, GREEN)
for event in pygame.event.get():
if event.type == QUIT:
exit()
if event.type == KEYDOWN:
key = event.key
if stage == 1:
if not pause:
if key == K_RIGHT or key == K_d:
head = Directions
elif key == K_UP or key == K_w:
head = Directions
elif key == K_LEFT or key == K_a:
head = Directions
elif key == K_DOWN or key == K_s:
head = Directions
if key == K_ESCAPE:
pause = not pause
if stage == 0:
if key == K_RETURN:
game_map.clear()
screen.fill(BLACK)
snake = Snake()
snake.draw()
for i in range(24):
for j in range(24):
if game_map == WALL:
draw((i, j), GRAY)
pygame.display.flip()
stage = 1
if stage == 1:
if pause:
continue
if not snake.move(head):
head = None
snake.draw()
pygame.display.flip()
clock.tick(1.7)
if stage == 2:
screen.blit(gameover, (360-gameover.get_width()//2, 240))
screen.blit(start_help, (360-start_help.get_width()//2, 480))
pygame.display.flip()
food = None
stage = 0
这是更改后的代码 9i 斗
页:
[1]