0
votes

I am trying to make a game in Pygame where he objective is to start the game but the start button keeps moving whenever you touch it but the window will not respond. I am not finished with the code yet because I tested it and it didn't work. Here is my code so far:

import pygame
import random
import time
pygame.init()
display = pygame.display.set_mode((800,600))
pygame.display.set_caption('BEST 3D PLATFORMER FPS GAME!')
clock = pygame.time.Clock()
pygame.display.update()
clock.tick(60)
display.fill((255,255,255))
def newposition()
    randx = random.randrange(100, 700)
    randy = random.randrange(100,500)
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pass
        if event.ty
    button = pygame.image.load('start.png')
    display.blit(button,(randx,randy))

pygame.quit()
quit()
3
Please ask a question. Also, working code to go along with it would be preferable. - robbymurphy
You have an if statement that isn't properly finished. Also, you are infinite looping, saying while True with no wait or something to stop it is just going to crash the game. - Imran Ali
find some tutorial and you will learn faster than asking on SO - furas

3 Answers

1
votes

All comments inside code

import pygame
import random

# --- constants --- (UPPER_CASE names)

WHITE = (255, 255, 255) # space after every `,`

FPS = 30

# --- classes --- (CamelCase names)

#empty

# --- functions --- (lower_case names)

def new_position():
    x = random.randrange(100, 700) # space after every `,`
    y = random.randrange(100, 500) # space after every `,`
    return x, y # you have to return value

# --- main --- (lower_case names)

# - init -

pygame.init()

display = pygame.display.set_mode((800, 600)) # space after every `,`

pygame.display.set_caption('BEST 3D PLATFORMER FPS GAME!')

# - objects -

# load only once - don't waste time to load million times in loop
button = pygame.image.load('start.png').convert_alpha()
button_rect = button.get_rect() # button size and position
button_rect.topleft = new_position() # set start position

# - mainloop -

clock = pygame.time.Clock()

running = True

while running:

    # - events -

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            #pass # it does nothing so you can't exit
            running = False # to exit `while running:`

        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                running = False # to exit `while running:`

        elif event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1: # left button
                 button_rect.topleft = new_position() # set new position

        # if event.ty # Syntax Error

    # - updates (without draws) -

    #empty

    # - draws (without updates) -

    # you have to use it inside loop
    display.fill(WHITE) # clear screeen before you draw elements in new place

    display.blit(button, button_rect)

    # you have to use it inside loop
    pygame.display.update() # you have to send buffer to monitor

    # - FPS -

    # you have to use it inside loop
    clock.tick(FPS) 

# - end -

pygame.quit()

BTW: simple template which you can use when you start new project.

PEP 8 -- Style Guide for Python Code

1
votes

I had a similar issue, the fix is not straight forward. Here are my notes for python 3.6.1 and Pygame 1.9.3:

1) The event is unresponsive because there is no display generated for pygame, add a window display after pygame initialization:

pygame.init()  # Initializes pygame
pygame.display.set_mode((500, 500)) # <- add this line. It generates a window of 500 width and 500 height

2) pygame.event.get() is a generates a list of event but not all event classes have .key method for example mouse motion. Thus, change all .key event handling codes such as

if event.key == pygame.K_q:
    stop = True

To

if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_q:
        stop = True

3) In some mac/python combinations even after the window is generated it doesn't focus / record keyboard events, this is a pygame issue and is fixed in a reimplementation of pygame using sdl2 (SDL is a cross-platform development library which provides low level access to keyboard, audio ,mouse etc). it can be installed by following the instructions here https://github.com/renpy/pygame_sdl2. One might need to install homebrew for it, which is another package manager for macOS. Instructions can be found here https://brew.sh/

4) after installing it using the instructions on github link you need to change all import pygame to import pygame_sdl2 as pygame

5) Voila! fixed ...

0
votes

You've got to load button outside the loop (it only needs to be done once.)

button = pygame.image.load('start.png')

Also, you have defined newposition(), but have not called it. Also, randx and randy would not be accessible from outside of the function, because the would be local to it.

So, change your function to this :

def newposition()
    randx = random.randrange(100, 700)
    randy = random.randrange(100,500)
    return randx, randy # Output the generated values

Then, before the loop :

rand_coords = newposition()

You simply forgot to update the pygame display with your modifications to it.

At the end of your loop, add pygame.display.update(), like this:

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pass
        # if event.ty why is that here?

    display.blit(button,(rand_coords[0],rand_coords[1])) # Take our generated values
    pygame.display.update()

Final code :

import pygame
import random
import time

pygame.init()
display = pygame.display.set_mode((800,600))
pygame.display.set_caption('BEST 3D PLATFORMER FPS GAME!') # Yeah, exactly :)

clock = pygame.time.Clock()

display.fill((255,255,255))

def newposition()
    randx = random.randrange(100, 700)
    randy = random.randrange(100,500)
    return randx, randy # Output the generated values

rand_coords = newposition() # Get rand coords

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit() # Quit pygame if window in closed
        # if event.ty why is that here?

    clock.tick(60) # This needs to be in the loop 

    display.fill((255,255,255)) # You need to refill the screen with white every frame.
    display.blit(button,(rand_coords[0],rand_coords[1])) # Take our generated values
    pygame.display.update()