Reputation: 51
So my idea is that i have a windowSurface.blit rectangle and when I press it with left mouse click it shall do these commands MOVE_SPEED = 9 end_it_levels = True
but I can't wrap my head around how to do that. The mouse functions are found here http://www.pygame.org/docs/ref/mouse.html#comment_pygame_mouse_get_pos
buttonEasy = pygame.Rect(10, 150, 200, 90)
buttonNormal = pygame.Rect(220, 150, 200, 90)
buttonHard = pygame.Rect(430, 150, 200, 90)
end_it_levels = False
while (end_it_levels == False):
windowSurface.fill(WHITE)
textlevelFont = pygame.font.SysFont("impact", 26)
level = textlevelFont.render("LEVEL:", True, (BLACK))
level_levels = textlevelFont.render("Easy = 1, Medium = 2 and Hard = 3"
, True, (BLACK))
for event in pygame.event.get():
if event.type == KEYDOWN and event.key == K_1:
MOVE_SPEED = 9
end_it_levels = True
if event.type == KEYDOWN and event.key == K_2:
MOVE_SPEED = 7
end_it_levels = True
if event.type == KEYDOWN and event.key == K_3:
MOVE_SPEED = 5
end_it_levels = True
windowSurface.blit (level, (290, 115))
#windowSurface.blit (level_levels, (140, 150))
windowSurface.blit(buttonEasyImage, buttonEasy)
windowSurface.blit(buttonNormalImage, buttonNormal)
windowSurface.blit(buttonHardImage, buttonHard)
pygame.display.flip()
Basicly this code here but that you click left mouse button and within the coordinates of the blit [buttonEasy = pygame.Rect(10, 150, 200, 90)]
for event in pygame.event.get():
if event.type == KEYDOWN and event.key == K_1:
MOVE_SPEED = 9
end_it_levels = True
new dilemma with kind of the same
end_it = False
while (end_it == False):
windowSurface.fill(WHITE)
for event in pygame.event.get():
if (event.type == pygame.MOUSEBUTTONDOWN and event.button == 1):
mouse_coordinates = pygame.mouse.get_pos()
if buttonStart.collidepoint(mouse_coordinates):
end_it_levels = True
windowSurface.blit(buttonStartImage, buttonStart)
pygame.display.flip()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The code until the specific loop:
import pygame, sys, random, math
from pygame.locals import *
from threading import Timer
pygame.init()
mainClock = pygame.time.Clock()
WINDOW_WIDTH = 640
WINDOW_HEIGHT = 400
windowSurface = pygame.display.set_mode ((WINDOW_WIDTH,
WINDOW_HEIGHT), 0)
pygame.display.set_caption('Catch the rabbits!')
BLACK = (0, 0, 0)
BLUE = (0, 0, 255)
WHITE = (255, 255, 255)
textFont = pygame.font.SysFont("impact", 60)
text = textFont.render("YOU WIN!", True, (193, 0, 0))
rabbitCounter = 0
NEW_RABBIT = 40
RABBIT_SIZE = 64
buttonStartImage = pygame.image.load('buttonStart.png')
background_image = pygame.image.load('bg.jpg').convert()
playerImage = pygame.image.load('Fox.png')
playerImageTwo = pygame.image.load('Fox2.png')
rabbitImage = pygame.image.load('topic_rabbit.png')
rabbitImageTwo = pygame.image.load('topic_rabbit2.png')
buttonEasyImage = pygame.image.load('buttonEasy.png')
buttonNormalImage = pygame.image.load('buttonNormal.png')
buttonHardImage = pygame.image.load('buttonHard.png')
player = pygame.Rect(420, 100, 40, 40)
buttonStart = pygame.Rect(220, 150, 200, 90)
buttonEasy = pygame.Rect(10, 150, 200, 90)
buttonNormal = pygame.Rect(220, 150, 200, 90)
buttonHard = pygame.Rect(430, 150, 200, 90)
rabbits = []
for i in range (20):
rabbits.append(pygame.Rect(random.randint(0, WINDOW_WIDTH
- RABBIT_SIZE), random.randint (0, WINDOW_HEIGHT - RABBIT_SIZE),
RABBIT_SIZE, RABBIT_SIZE))
moveLeft = False
moveRight = False
moveUp = False
moveDown = False
MOVE_SPEED = 0
end_it = False
while (end_it == False):
windowSurface.fill(WHITE)
for event in pygame.event.get():
if (event.type == pygame.MOUSEBUTTONDOWN and event.button == 1):
mouse_coordinates = pygame.mouse.get_pos()
if buttonStart.collidepoint(mouse_coordinates):
end_it_levels = True
windowSurface.blit(buttonStartImage, buttonStart)
pygame.display.flip()
Upvotes: 1
Views: 444
Reputation: 8292
You're going to have to add-in extra events to check for. There is a Rect
method just for this kind of thing called collidepoint
buttonEasy = pygame.Rect(10, 150, 200, 90)
buttonNormal = pygame.Rect(220, 150, 200, 90)
buttonHard = pygame.Rect(430, 150, 200, 90)
end_it_levels = False
while (end_it_levels == False):
windowSurface.fill(WHITE)
textlevelFont = pygame.font.SysFont("impact", 26)
level = textlevelFont.render("LEVEL:", True, (BLACK))
level_levels = textlevelFont.render("Easy = 1, Medium = 2 and Hard = 3"
, True, (BLACK))
for event in pygame.event.get():
if event.type == KEYDOWN and event.key == K_1:
MOVE_SPEED = 9
end_it_levels = True
if event.type == KEYDOWN and event.key == K_2:
MOVE_SPEED = 7
end_it_levels = True
if event.type == KEYDOWN and event.key == K_3:
MOVE_SPEED = 5
end_it_levels = True
# Is the left mousebutton being clicked?
if (event.type == pygame.MOUSEBUTTONDOWN and event.button == 1):
mouse_coordinates = pygame.mouse.get_pos()
if Your_Rect.collidepoint(mouse_coordinates):
# do stuff...
windowSurface.blit (level, (290, 115))
#windowSurface.blit (level_levels, (140, 150))
windowSurface.blit(buttonEasyImage, buttonEasy)
windowSurface.blit(buttonNormalImage, buttonNormal)
windowSurface.blit(buttonHardImage, buttonHard)
pygame.display.flip()
I'm not exactly sure if this is what you were wanting to do but, I hope this helps.
Upvotes: 1