Have just started using PyGame in Python. I have managed to set the player_image to follow the mouse cursor, but the image is not centred over the position of the cursor. The cursor always seems to be in the top left corner of the image. Could anyone help me with getting it so that the image is over the centre of the mouse cursor? I can then turn the mouse cursor off and the image track the movement correctly.
I have the below section of code so far, with a background set in the pygame window:
player_image = pygame.image.load("spaceship.png").convert_alpha()
player_image_rect = player_image.get_rect()
player_image_rect.centerx = player_image.get_rect().centerx
player_image_rect.centery = player_image.get_rect().centery
screen.blit(player_image, player_image_rect)
pygame.mouse.set_visible(True)
Thanks
Full code (I've slowly been adding in mixture of features):
import sys
import pygame
import random
black = (0, 0, 0)
white = (255, 255, 255)
# Initialise PyGame
pygame.init()
#Set screen dimensions and title
width = 802
height = 585
screen=pygame.display.set_mode([width, height])
pygame.display.set_caption("Space Attack Game v1")
#Load and set up graphics position
background_position = [0, 0]
background_image = pygame.image.load("solarsystem.jpg").convert()
#Set game title on screen text
basicfont = pygame.font.SysFont(None, 68)
text = basicfont.render('Space Attack', True, white).convert_alpha()
text_rect = text.get_rect()
#textrect.centerx = screen.get_rect().centerx
#textrect.centery = screen.get_rect().centery
#Set player mouse control image
player_image = pygame.image.load("spaceship.png").convert_alpha()
player_image_rect = player_image.get_rect()
pos = pygame.mouse.get_pos()
player_image_rect.center = pos
#player_image_rect.centerx = player_image.get_rect().centerx
#player_image_rect.centery = player_image.get_rect().centery
screen.blit(player_image, player_image_rect.center)
pygame.mouse.set_visible(True)
#Load star
star_image = pygame.image.load("star.png").convert_alpha()
screen.blit(star_image, (random.randint(0,width),random.randint(0,height)) )
#star_image_rect = star_image.get_rect()
pygame.display.flip()
#######################################################
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
screen.blit(star_image, (random.randint(0,width),random.randint(0,height)))
class ball:
def __init__(self,X,Y):
self.velocity = [1,1]
self.ball_image = pygame.image.load('alien.png').convert()
self.ball_image.set_colorkey(black)
self.ball_boundary = self.ball_image.get_rect (center=(X,Y))
self.screen = pygame.display.get_surface()
#self.sound = pygame.mixer.Sound ('Collide.wav')
if __name__ =='__main__':
num_balls = 5
ball_list = []
for i in range(num_balls):
ball_list.append( ball(random.randint(0, width),random.randint(0, height)) )
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit(0)
for ball in ball_list:
if ball.ball_boundary.left < 0 or ball.ball_boundary.right > width:
#ball.sound.play()
ball.velocity[0] = -1 * ball.velocity[0]
if ball.ball_boundary.top < 0 or ball.ball_boundary.bottom > height:
#ball.sound.play()
ball.velocity[1] = -1 * ball.velocity[1]
ball.ball_boundary = ball.ball_boundary.move (ball.velocity)
screen.blit (ball.ball_image, ball.ball_boundary)
player_position = pygame.mouse.get_pos()
x = player_position[0]
y = player_position[1]
pygame.display.flip()
screen.blit(background_image, background_position)
screen.blit(text, text_rect)
screen.blit(player_image, [x,y])
pygame.quit()
Just set the center of your Rect to the mouse position, like
pos = pygame.mouse.get_pos()
player_image_rect.center = pos
Related
I am creating a game with pygame & python 3.10.0 using VSCode everything is working fine but this if statement stops working after sometime and the font doesn't draw but I can't pinpoint the problem for that , for the if statement it usually runs till 10 or 11 score but it can be quicker like 2:
if player.rect.colliderect(food):
pygame.sprite.Sprite.kill(food)
food.rect.x = random.randrange(20, 1700)
food.rect.y = random.randrange(20, 860)
all_sprites_list.add(food)
score += 1
print(score)
whole code:
import pygame
import sys
import time
import random
import ctypes
from ctypes import wintypes
from pygame import sprite
from pygame.draw import rect
from pygame.event import pump, wait
from pygame import font
pygame.font.init()
myappid = 'elementalstudios.snake.Alpha 0_1' # arbitrary string
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)
#------ Initialize Variables------#
# Player
player_width = 20
player_height = 20
# Food
food_width = 10
food_height = 10
# Colours
seafoam_gr = (159, 226, 191)
black = (0, 0, 0)
blue = (0, 0, 255)
red = (255, 0, 0)
green = (0, 255, 0)
white = (255, 255, 255)
# Score
score = 0
#------ Score Font Initialize ------#
font = pygame.font.Font(None, 50)
text = font.render("Score:", False, white, None)
#------Sprites Class------#
class Sprite(pygame.sprite.Sprite):
def __init__(self, color, height, width):
super().__init__()
self.image = pygame.Surface([width, height])
self.image.fill(seafoam_gr)
self.image.set_colorkey(black)
pygame.draw.rect(self.image,
color,
pygame.Rect(0, 0, width, height))
self.rect = self.image.get_rect()
def moveRight(self, pixels):
self.rect.x += pixels
def moveLeft(self, pixels):
self.rect.x -= pixels
def moveUp(self, speed):
self.rect.y -= speed * speed / 5
def moveDown(self, speed):
self.rect.y += speed * speed / 5
#------ Font Class ------#
def create_font(t,s = 24, colour_font = white, bold = False, italic = False):
font = pygame.font.Font("prstart.ttf", s, bold, italic)
text = font.render(t, True, colour_font)
return text
#------ Initialize Pygame and Window------#
pygame.init()
icon = pygame.image.load('Icon.ico')
pygame.display.set_icon(icon)
gameDisplay = pygame.display.set_mode((1920,1080), pygame.FULLSCREEN)
#rect = pygame.Rect( * gameDisplay.get_rect().center, 0, 0).inflate(100, 100)
pygame.display.set_caption("Blocky")
gameDisplay.fill(black)
clock = pygame.time.Clock()
running = True
#------Initialize Sprites------#
all_sprites_list = pygame.sprite.Group()
player = Sprite(seafoam_gr, player_height, player_width)
food = Sprite(red, food_height, food_width)
player.rect.x = 960
player.rect.y = 540
food.rect.x = 20 #random.randrange(20, 1800)
food.rect.y = 30 #random.randrange(20, 1050)
#------Add Sprites to sprite list------#
all_sprites_list.add(player)
all_sprites_list.add(food)
clock = pygame.time.Clock()
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
#------Eating------#
if player.rect.colliderect(food):
pygame.sprite.Sprite.kill(food)
food.rect.x = random.randrange(20, 1700)
food.rect.y = random.randrange(20, 860)
all_sprites_list.add(food)
score += 1
print(score)
#------ Score Draw ------#
text_rect = text.get_rect()
text_rect.center = (100, 150)
gameDisplay.blit(text, text_rect)
#------Key Movement------#
keys = pygame.key.get_pressed()
# Move Left
if keys[pygame.K_a]:
player.moveLeft(3)
if keys[pygame.K_LCTRL]:
player.moveLeft(5)
if keys[pygame.K_LEFT]:
player.moveLeft(3)
if keys[pygame.K_LCTRL]:
player.moveLeft(5)
# Move Right
if keys[pygame.K_d]:
player.moveRight(5)
if keys[pygame.K_LCTRL]:
player.moveRight(5)
if keys[pygame.K_RIGHT]:
player.moveRight(3)
if keys[pygame.K_LCTRL]:
player.moveRight(5)
# Move Down
if keys[pygame.K_s]:
player.moveDown(3)
if keys[pygame.K_LCTRL]:
player.moveDown(5)
if keys[pygame.K_DOWN]:
player.moveDown(3)
if keys[pygame.K_LCTRL]:
player.moveDown(5)
# Move Up
if keys[pygame.K_w]:
player.moveUp(3)
if keys[pygame.K_LCTRL]:
player.moveUp(5)
if keys[pygame.K_UP]:
player.moveUp(3)
if keys[pygame.K_LCTRL]:
player.moveUp(5)
all_sprites_list.update()
gameDisplay.fill(black)
all_sprites_list.draw(gameDisplay)
pygame.display.flip()
clock.tick(60)
pygame.quit()
quit()
It is not necessary to kill the food object. It is sufficient to change the position of the food. pygame.sprite.Sprite.kill just removes the Sprite object from all Gorups. So there is no point in calling kill and then adding the object back to the Group.
The text does not show up, because you draw it before you clear the display. pygame.Surface.fill fills the Surface with a solid color. Everything that was previously drawn will be cleared. blit the text after fill.
You will have to render the text Surface again if the score has changed.
I recommend simplifying the code that moves the player. See How can I make a sprite move when key is held down.
class Sprite(pygame.sprite.Sprite):
def __init__(self, color, x, y, height, width):
super().__init__()
self.image = pygame.Surface([width, height])
self.image.fill(color)
self.image.set_colorkey(black)
self.rect = self.image.get_rect(topleft = (x, y))
self.x, self.y = self.rect.x, self.rect.y
def move(self, move_x, move_y, speed_up):
if move_x or move_y:
direction = pygame.math.Vector2(move_x, move_y)
direction.scale_to_length(5 if speed_up else 3)
self.x += direction.x
self.y += direction.y
self.rect.x, self.rect.y = round(self.x), round(self.y)
player = Sprite(seafoam_gr, 400, 300, player_height, player_width)
food = Sprite(red, 20, 30, food_height, food_width)
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
if player.rect.colliderect(food):
food.rect.x = random.randrange(20, 780)
food.rect.y = random.randrange(20, 580)
score += 1
# render text
text = font.render("Score: " + str(score), False, white, None)
keys = pygame.key.get_pressed()
move_x = ((keys[pygame.K_d] or keys[pygame.K_RIGHT]) - (keys[pygame.K_a] or keys[pygame.K_LEFT]))
move_y = ((keys[pygame.K_s] or keys[pygame.K_DOWN]) - (keys[pygame.K_w] or keys[pygame.K_UP]))
player.move(move_x, move_y, keys[pygame.K_LCTRL])
all_sprites_list.update()
gameDisplay.fill(black)
# draw text
gameDisplay.blit(text, text.get_rect(center = (100, 150)))
all_sprites_list.draw(gameDisplay)
pygame.display.flip()
clock.tick(60)
pygame.quit()
quit()
This code is mostly just the generic start up of a pygame window but I'm trying to make it so the object moves (the planet object I've made) in the orbit around the sun object I've made for the coordinates I've given it. I know the x and y values are updating but I don't understand why the object doesn't move.
#import the library
import pygame
import math
#classes
class button:
def _init_ (self,screen, colour, x, y, width,height, letter):
self.screen = screen
self.colour = colour
self.x = x
self.y = y
self.width = width
self.height = height
self.letter = letter
self.radius = radius
def draw(self):
pygame.draw.rect(self.screen, self.colour,(self.x,self.y, self.width, self.height))
if self.letter!= '+' and self.letter!= '-':
font = pygame.font.SysFont('agencyfb',15,True,False)
else:
font = pygame.font.SysFont('agencyfb',25,True,False)
text = font.render(self.letter, True, black)
text_rect = text.get_rect(center=(self.x+self.width/2,self.y+self.height/2))
screen.blit(text, text_rect)
class orbit:
def __init__(self,screen,colour,x,y,radius,width):
self.screen = screen
self.colour = colour
self.x = x
self.y = y
self.width = width
self.radius = radius
def draw_circle(self):
pygame.draw.circle(self.screen,self.colour,(self.x,self.y),self.radius,self.width)
#define colours
##Sun = pygame.draw.circle(screen,Sun,[1000,450],100,0)
Black = (0,0,0)
White = (255,255,255)
Green = (0,255,0)
Red = (255,0,0)
Blue = (0,0,255)
Sun = (255,69,0)
Sun = []
Planet = []
#initialise the engine
pygame.init()
#Opening a window
size = (1920,1080)
screen = pygame.display.set_mode(size)
#set window title
pygame.display.set_caption("Orbit Simulator")
#loop unti the user clicks the close button
done = False
#
x=1000
y=450
Sun.append(orbit(screen,Red,1000,450,100,0))
Planet.append(orbit(screen,White,x,y,50,0))
#
#used to manage how fast the screen updates
clock = pygame.time.Clock()
#------ Main program Loop ------
while not done:
#--- Main event loop
for event in pygame.event.get(): #user did something
if event.type == pygame.QUIT: #if user clicked close
done = True #flag that we are done and exit the loop
#------ Game logic should go here ------
#------ Drawing code should go here -------
#first, clear the screen to white. Don't put other drawing commands above this or they will be erased with this command.
screen.fill(Black)
for i in Sun:
i.draw_circle()
for i in Planet:
r=150
angle=0
count = 0
while angle <= 360:
angle_radians = math.radians(angle)
x = int(math.cos(angle_radians)*r)
y = int(math.sin(angle_radians)*r)
angle +=1
count +=1
print(count)
x+=1000
y+=450
pygame.draw.circle(screen,White,[x,y],10,0)
print("Position [",x,",",y,"]")
#update the screen
pygame.display.flip()
#------ Limit to 60 frames per second ------
clock.tick(60)
#------ When the loop ends, quit ------
pygame.quit()
You can make an object rotate around another by using trigonometry or vectors. With vectors you just have to rotate a vector which defines the offset from the rotation center each frame and add it to the position vector (self.pos which is the rotation center) to get the desired self.rect.center coordinates of the orbiting object.
import pygame as pg
from pygame.math import Vector2
class Planet(pg.sprite.Sprite):
def __init__(self, pos, *groups):
super().__init__(*groups)
self.image = pg.Surface((40, 40), pg.SRCALPHA)
pg.draw.circle(self.image, pg.Color('dodgerblue'), (20, 20), 20)
self.rect = self.image.get_rect(center=pos)
self.pos = Vector2(pos)
self.offset = Vector2(200, 0)
self.angle = 0
def update(self):
self.angle -= 2
# Add the rotated offset vector to the pos vector to get the rect.center.
self.rect.center = self.pos + self.offset.rotate(self.angle)
def main():
pg.init()
screen = pg.display.set_mode((640, 480))
screen_rect = screen.get_rect()
clock = pg.time.Clock()
all_sprites = pg.sprite.Group()
planet = Planet(screen_rect.center, all_sprites)
yellow = pg.Color('yellow')
while True:
for event in pg.event.get():
if event.type == pg.QUIT:
return
all_sprites.update()
screen.fill((30, 30, 30))
pg.draw.circle(screen, yellow, screen_rect.center, 60)
all_sprites.draw(screen)
pg.display.flip()
clock.tick(60)
if __name__ == '__main__':
main()
pg.quit()
I am making a game with Pygame. For this game I need to be able to detect not only that two rectangles have collided, but also the point of collision between them. I looked at the documentation but couldn't seem to find any answers.
Is something like this possible?
You can use Rect.clip:
crops a rectangle inside another
clip(Rect) -> Rect
Returns a new rectangle that is cropped to be completely inside the argument Rect. If the two rectangles do not overlap to begin with, a Rect with 0 size is returned.
Here's an example:
import pygame
import random
class Stuff(pygame.sprite.Sprite):
def __init__(self, pos, color, *args):
super().__init__(*args)
self.image = pygame.Surface((30, 30))
self.image.fill(color)
self.rect = self.image.get_rect(center=pos)
self.pos = pygame.Vector2(pos)
def update(self):
self.rect.center = self.pos
def main():
pygame.init()
screen = pygame.display.set_mode((500, 500))
screen_rect = screen.get_rect()
font = pygame.font.SysFont(None, 26)
clock = pygame.time.Clock()
sprites = pygame.sprite.Group()
blocks = pygame.sprite.Group()
movement = {
pygame.K_UP: ( 0, -1),
pygame.K_DOWN: ( 0, 1),
pygame.K_LEFT: (-1, 0),
pygame.K_RIGHT: ( 1, 0)
}
for _ in range(15):
x, y = random.randint(0, 500), random.randint(0, 500)
color = random.choice(['green', 'yellow'])
Stuff((x, y), pygame.Color(color), sprites, blocks)
player = Stuff(screen_rect.center, pygame.Color('dodgerblue'))
sprites.add(player)
dt = 0
while True:
events = pygame.event.get()
for e in events:
if e.type == pygame.QUIT:
return
pressed = pygame.key.get_pressed()
move = pygame.Vector2()
for dir in (movement[key] for key in movement if pressed[key]):
move += dir
if move.length() > 0: move.normalize_ip()
player.pos += move * dt/5
sprites.update()
screen.fill(pygame.Color('black'))
sprites.draw(screen)
for block in pygame.sprite.spritecollide(player, blocks, False):
clip = player.rect.clip(block.rect)
pygame.draw.rect(screen, pygame.Color('red'), clip)
hits = [edge for edge in ['bottom', 'top', 'left', 'right'] if getattr(clip, edge) == getattr(player.rect, edge)]
text = font.render(f'Collision at {", ".join(hits)}', True, pygame.Color('white'))
screen.blit(text, (20, 20))
pygame.display.flip()
dt = clock.tick(60)
if __name__ == '__main__':
main()
I need to move my paddle image vertically by mouse, how can i do so? Here is my code. I want to move my mouse and associate movement with paddle as in pong game.
import pygame
pygame.init()
width = 900
height = 600
black = (0,0,0)
white = (255, 255, 255)
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Pong')
clock = pygame.time.Clock()
paddle1Img = pygame.image.load('paddle.png')
paddle1Img = pygame.transform.scale(paddle1Img,(600, 300))
paddle2Img = pygame.image.load('paddle.png')
paddle2Img = pygame.transform.scale(paddle2Img,(600, 300))
def paddle1(paddle1X, paddle1Y):
screen.blit(paddle1Img,(paddle1X, paddle1Y))
def paddle2(paddle2X, paddle2Y):
screen.blit(paddle2Img, (paddle2X, paddle2Y))
def gameloop():
paddle1X = -90
paddle1Y = 0
paddle2X = width - 125
paddle2Y = 0
gameOver = False
while not gameOver:
for event in pygame.event.get():
if(event.type == pygame.QUIT):
gameOver = True
if(event.type == pygame.MOUSEMOVE):
# i want to add here something that i cant understand that is how to associate paddleImg with mouse movement
screen.fill(white)
paddle1(paddle1X, paddle1Y)
paddle2(paddle2X, paddle2Y)
pygame.display.update()
clock.tick(60)
gameloop()
pygame.quit()
quit()
You have event pygame.MOUSEMOTION to get mouse move.
It has mouse positon event.pos which you can use to set paddle position.
Or you can get event.rel to see how much mouse was moved since previous MOUSEMOTION.
I use Surface to generate paddle so everyone can run it without images.
I use Rect to keep positon because it has not only x and y but also center, left, right, top, bottom, etc. so I can check "collision" with border.
import pygame
# --- constants --- (UPPER_NAMES)
WIDTH = 900
HEIGHT = 600
BLACK = (0 , 0, 0)
WHITE = (255, 255, 255)
# --- classes --- (CamelNames)
# empty
# --- functions --- (lower_names)
# empty
# --- main --- (lower_names)
# - init -
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
# - objects -
paddle1_image = pygame.surface.Surface((100,25))
paddle1_rect = paddle1_image.get_rect(x=90, y=10)
# - mainloop -
clock = pygame.time.Clock()
game_over = False
while not game_over:
# - events -
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
elif event.type == pygame.MOUSEMOTION:
# move padle with mouse
mouse_x, mouse_y = event.pos
paddle1_rect.centerx = mouse_x
# - updates (detect collision)-
# keep padle inside window
if paddle1_rect.left < 0:
paddle1_rect.left = 0
elif paddle1_rect.right > WIDTH:
paddle1_rect.right = WIDTH
# - draws -
screen.fill(WHITE)
screen.blit(paddle1_image, paddle1_rect)
pygame.display.update()
# - FPS -
clock.tick(30) # 30 FPS is enough for human eye to see animation
# - end -
pygame.quit()
#Importing Modules
import pygame
import sys
import random
#All pygame stuff under here
pygame.init()
#Font definitions
backFont = pygame.font.SysFont("monospace",40)
titleFont = pygame.font.SysFont("garamond", 100)
buttonFont = pygame.font.SysFont("garamond", 25)
bigFont = pygame.font.SysFont("garamond",100)
#Colour definitions
BackGray = pygame.Color('gray60')
screenGray = pygame.Color('gray80')
buttonGray1 = pygame.Color('gray40')
buttonGray2 = pygame.Color('gray50')
buttonGray3 = pygame.Color('gray30')
textColour = pygame.Color('navy')
#Screen size set
screen = pygame.display.set_mode((800, 600))
#Class for the buttons set here
class Button:
def __init__(self, x, y, width, height, colour, surface):
self.x = x
self.y = y
self.height = height
self.width = width
self.colour = colour
self.surface = surface
def isPressed(self):
mouse_position = pygame.mouse.get_pos()
mouse_x = mouse_position[0]
mouse_y = mouse_position[1]
if mouse_x > self.x:
if mouse_x < self.x + self.width:
if mouse_y > self.y:
if mouse_y < self.y + self.height:
mouse_click = pygame.mouse.get_pressed()
left_click = mouse_click[0]
if left_click:
self.colour = (0,0,0)
return True
self.colour = (230, 230, 230)
return False
def drawButton(self):
pygame.draw.rect(self.surface, self.colour, (self.x, self.y, self.width, self.height))
pygame.display.flip()
def FrontPage():
#Back screen
screen.fill(screenGray)
#The background is defined here
BinaryPage = []
for i in range(0,15):
BinaryString = ""
for j in range(0,33):
BinaryString += random.choice(["0","1"])
BinaryPage.append(BinaryString)
for i in range(0,15):
screen.blit(backFont.render(BinaryPage[i], 1, BackGray), (0,i * 40))
#The title is defined and printed here
Title1 = titleFont.render("The Six", 10, textColour)
Title2 = titleFont.render("Cipher", 10, textColour)
Title3 = titleFont.render("Simulator", 10, textColour)
screen.blit(Title1, (100, 100))
screen.blit(Title2, (100, 200))
screen.blit(Title3, (100, 300))
#Text for the button
buttonText = buttonFont.render("Continue", 10, textColour)
screen.blit(buttonText, (115,405))
#Where the buttons are defined and drawn
ButtonBig = Button(100, 450, 120, 60, buttonGray1, screen)
ButtonSmall = Button(105, 455, 110, 50, buttonGray2, screen)
ButtonBig.drawButton()
ButtonSmall.drawButton()
#Pygame While loop
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if ButtonBig.isPressed():
print("Hello")
break
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
clock.tick(60)
The above code runs a window that has 0's and 1's in a random order as the background, with the title of my program in the middle.
Then, there should be a button at the bottom, or a grey rectangle. However, when I try to .blit, or print, something onto the button frame, after I have drawn the button onto the screen, it does not appear.
Can anybody tell me why and how to fix it?
Or any alternatives of how to output text onto the buttons.
Thanks in advance
I'd first create a separate background surface and blit all the background graphics which never change onto it (the numbers in the following example). In the main loop you can just blit this background to clear the screen.
To blit text onto a button:
Pass the text to the __init__ method
create the button image (a pygame.Surface)
render the text
create rects for the image and the text (pass the center of the image rect to the text rect to center the text)
blit the text surface onto the image.
For the buttons I'd use sprites and put them into a sprite group, so that you can draw them simply by calling sprite_group.draw(screen). Sprites need an image and a rect attribute to work. Pygame rects have collision detection methods that you should use instead of writing your own. In this case we can use the collidepoint method and pass the event.pos to it.
import random
import pygame as pg
pg.init()
screen = pg.display.set_mode((800, 600))
FONT = pg.font.SysFont('garamond', 25)
SCREEN_GRAY = pg.Color('gray80')
BUTTON_GRAY = pg.Color('gray40')
TEXT_COLOUR = pg.Color('navy')
# Inherit from pg.sprite.Sprite. That will allow you to
# put the instances into a sprite group.
class Button(pg.sprite.Sprite):
def __init__(self, text, x, y, width, height, colour):
super().__init__()
# Create the image of the button.
self.image = pg.Surface((width, height))
self.image.fill(colour)
# A pygame.Rect will serve as the blit position.
self.rect = self.image.get_rect()
# Render the text.
txt = FONT.render(text, True, TEXT_COLOUR)
# This txt_rect is used to center the text on the image.
txt_rect = txt.get_rect(center=self.rect.center)
self.image.blit(txt, txt_rect)
# Set the position of the image rect.
self.rect.topleft = x, y
def is_pressed(self, event):
if event.type == pg.MOUSEBUTTONDOWN:
# MOUSE... events have an event.pos attribute (the mouse position)
# which you can pass to the collidepoint method of the rect.
if self.rect.collidepoint(event.pos):
return True
return False
def main():
button_big = Button('big button', 100, 250, 120, 60, BUTTON_GRAY)
button_small = Button('small button', 105, 455, 120, 50, BUTTON_GRAY)
buttons_group = pg.sprite.Group(button_big, button_small)
background = pg.Surface(screen.get_size())
background.fill(SCREEN_GRAY)
for i in range(screen.get_height()//40):
for j in range(screen.get_width()//40):
n = random.choice(['0', '1'])
background.blit(FONT.render(n, True, BUTTON_GRAY), (j*40, i*40))
clock = pg.time.Clock()
while True:
for event in pg.event.get():
if event.type == pg.QUIT:
return
if button_big.is_pressed(event):
print('big')
elif button_small.is_pressed(event):
print('small')
screen.blit(background, (0, 0))
# Blit the images of the contained sprites onto the screen.
buttons_group.draw(screen)
pg.display.flip()
clock.tick(60)
if __name__ == '__main__':
main()
pg.quit()