Pygame problem

This is the place for queries that don't fit in any of the other categories.

Pygame problem

Postby gerdkanter » Wed Jun 11, 2014 3:40 pm

Hi!
I'm new to programming and I'm trying to make a small game using pygame. The game is quite simple. There are 4 squares. A frog appears in one of these squares at random. The player must click on the frog before it goes away to another square. If the player successfully clicks on the frog, 1 point is added.
The code is very, very incomplete right now. The problem I'm having right now is this - the points are added just BEFORE the frog appears in the square. For example: I click on an empty square and I get a point, even though the frog appears directly after that. And when there IS a frog in the square and I click on it, I get no points.
I sincerely hope you understand what I'm saying since English is not my native language.
Code: Select all
import pygame, sys, random
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode([800,600])

frog = pygame.image.load("konn.png")
x = 100
y = 100
points = 0

while True:
    screen.fill([255,255,255])
    pygame.draw.rect(screen, [0,0,0], [100,100,100,100],2)
    pygame.draw.rect(screen, [0,0,0], [300,100,100,100],2)
    pygame.draw.rect(screen, [0,0,0], [100,300,100,100],2)
    pygame.draw.rect(screen, [0,0,0], [300,300,100,100],2)
    rand = random.randrange(0,4)
    screen.blit(frog, (x,y))
    pygame.display.flip()

    for i in range (10):
        pygame.time.delay(100)
        if points == 10:
            print("Game over")
            sys.exit()
        if rand == 0:
            x = 100
            y = 100
            for j in pygame.event.get():
                if j.type == pygame.MOUSEBUTTONDOWN:
                    x1, y1 = j.pos
                    if (x1 >= 100 and x1 <= 200 and y1 >= 100 and y1 <= 200):
                        points += 1
                        print(points)

        elif rand == 1:
            x = 100
            y = 300
            for j in pygame.event.get():
                if j.type == pygame.MOUSEBUTTONDOWN:
                    x1, y1 = j.pos                   
                    if (x1 >= 100 and x1 <=200 and y1 >= 300 and y1 <= 400):
                        points += 1
                        print(points)
                   
        elif rand == 2:
            x = 300
            y = 100
            for j in pygame.event.get():
                if j.type == pygame.MOUSEBUTTONDOWN:
                    x1, y1 = j.pos                   
                    if (x1 >= 300 and x1 <= 400 and y1 >= 100 and y1 <= 200):
                        points += 1
                        print(points)
                       
        elif rand == 3:
            x = 300
            y = 300         
            for j in pygame.event.get():
                if j.type == pygame.MOUSEBUTTONDOWN:
                    x1, y1 = j.pos
                    if (x1 >= 300 and x1 <= 400 and y1 >= 300 and y1 <= 400):
                        points += 1
                        print(points)
                 
    for i in pygame.event.get():
        if i.type == pygame.QUIT:
            sys.exit()
Last edited by Mekire on Wed Jun 11, 2014 3:44 pm, edited 1 time in total.
Reason: First post lock.
gerdkanter
 
Posts: 1
Joined: Wed Jun 11, 2014 2:45 pm

Re: Pygame problem

Postby Mekire » Wed Jun 11, 2014 4:16 pm

You should start thinking of your game as having three phases.
  • get user input
  • update
  • draw

Take a look at this for starters.
Code: Select all
import sys
import random
import pygame as pg


COORDINATES = [(100,100), (300,100), (100,300), (300,300)]
DELAY_TIME = 350
FPS = 60

pg.init()
screen = pg.display.set_mode((800,600))
clock = pg.time.Clock()

timer = 0.0


##frog = pg.image.load("konn.png")
frog = pg.Surface((100,100)).convert()
frog.fill(pg.Color("green"), frog.get_rect().inflate(-16,-16))
frog_rect = frog.get_rect(topleft=(100,100))

points = 0
done = False
while not done:
    now = pg.time.get_ticks()

    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True
        elif event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
            if frog_rect.collidepoint(event.pos):
                points += 1
                frog_rect.topleft = (-100, -100)
                timer = now
                print(points)

    if now-timer > DELAY_TIME:
        timer = now
        frog_rect.topleft = random.choice(COORDINATES)
    if points == 10:
        print("Game over")
        done = True

    screen.fill(pg.Color("white"))
    for i,j in COORDINATES:
        screen.fill(pg.Color("black"), (i,j,100,100))
        screen.fill(pg.Color("white"), (i+2,j+2,96,96))
    screen.blit(frog, frog_rect)
    pg.display.update()
    clock.tick(FPS)

pg.quit()
sys.exit()

Notice these three phases in the main loop.
You should also stop what you are doing, and take the time now to learn about functions.
Game programming requires the use of classes and OO.
You will definitely need to learn classes pretty quickly; but you need to learn functions immediately.

-Mek
Last edited by Mekire on Wed Jun 11, 2014 4:25 pm, edited 1 time in total.
Reason: Slight code edit
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Pygame problem

Postby XndrK » Wed Jun 11, 2014 8:36 pm

Are you sure English isn't your native language, because I understood every word you said. Many native speakers on this forum are less comprehensible than you. Good job!

As for your Python problems, I'm not familiar enough with Pygame to help.
Proverbs 26:14 describes me a bit too well.

Version: Python 2.7.5

https://github.com/4of92000
https://github.com/PythonForum/
User avatar
XndrK
 
Posts: 172
Joined: Sat Jun 15, 2013 7:57 pm


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot], micseydel and 2 guests

cron