只有当鼠标移动时图像移动,为什么? Python3 + pygame的 [英] Image move only when mouse move, why? Python3+pygame

查看:1684
本文介绍了只有当鼠标移动时图像移动,为什么? Python3 + pygame的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,不知道如何解释这个错误。我有一个想要移动的图像,这是代码:

I have a problem and have no idea how explain this bug. I have an image that I want to move, and this is the code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import pygame
import random

pygame.init() 
size=[800,600] 
screen=pygame.display.set_mode(size)
pygame.display.set_caption("Sub Dice")

background_position=[0,0]
background_image=pygame.image.load('Gfx/Table.png').convert()
card=pygame.image.load('Gfx/Deck1card01.png').convert_alpha()
card=pygame.transform.smoothscale(card,(130,182))
closeDeckShirt=pygame.image.load('Gfx/CardBack.png').convert_alpha()

SETFPS=30
zx=0
zy=0

done=False
clock=pygame.time.Clock()

while done==False:
    clock.tick(SETFPS)
    for event in pygame.event.get():  
        if event.type == pygame.QUIT:
            done=True

        if event.type == pygame.MOUSEBUTTONDOWN:
            print('a')


        screen.blit(background_image, background_position)
        screen.blit(card,[zx,zy])
        zx=zx+2
        zy=zy+2
        pygame.display.flip()

pygame.quit ()

问题是图像仅在我单击或移动鼠标时移动。如果我不触摸鼠标,图像会静止不动。但是应该每帧移动(1/30秒)。

The problem is that card image moves only when I click or move a mouse. If I don't touch the mouse, the image stands still. But in shall move every frame (1/30 sec).

谁能解释我做错了什么?

Can anyone explain what I'm doing wrong?

推荐答案

你正在翻转你的事件循环。这意味着您当前正在为生成的每个事件更新屏幕。你不希望这样。

You are flipping inside your event loop. Which means you are currently updating the screen for every event generated. You don't want that.

这是样板文件。

要点是:

for event in events:
    #handle them
draw()
pygame.display.flip()

这篇关于只有当鼠标移动时图像移动,为什么? Python3 + pygame的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆