- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在 pygame 中制作文明 build 者游戏。现在你能做的就是点击城市来声明它们是你的,每 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()
最佳答案
[...] So the problem I'm at right now is because of for event in
pygame.event.get():
the screen only updates when I move my mouse [...]
答案被编码在问题中。您必须在主应用程序循环中而不是事件循环中更新窗口。事件循环必须处理用户输入(在您的问题的评论中提到)并更改游戏状态以反射(reflect)输入。但事件循环的职责不是绘制场景。
为了实现最佳控制流,主应用程序循环必须执行以下操作:
这会导致场景在游戏的当前状态的每一帧中重新绘制:
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()
关于python - 对于 pygame.event.get() 中的事件 : is getting in the way of Timer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59517278/
当检测鼠标x和y坐标时,最好像这样使用event.clientX和event.clientY: function show_coords(event){ var x=event.clientX;
我有以下代码: document.oncontextmenu = function(evt) { evt = evt || window.event; console.log(evt.
对于另一个问题,我遇到了一个似乎偶尔出现在 SO 的误解。一些提问者似乎认为触发器之于数据库就像事件之于 OOP 一样。 有没有人有一个很好的类比来解释为什么这是一个有缺陷的比较,以及误用它的后果?
$('body').keypress(function(event){ if(event.keyCode == 46){console.log('Delete Key Pressed')};
我正在制作一个“流体”文本区域,它根据内容调整其高度。我实际上正在尝试实现 this脚本。我有以下代码:https://ellie-app.com/Vjtvm6yrKWa1/4 问题是,当增加高度时,
我使用 Raphael .mouseover() 和 .mouseout() 事件来突出显示 SVG 中的某些元素。这工作正常,但在我单击一个元素后,我希望它停止突出显示。 在 Raphael doc
我目前正在开发一个应用程序,允许人们为在线广播电台安排“节目”。 我希望用户能够设置重复事件,例如:- “躁狂星期一”节目 - 每周一 9 点至 11 点“月中疯狂” - 每个月的第二个星期四“本月新
我有以下三个表格(简化版本): 已加载关卡: id(整数、主键、自动增量) globalId(整数,键) 日期(日期时间、键) serverId(Int,键) gamemodeId(Int,Key)
在我阅读 Gevent Tutorial 之后,我有一个关于 gevent.event.Event 的问题。 Event.set() 是否会唤醒所有被 Event.wait() 阻塞的函数? 就像下面
我对 cakephp ver3.1.3 没有经验 我按照说明实现了登录认证功能; http://book.cakephp.org/3.0/en/tutorials-and-examples/blog-
现在,我发送 10 个事件,每个事件有 1 个属性。但是当我想过滤特定事件并按属性选择事件时,在“事件属性”过滤器中仅显示前 7 个事件,而我为其余事件添加的事件仅显示“第一次”过滤器,为什么? 最佳
我不知道我的 Firefox 发生了什么! 我的aspx和javascript代码是这样的: function a() { alert(
中有3个事件fns重装 ,我可以对两者做同样的事情 reg-event-db和 reg-event-fx . reg-event-db之间的主要区别是什么, reg-event-fx和 reg-eve
我遇到了 Firefox keydown 行为,因为在没有聚焦于特定字段的情况下按下 Enter 键(实际上是任何键)不会触发 keydown 事件只会触发`按键事件。 这可能会非常令人困惑,因为 k
这是我的代码片段 public class Notation : INotifyPropertyChanged { public event PropertyChangedEventHandl
我可以在一个 Jsf2 xhtml 文件中有多个标签吗? 在那种情况下,关联的监听器将以什么顺序被调用? Mojarra 2.1.1/Apache Tomcat 7.0.22/PrimeFaces 3
我可以在一个 Jsf2 xhtml 文件中有多个标签吗? 在那种情况下,关联的监听器将以什么顺序被调用? Mojarra 2.1.1/Apache Tomcat 7.0.22/PrimeFaces 3
我有以下 JavaScript: $('#ge-display').click(function (event) { window.open('/googleearth/ge-display.ph
我需要确定触发事件的元素。 使用 event.target 获取相应的元素。 我可以从那里使用哪些属性? 引用 编号 节点名 我找不到关于它的大量信息,即使在 jQuery 上也是如此页,所以希望有人
我在pyGame中创建了一个Asteroidz克隆,并在pygame.vent.get()循环中有两个for Event,一个用于检查退出请求,以及游戏是否应该通过按空格键开始,然后在游戏中进一步尝试
我是一名优秀的程序员,十分优秀!