Can't move the image with collidepoint(event.pos)? - python

I've just recently become interested in pygame and the thing is my code doesn't work as what I intended: the image that I want to move with my mouse doesn't moving at all. So here's mine (contains code from previous question I saw):
import pygame,sys,os
WHITE = (255,255,255)
BLACK = (0,0,0)
GREY = (128,128,128)
class SilverGeneral:
def __init__(self,rect):
self.click = False
self.rect = pygame.Rect(rect)
def update(self,screen):
if self.click:
self.rect.center = pygame.mouse.get_pos()
pygame.init()
screen=pygame.display.set_mode([1000,600])
pygame.display.set_caption("Test")
silv = SilverGeneral((5,5,40,20))
silv.rect.center=screen.get_rect().center
clock = pygame.time.Clock()
image = pygame.image.load("c:\game\silvergeneral.bmp").convert()
while 1:
screen.fill(WHITE)
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONUP:
print(silv.rect.collidepoint(event.pos))
if silv.rect.collidepoint(event.pos):
print("True")
silv.click=True
elif event.type == pygame.MOUSEBUTTONDOWN:
print("False")
silv.click=False
elif event.type == pygame.QUIT:
pygame.quit()
sys.exit()
print (pygame.mouse.get_pos())
print (silv.rect.center)
silv.update(screen)
screen.blit(image,silv.rect)
clock.tick(10)
pygame.display.update()
I have been thinking for a whole hour and don't know why the collidepoint(event.pos) doesn't work. Also even if it's tested, the console never prints True.

collidepoint(event.pos) works just fine.
It seems that you want to be able to move the image once you click on it.
I guess your problem is that you expect it to work with clicking anywhere on that image, but you actually check if the mouse position is in the top left 40x20 pixel box of the image.
You can easily verify that by changing
screen.blit(image,silv.rect)
to
pygame.draw.rect(screen, pygame.color.THECOLORS['blue'], silv.rect, 0)
A good starting point is to use pygame's Sprite class and change your code to something like this:
class SilverGeneral(pygame.sprite.Sprite):
def __init__(self, *groups):
pygame.sprite.Sprite.__init__(self, *groups)
self.click = False
self.image = pygame.image.load("image.jpg").convert()
self.rect = self.image.get_rect()
def update(self):
if self.click:
self.rect.center = pygame.mouse.get_pos()
pygame.init()
screen=pygame.display.set_mode([1000,600])
pygame.display.set_caption("Test")
sprites = pygame.sprite.Group()
silv = SilverGeneral(sprites)
silv.rect.center = screen.get_rect().center
clock = pygame.time.Clock()
while 1:
screen.fill(WHITE)
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONUP:
silv.click = silv.rect.collidepoint(event.pos) and not silv.click
elif event.type == pygame.QUIT:
pygame.quit()
sys.exit()
sprites.update()
sprites.draw(screen)
clock.tick(60)
pygame.display.update()
The important thing here is that the rect is set to the rect of the image, so it will have the right size.

Related

Trouble with pygame rendering

I've got an issue where pygame is only rendering ablack screen with a strange glitchy green line on it.
I'm following a tutorial on RealPython.com and everything went smoothly with the first attempt. Once I did the second attempt this is what renders, which is clearly wrong.
#sidescrolling air-shooter
import pygame
#import pygame locals
from pygame.locals import(
K_UP,
K_DOWN,
K_LEFT,
K_RIGHT,
K_ESCAPE,
KEYDOWN,
QUIT,
)
#constants for screen width/height
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
class Player(pygame.sprite.Sprite):
def __init__(self):
super(Player, self).__init__()
self.surf = pygame.Surface((75,25))
self.surf.fill((255, 255,255))
self.rect = self.surf.get_rect()
pygame.init()
#create screen
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# Instantiate player
player = Player()
#keep the game running!
running = True
#loop time!
while running:
#look at all the events
for event in pygame.event.get():
#did the user hit a key?
if event.type == KEYDOWN:
#Was it escape? uh oh we gotta stop
if event.key == K_ESCAPE:
running = False
elif event.type ==QUIT:
running = False
#fill screen with white
screen.fill((255, 255, 255))
# Create a surface and pass in a tuple containing legth and width
surf = pygame.Surface((50, 50))
# Give the surface a color to separate it from the Background
surf.fill((0, 0, 0))
rect = surf.get_rect()
#This line says "Draw surf onto the screen at the center"
screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
#Draw the player on the screen
screen.blit(player.surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
pygame.display.flip()
pygame.quit()
I've even checked the source code for their project and it seems to match up.
It is a matter of Indentation. You need to draw the scene and update the display in the application loop instead of after the application loop:
#loop time!
while running:
#look at all the events
for event in pygame.event.get():
#did the user hit a key?
if event.type == KEYDOWN:
#Was it escape? uh oh we gotta stop
if event.key == K_ESCAPE:
running = False
elif event.type ==QUIT:
running = False
#-->| INDENTATION
#fill screen with white
screen.fill((255, 255, 255))
# Create a surface and pass in a tuple containing legth and width
surf = pygame.Surface((50, 50))
# Give the surface a color to separate it from the Background
surf.fill((0, 0, 0))
rect = surf.get_rect()
#This line says "Draw surf onto the screen at the center"
screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
#Draw the player on the screen
screen.blit(player.surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
pygame.display.flip()
pygame.quit()

Make pygame draw cubes wherever the user clicks

I want to make a kind of "level editor" as an exercise.
I've got this code:
import pygame
running = True
pygame.init()
screen = pygame.display.set_mode((800, 500))
class Cube:
def update(self):
self.cx, self.cy = pygame.mouse.get_pos()
self.square = pygame.Rect(self.cx, self.cy, 50, 50)
def draw(self):
pygame.draw.rect(screen, (255, 0, 0), self.square)
cube = Cube()
drawing_cube = False
drawing_cube2 = False
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
cube.update()
drawing_cube = True
screen.fill((0, 255, 0))
if drawing_cube:
cube.draw()
pygame.display.flip()
pygame.quit()
However, it doesn't create multiple squares; it just re-locates the already created square.
It's doing exactly what you told it to do: update the one and only Cube object in the game, and then redraw the screen and that one object. If you want multiple cubes, you have to create each one. Perhaps something like this:
cube_list = [] # List of all cubes
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN: # Make a new cube
cube = Cube()
cube.update()
cube_list.append(cube)
drawing_cube = True
screen.fill((0, 255, 0))
if drawing_cube: #New cube made; draw them all.
for cube in cube_list:
cube.draw()
pygame.display.flip()
Now, as an exercise for the student, can you simplify this so that it merely adds the most recent cube to the existing screen, instead of redrawing the entire game area for each new cube?
Thanks, Prune! but for anyone that is reading this after me, here is the full code:
import pygame
cube_list = []
running = True
pygame.init()
screen = pygame.display.set_mode((800, 500))
class Cube:
def update(self):
self.cx, self.cy = pygame.mouse.get_pos()
self.square = pygame.Rect(self.cx, self.cy, 50, 50)
def draw(self):
pygame.draw.rect(screen, (255, 0, 0), self.square)
cube = Cube()
drawing_cube = False
drawing_cube2 = False
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN: # Make a new cube
cube = Cube()
cube.update()
cube_list.append(cube)
drawing_cube = True
screen.fill((0, 255, 0))
if drawing_cube: #New cube made; draw them all.
for cube in cube_list:
cube.draw()
pygame.display.flip()
pygame.quit()
Cheers!
P.S I've marked your answer as accepted

Clicking on image will not work

I am using pygame to create a fully customizable enigma machine in python. One thing I decided to implement early is a help function. When I was testing this, nothing would show up on the console. Here is the code for the image clicking (not all of the code)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
pygame.display.quit()
if event.type == pygame.MOUSEBUTTONDOWN:
x, y = event.pos
if img.get_rect().collidepoint(x, y):
print('test')
How do I make this work? All help would be useful.
When you call img.get_rect() you create a pygame.Rect with the size of the image/surface and the default topleft coordinates (0, 0), i.e. your rect is positioned at the top left corner of your screen. I suggest creating a rect instance for the img at the beginning of the program and use it as the blit position and for the collision detection. You can pass the topleft, center, x, y, etc., coordinates directly as an argument to get_rect: rect = img.get_rect(topleft=(200, 300)).
import pygame as pg
pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
BG_COLOR = pg.Color('gray12')
img = pg.Surface((100, 50))
img.fill((0, 100, 200))
# Create a pygame.Rect with the size of the surface and
# the `topleft` coordinates (200, 300).
rect = img.get_rect(topleft=(200, 300))
# You could also set the coords afterwards.
# rect.topleft = (200, 300)
# rect.center = (250, 325)
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
elif event.type == pg.MOUSEBUTTONDOWN:
if rect.collidepoint(event.pos):
print('test')
screen.fill(BG_COLOR)
# Blit the image/surface at the rect.topleft coords.
screen.blit(img, rect)
pg.display.flip()
clock.tick(60)
pg.quit()

Cannot find reference 'load' in 'image.py'

I want to blit out my image on the screen with Pygame, but it doesn't work; It gives me a warning :
Cannot find reference 'load' in 'image.py'
And during execution time, it gives no such fatal error. All I get is a blank white screen with no image. I have referred to this and this, but just does not seem to work. This is my code:
import pygame
class Bouncer(object):
display_width = 600
display_height = 400
color_white = (255, 255, 255)
clock = pygame.time.Clock()
game_exit = False
bar_image = pygame.image.load('bar.png') # <------ here is the issue
def __init__(self):
pygame.init()
self.game_display = pygame.display.set_mode((self.display_width,
self.display_height))
pygame.display.set_caption('Bouncy Bouncer')
def showBar(self):
self.game_display.blit(self.bar_image, (10, 10))
def gameLoop(self):
while not self.game_exit:
for event in pygame.event.get():
#on close quit
if event.type == pygame.QUIT:
self.game_exit = True
pygame.quit()
quit()
#on key press quit
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_x:
self.game_exit = True
pygame.quit()
quit()
self.showBar()
self.game_display.fill(self.color_white)
pygame.display.update()
self.clock.tick(60)
game_instance = Bouncer()
game_instance.gameLoop()
I called fiest called my image then the fill function, so it overlapped the white background, thus, covering the image. So, what I did was first call the background, then the image.

Insert score in a button game function

I have this code for a button that is pressable:
def button(msg,xloc,yloc,xlong,ylong,b1,b2,action=None):
hover = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if xloc < hover [0] < xloc+xlong and yloc< hover [1] < yloc+ylong:
pygame.draw.rect(display, b1, (xloc ,yloc ,xlong,ylong))
if click [0]==1 and action != None:
action()
else:
pygame.draw.rect(gameDisplay, inactiveButton, (xloc ,yloc ,xlong,ylong))
label = pygame.font.SysFont("arial",16)
textSurf, textBox = textMsg(msg, label)
textBox.center = ((xloc +(300)),((yloc +(150))
gameDisplay.blit(textSurf,textBox)
and the code for the scoring is:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.MOUSEBUTTONDOWN:
score+=1
print (score)
I would like have a score that – upon pressing the correct button in the choices in order to answer in a quiz game – will be displayed and incremented by 1. How can I do that?
Here's the simplest way to implement a button that I know. Create a rect for the button and draw it with pygame.draw.rect (or blit an image). For the collision detection, check if the event.pos of a pygame.MOUSEBUTTONDOWN event collides with the rect and then just increment the score variable.
import pygame as pg
pg.init()
screen = pg.display.set_mode((640, 480))
GRAY = pg.Color('gray15')
BLUE = pg.Color('dodgerblue1')
def main():
clock = pg.time.Clock()
font = pg.font.Font(None, 30)
button_rect = pg.Rect(200, 200, 50, 30)
score = 0
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
if event.type == pg.MOUSEBUTTONDOWN:
if event.button == 1:
if button_rect.collidepoint(event.pos):
print('Button pressed.')
score += 1
screen.fill(GRAY)
pg.draw.rect(screen, BLUE, button_rect)
txt = font.render(str(score), True, BLUE)
screen.blit(txt, (260, 206))
pg.display.flip()
clock.tick(30)
if __name__ == '__main__':
main()
pg.quit()
Addendum: Actually, I would implement a button with the help of classes, sprites and sprite groups. If you don't know how classes and sprites work, I'd recommend to check out Program Arcade Games (chapter 12 and 13).
import pygame as pg
pg.init()
GRAY= pg.Color('gray12')
BLUE = pg.Color('dodgerblue1')
FONT = pg.font.Font(None, 30)
# The Button is a pygame sprite, that means we can add the
# instances to a sprite group and then update and render them
# by calling `sprite_group.update()` and `sprite_group.draw(screen)`.
class Button(pg.sprite.Sprite):
def __init__(self, pos, callback):
pg.sprite.Sprite.__init__(self)
self.image = pg.Surface((50, 30))
self.image.fill(BLUE)
self.rect = self.image.get_rect(topleft=pos)
self.callback = callback
def handle_event(self, event):
"""Handle events that get passed from the event loop."""
if event.type == pg.MOUSEBUTTONDOWN:
if self.rect.collidepoint(event.pos):
print('Button pressed.')
# Call the function that we passed during the
# instantiation. (In this case just `increase_x`.)
self.callback()
class Game:
def __init__(self):
self.screen = pg.display.set_mode((800, 600))
self.clock = pg.time.Clock()
self.x = 0
self.button = Button((200, 200), callback=self.increase_x)
self.buttons = pg.sprite.Group(self.button)
self.done = False
# A callback function that we pass to the button instance.
# It gets called if a collision in the handle_event method
# is detected.
def increase_x(self):
"""Increase self.x if button is pressed."""
self.x += 1
def run(self):
while not self.done:
self.handle_events()
self.run_logic()
self.draw()
self.clock.tick(30)
def handle_events(self):
for event in pg.event.get():
if event.type == pg.QUIT:
self.done = True
for button in self.buttons:
button.handle_event(event)
def run_logic(self):
self.buttons.update()
def draw(self):
self.screen.fill(GRAY)
self.buttons.draw(self.screen)
txt = FONT.render(str(self.x), True, BLUE)
self.screen.blit(txt, (260, 206))
pg.display.flip()
if __name__ == "__main__":
Game().run()
pg.quit()

Categories

Resources