Python Forum
[PyGame] Need help.. function not work i think...
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Need help.. function not work i think...
#1
Hey guys,
I'm a french beginner from python. I know all the basic python but i have a problem with pygame and class in generally..
So my game goal is :
When the game start, a window is open and a enemy is draw in, u have to click on the enemy and if u hit him(with ur mouse) then this one is desappearing and an other one is appearing, you have 30sec to click on the most enemy u can.

import sys
import pygame
from pygame.locals import *
from random import *

# Initialising
score = 0
x = int()
y = int()
size = width, height = 1280, 720
FPS = 60
timer = 0
retry = False


class Player(pygame.sprite.Sprite):
    # sprite for the enemy u have to click-on
    def __init__(self):
        # this line is required to properly create the sprite
        pygame.sprite.Sprite.__init__(self)
        # create a plain rectangle for the sprite image
        self.image = pygame.image.load("ImageSonic.png")
        self.image = pygame.transform.scale(self.image, (200, 200))
        # find the rectangle that encloses the image
        self.rect = self.image.get_rect()
        self.left = x
        self.right = x + 200
        self.top = y
        self.bottom = y + 200

    def take_aim(self):  # any code here will happen every time the game loop updates
        mx, my = pygame.mouse.get_pos()
        print(mx, my)
        aim_x, aim_y, aim_global = False, False, False
        if self.left <= mx <= self.right:
            aim_x = True
        if self.top <= my <= self.bottom:
            aim_y = True
        if aim_x or aim_y:
            aim_global = True
        return aim_global

    def random_aim(self):
        img_x = randint(100, 1180)
        img_y = randint(100, 620)
        screen.blit(self.image, (img_x, img_y))
        pygame.display.flip()


# initialize pygame and create window
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode(size)
pygame.display.set_caption("SWAP - Fast_Click")
backGround = pygame.image.load("Image_bg_pink&blue_flou.jpg")
backGround = pygame.transform.scale(backGround, (1280, 720))
clock = pygame.time.Clock()

all_sprites = pygame.sprite.Group()
player = Player()
all_sprites.add(player)

# Game loop
running = True
while running:
    clock.tick(FPS)
    timer += 1
    if timer == 1800:
        sys.exit()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            print("Ton score est de : ", score)
            running = False
        elif event.type == MOUSEBUTTONDOWN:
            retry = Player.take_aim(player)
            if retry:
                score += 1
                all_sprites.remove(player)
                Player.random_aim(player)

    # Update
    all_sprites.update()

    # Draw / render
    screen.blit(backGround, (0, 0))
    all_sprites.draw(screen)
    # *after* drawing everything, flip the display
    pygame.display.flip()
If u don't understand something in my code, please contact me in pv and ask me.
I need to get it working in 4 days so .. (personnal game presentation)
Reply


Messages In This Thread
Need help.. function not work i think... - by Skytarez - Nov-09-2017, 08:54 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020