python-3.x - 卡在 pygame.event.get()
问题描述
这几天开始学习pygame了,在使用pygame.event.get()的时候遇到了一些问题。希望有人能帮我弄清楚它背后的概念。
下面是我的简单代码:
import pygame
# initialize pygame
pygame.init()
# create a window
screen = pygame.display.set_mode((800,600))
# load player (image) into pygame
player_img = pygame.image.load("aircraft.png")
player_pos_x = 400
player_pos_y = 500
player_pos_x_change = 0
# remove and redraw image in screen: move the object
def PlayerPosition(x,y):
screen.blit(player_img, (x,y))
# go inside game loop
running = True
while running:
# receive any event
for event in pygame.event.get():
# handle QUIT event
if event.type == pygame.QUIT:
running = False
# handle KEYDOWN event
if event.type == pygame.KEYDOWN:
# decide left key or right key is pressed
if event.key == pygame.K_LEFT:
if player_pos_x > 0:
player_pos_x_change = -0.1
else:
player_pos_x_change = 0
if event.key == pygame.K_RIGHT:
player_pos_x_change = 0.1
# handle KEYUP event
if event.type == pygame.KEYUP:
player_pos_x_change = 0
# set background color :
screen.fill((0, 0, 0))
# update player's x position
player_pos_x += player_pos_x_change
# call function to remove and redraw image in screen
PlayerPosition(player_pos_x, player_pos_y)
pygame.display.update()
在 while 循环中,程序将使用 pygame.event.get() 接收事件。如果检测到“左键”被按下,则将x_change设置为负数,因此屏幕中的对象将向左移动,反之亦然。
现在,我想为我的屏幕做边界,对象不能超出屏幕。因此,我在if event.key == pygame.K_LEFT:中添加了一个条件:
if player_pos_x > 0:
player_pos_x_change = -0.1
else:
player_pos_x_change = 0
但是,当我连续按住左键时,对象仍然会离开屏幕。我不明白为什么会发生?
我非常感谢您的回复和评论。
解决方案
setPostViews(get_the_ID());
交换和的顺序the_post();
如果您在the_post之前调用它,您的调用get_the_ID()
将返回不可靠的结果,因为这将设置您在循环内查看的帖子,并让所有标准 WordPress 模板函数返回正确的信息。the_post
get_header(); ?>
<div id="page" class="single clear">
<div class="content">
<article class="article">
<?php
// Elementor `single` location.
if ( ! function_exists( 'elementor_theme_do_location' ) || ! elementor_theme_do_location( 'single' ) ) {
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
setPostViews(get_the_ID());
?>
<div id="post-<?php the_ID(); ?>" <?php post_class( 'post' ); ?>>
<div class="single_post">
<?php if ( '1' === $schema_lite_single_breadcrumb_section && empty( $disable_title ) ) { ?>
<div class="breadcrumb" xmlns:v="http://rdf.data-vocabulary.org/#"><?php schema_lite_the_breadcrumb(); ?></div>
<?php } ?>
<?php if ( empty( $disable_title ) || empty( $disable_post_meta ) ) { ?>
<header>
<?php if ( empty( $disable_title ) ) { ?>
<h1 class="title single-title"><?php the_title(); ?></h1>
<?php } ?>
<?php if ( empty( $disable_post_meta ) ) { ?>
<div class="post-info">
<!-- <span class="theauthor"><i class="schema-lite-icon icon-user"></i> <?php esc_html_e( 'By', 'schema-lite' ); ?> <?php the_author_posts_link(); ?></span> -->
<span class="posted-on entry-date date updated"><i class="schema-lite-icon icon-calendar"></i> <?php the_time( get_option( 'date_format' ) ); ?></span>
<span class="featured-cat"><i class="schema-lite-icon icon-tags"></i> <?php the_category( ', ' ); ?></span>
<span> <?php echo getPostViews(get_the_ID()); ?> </span>
推荐阅读
- android - 找不到 com.android.support:appcompat-v7:26.0.1
- android - 我的语言翻译在调试版本中不起作用
- php - 如何自动增加账单号
- ios - 根据对象属性过滤任何对象的数组
- eclipse - selectionChanged 在特定项目上
- mysql - 为什么在 phpmyadmin 中导入数据库时出现错误 1616?
- c# - 在游戏对象 unity3d 上绘制自定义形状
- c# - 在 HttpClient 的多个重定向中传递授权标头
- c# - 实例化一个新类和从游戏对象中获取该组件有什么区别?
- mysql - 从 docker 容器记录 mysql 查询