I can successfully get my game to fully resize now and the keyboard events are working fine, but the mouse events will only trigger when I place the pointer where the objects originally had blit to the surface even after I resize the screen. I feel like an idiot asking but ive tried almost everything and I keep getting the same problem.
Edit: I added the actionbar class in the code incase that would be where the bug would be Ill explain further that what I have going on is I have an image of a window with 3 buttons that have text "examine, useitem, sleep".
I have 3 images that I can switch to where the text on each button in the image glows yellow I also have a transparent rectangle image the size of each button. What I am trying to accomplish is that when I make the mouse hover on a button it switches the image so it displays a glowing button. I had it working the way I intended except when I added the code to resize the game when I resize the window it only changes the images when the mouse hovers over the location the buttons originate at.
UPDATE: Im adding a link to my google drive folder with the whole code and assets here incase anyone wants to reproduce this in there Python IDE for a more hands-on look. https://drive.google.com/drive/folders/1EnAZrMG4omJwMkss8z3QvVZfYcPidG9a?usp=sharing
import pygame
import sys
from pygame.locals import *
from config import *
from graphics import *
from gameobjects import *
from guiobjects import *
pygame.init()
win = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT), HWSURFACE|DOUBLEBUF|RESIZABLE)
resizescreen = win.copy()
pygame.display.set_caption("TESTGAME")
clock = pygame.time.Clock()
class actionbar(object):
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.inactive = False
self.examine = False
self.useitem = False
self.sleep = False
def draw_AB(self, win):
if self.examine == True:
win.blit(examineBar, (self.x,self.y))
elif self.useitem == True:
win.blit(useitemBar, (self.x,self.y))
elif self.sleep == True:
win.blit(sleepBar, (self.x,self.y))
else:
win.blit(actionBar, (self.x,self.y))
class Button(object):
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.image = pygame.image.load("ASSETS/transbutton.png")
def draw_button(self, win):
win.blit(self.image, (self.x,self.y,self.width,self.height))
def on_button(self, mouse):
if mouse[0] > self.x and mouse[0] < self.x + self.width:
if mouse[1] > self.y and mouse[1] < self.y + self.height:
return True
return False
def draw_grid():
for x in range(0, WIN_WIDTH, TILESIZE):
pygame.draw.line(win, GREY, (x, 0), (x, WIN_HEIGHT))
for y in range(0, WIN_HEIGHT, TILESIZE):
pygame.draw.line(win, GREY, (0, y), (WIN_WIDTH, y))
def redrawGameWindow():
resizescreen.fill((0, 0, 0))
draw_grid()
actbar.draw_AB(resizescreen)
examine.draw_button(resizescreen)
useitem.draw_button(resizescreen)
sleep.draw_button(resizescreen)
adv.draw(resizescreen)
win.blit(pygame.transform.scale(resizescreen, win.get_rect().size), (0, 0))
pygame.display.flip()
pygame.display.update()
#mainloop
adv = player(300, 410, 32, 32)
actbar = actionbar(0, 480, 704, 98)
examine = Button(75, 512, 150, 30)
useitem = Button(291, 512, 150, 30)
sleep = Button(505, 512, 150, 30)
run = True
while run:
clock.tick(12)
for event in pygame.event.get():
mouse = pygame.mouse.get_pos()
if event.type == pygame.QUIT:
run = False
if event.type == VIDEORESIZE:
win = pygame.display.set_mode(event.size, HWSURFACE|DOUBLEBUF|RESIZABLE)
if event.type == pygame.MOUSEMOTION:
if examine.on_button(mouse):
actbar.examine = True
actbar.useitem = False
actbar.sleep = False
elif useitem.on_button(mouse):
actbar.examine = False
actbar.useitem = True
actbar.sleep = False
elif sleep.on_button(mouse):
actbar.examine = False
actbar.useitem = False
actbar.sleep = True
else:
actbar.examine = False
actbar.useitem = False
actbar.sleep = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and adv.x > adv.vel:
adv.x -= adv.vel
adv.left = True
adv.right = False
adv.up = False
adv.down = False
elif keys[pygame.K_RIGHT] and adv.x < 700 - adv.width - adv.vel:
adv.x += adv.vel
adv.left = False
adv.right = True
adv.up = False
adv.down = False
elif keys[pygame.K_UP] and adv.y > adv.vel:
adv.y -= adv.vel
adv.left = False
adv.right = False
adv.up = True
adv.down = False
elif keys[pygame.K_DOWN] and adv.y < 475 - adv.height - adv.vel:
adv.y += adv.vel
adv.left = False
adv.right = False
adv.up = False
adv.down = True
else:
adv.left = False
adv.right = False
adv.up = False
adv.down = False
adv.walkCount = 0
redrawGameWindow()
pygame.quit()