0
votes

I have started to learn pygame these days, and I have some questions when I uses pygame.event.get(). Hope someone can help me figure out the concept behind it.

Below is my simple code:

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()

Inside the while loop, program will receive event with pygame.event.get(). If detect "left key" is pressed, set x_change to negative, so the object in screen will go left, vice versa.

Now, I want to make boundary for my screen, the object cannot go outside the screen. Hence, I add a condition in if event.key == pygame.K_LEFT::

if player_pos_x > 0:
    player_pos_x_change = -0.1
else:
    player_pos_x_change = 0

However, when I hold the left key continuously, the object still go outside the screen. I cannot understand why will it happen ?

I'm very thank for your reply and comment.

1
If you are only calling view count setter and getter in single.php, you can call setPostViews(get_the_ID()); outside of the loop and still get this to work, probably in the correct manner. - Faham Shaikh
Also, you need to check if $count has any value stored in it and set it to '0' or increment and then call update_post_meta as it internally calls add_post_meta if the meta data doesn't exist or update it if meta data does exists. - Faham Shaikh
Here is the answer for the new question: link and link - Johnny

1 Answers

-1
votes

Swap the order of setPostViews(get_the_ID()); and the_post();

Your call to get_the_ID() will return unreliable results if you call it before the_post as the_post will set up the post you're viewing inside the loop and let all of the standard WordPress templating functions return the correct information.

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>