Pygame显示图片闪烁

稻dao草re人n 2017-02-09 10:17:32
pygame实现消消乐游戏,当点击某一个小动物图标时,为该小动物图标加载一个选择框,采用screen.blit(frame,(frame_x,frame_y))进行的加载图片,其中frame是图片,(frame_x,frame_y)是经过计算获得的所点击的小动物的左上角坐标,可不知道为什么frame显示不出来,有时候会闪一下然后消失,各位大神有没有好的解决方案,源代码如下:
import pygame
from pygame.locals import *
from sys import exit
import random

'''brick : 218*218
animal : 40*40
bg : 850*600 '''
pygame.init() # 初始化


animalsize = 40
bricksize = 44
centerx, centery = 300, 200
allbrick = 218
pygame.display.set_caption('开心消消乐')
loca = [[random.randint(0, 4) for col in range(5)]for row in range(5)]
image = ()

screen = pygame.display.set_mode((850, 600), 0, 32)

fox = pygame.image.load('fox.png').convert_alpha()
bear = pygame.image.load('bear.png').convert_alpha()
chick = pygame.image.load('chick.png').convert_alpha()
frog = pygame.image.load('frog.png').convert_alpha()
eagle = pygame.image.load('eagle.png').convert_alpha()
image = (fox, bear, chick, frog, eagle)

bg_image = pygame.image.load('bg.jpg').convert()
brick = pygame.image.load('brick.png').convert_alpha()
frame = pygame.image.load('frame.png').convert_alpha()
mouse_cursor = pygame.image.load('mouse.png').convert_alpha()


def mouse_change():
""" 更换鼠标图片 """
# 获取鼠标的x,y坐标
mouse_x, mouse_y = pygame.mouse.get_pos()
# 隐藏鼠标
pygame.mouse.set_visible(False)
# 计算光标的左上角位置
mouse_x -= mouse_cursor.get_width() / 2
mouse_y -= mouse_cursor.get_height() / 2
# 用其他图形代替鼠标
screen.blit(mouse_cursor, (mouse_x, mouse_y))

# print(mouse_change.__doc__)
while True:
#设置背景图
screen.blit(bg_image, (0, 0))
#设置深色背景
screen.blit(brick, (centerx, centery))
#设置小动物
for i in range(5):
for j in range(5):
x, y = centerx + 1 + j * bricksize, centery + 1 + i * bricksize
screen.blit(image[loca[i][j]], (x, y))
# screen.blit(frame, (300, 200))
#响应事件
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == pygame.K_q:
exit()
if event.type == QUIT:
exit()
# screen.blit(frame, (300, 200))
if event.type == MOUSEBUTTONDOWN:
pressed_array = pygame.mouse.get_pressed()
mouse_down_x, mouse_down_y = event.pos
# print(mouse_down_x, mouse_down_y)
#判断是否鼠标点击在小动物上
if centerx <= mouse_down_x <= (centerx + allbrick) and centery <= mouse_down_y <= (centery + allbrick):
row = int((mouse_down_y - centery) / bricksize)
col = int((mouse_down_x - centerx) / bricksize)
print(row, col)
#计算点击的小动物图标的左上角坐标
frame_x = centerx + col * bricksize
frame_y = centery + row * bricksize
print(frame_x, frame_y)
print(frame)
#如果鼠标左键按下进行响应
for index in range(3):
if pressed_array[index]:
if index == 0:
#在屏幕上显示选择框------------出错!!!!!
screen.blit(frame, (frame_x, frame_y))
pygame.display.update(pygame.rect.Rect((frame_x, frame_y), (45, 45)))
pygame.display.update()
#if event.type == MOUSEBUTTONUP:
#pygame.surface.blit(frame, (frame_x, frame_y))
mouse_change()


pygame.display.update()

...全文
563 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
print_5 2020-03-03
  • 打赏
  • 举报
回复
对,注意也不要有两个flip
Solitary man 2018-12-23
  • 打赏
  • 举报
回复
有两个update

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧