Pygame window problem

Pygame window problem

Postby runningluke » Tue Jul 12, 2016 7:42 pm

I've recently started learning python and have been trying to use pygame to produce a game with the help of a Youtube tutorial, while the coding is simple enough (I think) there seems to be a problem with the window that the code creates. Whenever I start run it, a window pops up with the correct image as the playable character which can move fine, however the image starts at the edge of the screen rather than half way along horizontally like i determined it should be. I also added barriers to block it going off the screen at 0 and at horizontal_width, but the game will detect the car hitting the barrier at around 250 pixels away from the edge of the screen. I've already run tests using print (event) to show that the game itself does detect the edge of the screen as 0 and the vertical line it hits is around 250 pixels. I've copied and pasted the original tutorials code to test if it was just my code that was wrong but it had the exact same effect. I'd really appreciate any feedback that might help solve this problem as i really want to try to get into python coding, I'll add my code just below in case there is actually something in it that caused the problem that i just missed
Code: Select all
import pygame
import os
position = 200, 500
os.environ['SDL_VIDEO_WINDOW_POS'] = str(position[0]) + "," + str(position[1])
pygame.init()
display_width = 600
display_height = 400

black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)

car_width = 91

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("A Bit Racey")
clock = pygame.time.Clock()

carImg = pygame.image.load("racecarpygame.png")

def car(x,y):
    gameDisplay.blit(carImg,(x,y))


def game_loop():

       
    x = (display_width * 0.5)
    y = (display_height * 0.45)

    x_change = 0
    y_change = 0

    gameExit = False
    while not gameExit:
       
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                  gameExit = True


                 
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -5
                elif event.key == pygame.K_RIGHT:
                    x_change = 5
                elif event.key == pygame.K_UP:
                    y_change = -5
                elif event.key == pygame.K_DOWN:
                    y_change = 5
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT:
                    x_change = 0
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_RIGHT:
                    x_change = 0
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_UP:
                    y_change = 0
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_DOWN:
                    y_change = 0       


        x += x_change
        y += y_change
       
        gameDisplay.fill(white)
        car(x,y)

        if x > display_width - car_width or x < 0:
            gameExit = True
        print(event)
        pygame.display.update()
        clock.tick(60)
game_loop()
pygame.quit()
quit()
Last edited by Yoriz on Tue Jul 12, 2016 7:51 pm, edited 1 time in total.
Reason: First post lock. Added code tags
runningluke
 
Posts: 2
Joined: Tue Jul 12, 2016 7:32 pm

Re: Pygame window problem

Postby metulburr » Tue Jul 12, 2016 8:12 pm

I've recently started learning python and have been trying to use pygame to produce a game with the help of a Youtube tutorial

I hope its not sentdex tutorials? If so see here...
viewtopic.php?f=26&t=20317

As for the positioning of the image. Depending on your size of your image, it will appear to be at different positions. The larger it is, the more it is to the right. Only the topleft corner is at the center. You should be using pygame rects to coordinate movement and positioning anyways. It will simplify your programming. You could set an image in the center of the screen with a single line regardless of the size of the image.

Code: Select all
rect = image.get_rect(center=screen_rect.center)


such as in the tutorials
http://python-gaming.com/pygame/docs/tuts/index.html
we will be moving to python-forum.io on October 1 2016
more details here
User avatar
metulburr
 
Posts: 2244
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Pygame window problem

Postby runningluke » Tue Jul 12, 2016 8:33 pm

It actually was, that's embarrassing. Thank you for the quick reply I'll take a look and see if that sorts it out, Thanks again
runningluke
 
Posts: 2
Joined: Tue Jul 12, 2016 7:32 pm


Return to Game Development

Who is online

Users browsing this forum: No registered users and 2 guests

cron