pygame.event.get()中的事件:妨碍了Timer

TNT爆发

我在pygame中制作一个文明建设者游戏。现在,您所能做的就是点击城市以将其声明为您的城市,每2秒钟您就会获得与拥有多少个城市相等的资金。所以我现在for event in pygame.event.get():遇到的问题是因为只有在移动鼠标时屏幕才会更新。我不确定如何重新排列代码,以便代码自行更新。

import pygame, time, random, threading
import numpy as np
from PIL import Image
from threading import Timer

pygame.init()
width=525
height=700
screen = pygame.display.set_mode( (width, height ) )
pygame.display.set_caption('Territory Game')

font = pygame.font.Font('freesansbold.ttf', 20)

base = pygame.image.load("base.png").convert()
character = pygame.image.load("character.png").convert()
captured_base = pygame.image.load("captured-base.png").convert()

xIm = 10 # x coordnate of image
yIm = 10 # y coordinate of image

Startlist = []
for lop in range(441):
    Startlist.append(random.randint(0,20))
Map = np.reshape((Startlist),(21, 21))
Startlist = []
Map[10,10] = 1
xcounter = -1
captured = ([[10,10]])
money = 0

def printit():
    global money
    money += len(captured)
    t = Timer(2, printit)
    t.start()
t = Timer(2, printit)
t.start()
running = True
while (running):
    for event in pygame.event.get():
        screen.fill((79,250,91))
        pygame.draw.rect(screen, (0,0,0), (0,525,525,10))
        pygame.draw.rect(screen, (164,164,164), (0,535,525,165))
        for iterate in np.nditer(Map):
            xcounter +=1
            Icony = int(xcounter/21)
            Iconx = xcounter-(Icony*21)
            if iterate == 1:
                if [Iconx,Icony] not in captured:
                    screen.blit(base,(Iconx*25,Icony*25))
                if [Iconx,Icony] in captured:
                    screen.blit(captured_base,(Iconx*25,Icony*25))
                if event.type == pygame.MOUSEBUTTONDOWN:
                    #Set the x, y postions of the mouse click
                    x, y = event.pos
                    if base.get_rect().collidepoint(x-(Iconx*25), y-(Icony*25)):
                        if [Iconx,Icony] not in captured:
                            captured.append([Iconx,Icony])
        for thing in captured:
            screen.blit(captured_base,(thing[0]*25,thing[1]*25))

        screen.blit(font.render("Money: "+str(money), True, (0,0,0)),(5, 541))
        xcounter = -1
        pygame.display.flip()
        if event.type == pygame.QUIT:
            running = False
pygame.quit()
拉比德76

[...]我现在的问题是因为pygame.event.get():屏幕上的事件仅在移动鼠标时才会更新[...]

答案编码在问题中。您必须在主应用程序循环中而不是事件循环中更新窗口。事件循环必须处理用户输入(在问题注释中提到)并更改游戏状态以反映输入。但是事件循环的职责不是绘制场景。

为了获得最佳控制流,主应用程序循环必须执行以下操作:

  • 处理事件
  • 清除显示
  • 绘制场景
  • 更新显示

这导致场景在每一帧中都以游戏的当前状态重新绘制:

running = True
while running:

    # handle the events
    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            running = False

        if event.type == pygame.MOUSEBUTTONDOWN:
            xcounter = -1
            for iterate in np.nditer(Map):
                xcounter +=1
                Icony = int(xcounter/21)
                Iconx = xcounter-(Icony*21)
                if iterate == 1:
                    #Set the x, y postions of the mouse click
                    x, y = event.pos
                    if base.get_rect().collidepoint(x-(Iconx*25), y-(Icony*25)):
                        if [Iconx,Icony] not in captured:
                            captured.append([Iconx,Icony])

    # clear the display
    screen.fill((79,250,91))

    # draw the scene
    pygame.draw.rect(screen, (0,0,0), (0,525,525,10))
    pygame.draw.rect(screen, (164,164,164), (0,535,525,165))

    xcounter = -1
    for iterate in np.nditer(Map):
        xcounter += 1
        Icony = int(xcounter/21)
        Iconx = xcounter-(Icony*21)
        if iterate == 1:
            if [Iconx,Icony] not in captured:
                screen.blit(base,(Iconx*25,Icony*25))
            if [Iconx,Icony] in captured:
                screen.blit(captured_base,(Iconx*25,Icony*25))

    for thing in captured:
        screen.blit(captured_base,(thing[0]*25,thing[1]*25))

    screen.blit(font.render("Money: "+str(money), True, (0,0,0)),(5, 541))

    # update the display
    pygame.display.flip()

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

pygame.event.get()无法发布事件

来自分类Dev

在Pygame event.get()循环中使用缩进事件

来自分类Dev

pygame.event.get()不返回任何事件

来自分类Dev

无法在pygame.event中退出事件

来自分类Dev

我应该使用pygame.event.get()还是pygame.event.poll()?

来自分类Dev

我应该使用pygame.event.get()还是pygame.event.poll()?

来自分类Dev

更快版本的“ pygame.event.get()”。为什么会错过事件,为什么会延迟事件?

来自分类Dev

Python pygame.event.get_pressed()未更新

来自分类Dev

pygame-event.pos在def main()中不起作用

来自分类Dev

pygame 没有响应(pygame.event.get() 已经被调用)

来自分类Dev

为什么pygame.event.get()每帧只能调用一次

来自分类Dev

在pygame中检测用户事件

来自分类Dev

在pygame中处理事件

来自分类Dev

Pygame事件错误

来自分类Dev

PyGame:MOUSEBUTTONDOWN事件的问题

来自分类Dev

pygame事件循环麻烦

来自分类Dev

pygame-pygame.mouse.get_pos问题

来自分类Dev

pygame.time.set_timer()事件重置了吗?

来自分类Dev

pygame:通过pygame.time.set_timer()发送具有属性的自定义事件

来自分类Dev

我在pygame中的事件循环有问题

来自分类Dev

pygame,如果event.type == pygame.KEYDOWN()TypeError:'int'对象不可调用

来自分类Dev

Pygame-在event.type == pygame.KEYDOWN时发出更改x值的问题

来自分类Dev

pygame中的旋转

来自分类Dev

pygame中的线程

来自分类Dev

在程序中包含pygame

来自分类Dev

pygame中的矩形闪烁

来自分类Dev

在PyGame中刷新图像

来自分类Dev

在pygame中爬梯子

来自分类Dev

Pygame中的单选按钮?