我正在用pygame编写蛇游戏,但系统仅提示我有一个黑色窗口,没有任何错误。
我检查了类似的问题,他们错误地使用了屏幕对象。但是,这里似乎不是这样,因为它只能创建一次?
知道我在做什么错吗?由于没有收到任何错误消息,因此我无法弄清为什么只显示黑屏。按键输入事件可以正常工作。
import pygame
import random
import sys
# Define colors
GRAY = (240, 240, 245)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLACK = (50, 50, 50)
# Field size
width = 400
height = 400
# Calculate the distance between two points
def calculate_distance(x1, x2, y1, y2):
return (x2 - x1)**2 + (y1 - y2)**2 ** 1/2
# Draws a label showing the current score in the top mid of the window
def draw_score_text(surface, text, font_size, x, y):
font_name = pygame.font.match_font('arial')
font = pygame.font.Font(font_name, font_size)
text_surface = pygame.font.render(text, True, WHITE)
text_rect = text_surface.get_rect()
text_rect.midtop = (x, y)
surf.blit(text_surface, text_rect)
class Snake(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((12,12))
self.image.fill(WHITE)
self.rect=self.image.get_rect()
self.rect.center = (100,100)
self.speedx=0
self.speedy=0
self.score=0
self.tail=[]
def update(self):
if self.score == len(self.tail):
self.tail.append((self.rect.x, self.rect.y))
else:
self.tail.append((self.rect.x, self.rect.y))
self.tail.pop(0)
self.rect.x += self.speedx
self.rect.y += self.speedy
keys = pygame.key.get_pressed()
if(keys[pygame.K_RIGHT] and self.speedx >=0):
self.speedx = 10
self.speedy = 0
if(keys[pygame.K_LEFT] and self.speedx <=0):
self.speedx = -10
self.speedy = 0
if(keys[pygame.K_UP] and self.speedy <=0):
self.speedx = 0
self.speedy = -10
if(keys[pygame.K_DOWN] and self.speedy >=0):
self.speedx = 0
self.speedy = 10
if self.rect.left >= width:
self.rect.left = 1
if self.rect.top >= height:
self.rect.top = 0
if self.rect.bottom <= 0:
self.rect.bottom = height
if self.rect.left <= 0:
self.rect.left = width
def exit(self):
for i in range(1, len(self.tail)):
if dist(self.rect.x, self.rect.y, self.tail[i][0], self.tail[i][1]) < 1:
sys.exit()
class Food(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((12,12))
self.image.fill(RED)
self.rect=self.image.get_rect()
self.rect.center = (x, y)
# Start dah game
pygame.init()
screen = pygame.display.set_mode([width,height])
clock = pygame.time.Clock()
running = True
print("ok?")
all_sprites = pygame.sprite.Group()
player = Snake()
food = Food(random.randrange(20, width - 20), random.randrange(20, height - 20))
all_sprites.add(player)
all_sprites.add(food)
while running:
clock.tick(3)
#pygame.display.update()
keys_pressed = pygame.event.get()
print(keys_pressed)
for i in keys_pressed:
if i.type == pygame.QUIT:
print("quit")
running = False
if pygame.sprite.collide_rect(player, food):
food.kill()
player.score += 1
food = Food(random.randrange(20, width - 20), random.randrange(20, height - 20))
all_sprites.add(food)
all_sprites.update()
screen.fill(BLACK)
player.exit()
for i in range(1, len(player.tail)):
pygame.draw.rect(screen, WHITE, (player.tail[i][0], player.tail[i][2], 12, 12))
#draw_score_text(screen, str(player.score), 18, width/2, 10)
all_sprites.draw(screen)
pygame.display.flip()
pygame.quit()
答案 0 :(得分:1)
必须在主循环中而不是在内部for
循环中更新显示和精灵图形:
while running:
# [...]
for i in range(1, len(player.tail)):
pygame.draw.rect(screen, WHITE, (player.tail[i][0], player.tail[i][2], 12, 12))
# <--
all_sprites.draw(screen)
pygame.display.flip()
请注意,如果蛇仅由头部组成,则不会执行内部循环。如果尾巴的长度大于1,则每帧显示将更新多次,这不是您想要的。
我收到超出范围的异常
Traceback (most recent call last): File "snake.py", line 132, in <module> > pygame.draw.rect(screen, WHITE, (player.tail[i][0], player.tail[i][2], 12, 12)) IndexError: tuple index out of range `
第二个(y)坐标的索引为1而不是2。Ist必须为player.tail[i][1]
:
for i in range(1, len(player.tail)):
pygame.draw.rect(screen, WHITE, (player.tail[i][0], player.tail[i][1], 12, 12))