|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import pygame
import sys
from pygame.locals import *
import 金币
pygame.init()
size = width, height = 600, 400
bg = (255, 255, 255)
YELLOW = (255, 255, 0)
snake = (0, 255, 0)
BLACK = (0, 0, 0)
fanxiang = 'right'
left = 300
top = 200
screen = pygame.display.set_mode(size)
pygame.display.set_caption('贪吃蛇')
move = USEREVENT
pygame.time.set_timer(move, 1 * 1000)
money = USEREVENT + 1
pygame.time.set_timer(money, 5 * 1000)
score = 0
score_font = pygame.font.Font(None, 20)
Money = pygame.sprite.Group()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == move:
if fanxiang == 'left':
left -= 11
elif fanxiang == 'top':
top -= 11
elif fanxiang == 'right':
left += 11
elif fanxiang == 'bottom':
top += 11
#生成金币
elif event.type == money:
m = 金币.money()
Money.add(m)
key_pressed = pygame.key.get_pressed()
#控制方向
if key_pressed[K_w] or key_pressed[K_UP]:
fanxiang = 'top'
if key_pressed[K_s] or key_pressed[K_DOWN]:
fanxiang = 'bottom'
if key_pressed[K_d] or key_pressed[K_RIGHT]:
fanxiang = 'right'
if key_pressed[K_a] or key_pressed[K_LEFT]:
fanxiang = 'left'
#显示字迹
score_text = score_font.render(f'Score:{score}', True, BLACK)
screen.blit(score_text, (10, 5))
screen.fill(bg)
#金币机制
for i in Money:
pygame.draw.rect(screen, YELLOW, i.rect, 0)
if i.rect.colliderect(pygame.Rect(left, top, 10, 10)):
score+=1000
Money.remove(i)
#绘制贪吃蛇
pygame.draw.rect(screen, snake, (left, top, 10, 10), 0)
pygame.display.flip()
问,为什么无法显示字迹?谢谢
您好,根据您的代码,您需要将 screen.blit 和 pygame.display.flip 的顺序调换一下。将 screen.blit 放在 screen.fill(bg) 之后, pygame.display.flip 放在最后即可。这样做可以确保字迹能够显示在屏幕上。希望能帮到您。
|
|