【发布时间】:2015-06-30 13:36:25
【问题描述】:
每当我尝试制作蛇游戏时,这仍然不完整,每当我的蛇与边界碰撞时,它都会进入下一个屏幕打印
你太烂了再试一次
但如果我双击 pygame 窗口上的任意位置,它有时会崩溃。
左键点击次数不止2次,但最后还是点击了一段时间后崩溃。
我必须做些什么来解决这个问题,因为在我玩蛇的初始屏幕上不存在这个问题。
我使用的代码如下:
import pygame
import random
pygame.init()
font = pygame.font.Font(None,25)
def mess(msg,color):
screen_text = font.render(msg, True , color)
display.blit(screen_text , [(w/2) - 50 , h/2] )
score = 0
w = 800
h = 600
display = pygame.display.set_mode((w,h))
pygame.display.update()
red = (255,0,0)
white = (255,255,255)
black = (0,0,0)
yellow = (255,255,0)
bye = False
xpos = w/2
ypos = h/2
xch = 0
ych = 0
b = 10
while True:
wa = random.randrange (10,700)
ha = random.randrange (10,500)
if wa%10 == 0 and ha%10 == 0:
break
time = pygame.time.Clock()
FPS = 10
up = False
side = False
lenth = 10
while not bye:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w and up == False:
ych = -b
xch = 0
up = True
side = False
if event.key == pygame.K_s and up == False:
ych = +b
xch = 0
up = True
side = False
if event.key ==pygame.K_d and side == False:
xch = +b
ych = 0
side = True
up = False
if event.key == pygame.K_a and side == False:
xch = -b
ych = 0
side = True
up = False
if xpos == wa and ypos == ha:
lenth = lenth + 10
while True:
wa = random.randrange (10,700)
ha = random.randrange (10,500)
if wa%10 ==0 and ha%10 == 0:
break
score = score + 10
if xpos == w - 10 or xpos == 0 or ypos == h - 10 or ypos == 0:
xch = 0
ych = 0
display.fill(black)
mess("You suck Try again!",red)
pygame.display.update()
break
xpos = xpos + xch
ypos = ypos + ych
time.tick(FPS)
display.fill(white)
display.fill(red , rect=[xpos,ypos,10,10])#snake
display.fill(yellow ,rect=[xpos + 0.41 , ypos +10 ,10 , lenth])#bady
display.fill(black, rect=[wa,ha,10,10]) #food
pygame.display.update()
【问题讨论】:
-
下次请花点时间让您的问题可读。