This question already has an answer here:
How can I make a button that goes to website in pygame?
(1 answer)
Closed 1 year ago.
Can I add a link into a Pygame? Like in HTML; once it was clicked, we will be redirected to the URL.
I have the game 'Turn Based Battle' -> https://www.pygame.org/project/5492/7939
I want to add a text somewhere on the screen with the link they can press on.
If you want to look at the code you can check on the Github page from the creator, it's basically the same like the game I have now. -> https://github.com/russs123/Battle
All you have to do is check if the mouse press event happened inside the text's rect. Then you can use webbrowser.open() to open the link in a browser.
Sample Code:
import pygame
import webbrowser
pygame.init()
screen = pygame.display.set_mode((1000, 800))
link_font = pygame.font.SysFont('Consolas', 50)
link_color = (0, 0, 0)
running = True
while running:
screen.fill((255, 255, 255))
rect = screen.blit(link_font.render("Sample Link", True, link_color), (50, 50))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
pos = event.pos
if rect.collidepoint(pos):
webbrowser.open(r"https://stackoverflow.com/")
if rect.collidepoint(pygame.mouse.get_pos()):
link_color = (70, 29, 219)
else:
link_color = (0, 0, 0)
pygame.display.update()
Related
This question already has answers here:
Why is my PyGame application not running at all?
(2 answers)
What is the difference between .quit and .QUIT in pygame
(2 answers)
Closed 1 year ago.
Basically I wrote this simple script to just open a pygame window and just close it when pg.Quit (256) event occurs but it wont work. When I manually check event.type it always returns 32776 which im not event able to find in the pg.event.get() list. I have event tried inserting pg.Quit as 256 it didnt work
import pygame as pg
import sys
pg.init()
def q():
pg.quit()
sys.exit()
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
FPS = 60
screen = pg.display.set_mode((800,800))
clock = pg.time.Clock()
rect = pg.Rect((0, 0), (32, 32))
image = pg.Surface((32, 32))
image.fill(white)
clock.tick(FPS)
for event in pg.event.get():
#when I mannualy check event.type it only returns 32776
#I v even tried using 256 instead of pg.Quit()
if event.type == pg.Quit():
q()
I am making a text based RPG as my first python/pygame project. In my game I would like to present the player with choices then ask them to type it in. I was able to download and import a module which accepts user input and displays it on the screen. However, before using it for anything like..lets say, if the user input is 'yes', then they go to a new area, I'd like the program to only accept the user input once they hit the enter key. I believe the tutorial for the textinput module I downloaded has directions to do this, but to be honest I just don't understand what it's saying. I've tried multiple types of loops but nothing is happening. Any help will be appreciated. Here is my main game code:
import pygame_textinput
import pygame
pygame.init()
#fps
clock=pygame.time.Clock()
# create font here
font_name = pygame.font.get_default_font()
WHITE_TEXT_COLOR = (255, 255, 255)
# create screen and window and display and font here
screen_width, screen_height = 800, 700
background_color_black = (0, 0, 0)
screen = pygame.display.set_mode((screen_width, screen_height))
our_game_display = pygame.Surface((screen_width, screen_height))
pygame.display.set_caption('MyRPGGame')
#create text input object
textinput = pygame_textinput.TextInput()
def draw_text(text, size, x, y):
pygame.font.init()
font = pygame.font.Font(font_name, size)
text_surface = font.render(text, True, WHITE_TEXT_COLOR)
text_rect = text_surface.get_rect()
text_rect.center = (x, y)
our_game_display.blit(text_surface, text_rect)
def choose_to_play():
draw_text("You've decided to play",20,screen_width/2,screen_height/2+50)
def first_area():
our_game_display.fill(background_color_black)
draw_text('The story of this game depends on your choices. Do you wish to play?', 20, screen_width / 2,screen_height / 2 - 100)
draw_text('Type your answer and hit enter.', 20, screen_width / 2,screen_height / 2 - 50)
draw_text('Yes', 20, screen_width/2,screen_height/2+50)
draw_text('No', 20, screen_width/2,screen_height/2+100)
screen.blit(our_game_display, (0, 0))
pygame.display.update()
while True:
our_game_display.fill((background_color_black))
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
exit()
first_area()
# Feed it with events every frame
textinput.update(events)
# Blit its surface onto the screen
screen.blit(textinput.get_surface(), (10, 600))
pygame.display.update()
clock.tick(30)
Now here is the source for the pygame textinput module, figured I would link to the code so this post isn't too crowded:
https://github.com/Nearoo/pygame-text-input
You need to a variable for the state of the game. Once enter is pressed change the state. Implement different cases in the application loop depending on the state of the game::
game_state = 'start'
while True:
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
game_state = 'input'
our_game_display.fill((background_color_black))
if game_state == 'input':
textinput.update(events)
# [...]
This question already has answers here:
pygame - How to display text with font & color?
(7 answers)
Closed 1 year ago.
import pygame, sys
pygame.init()
clock = pygame.time.Clock()
coordinate = pygame.mouse.get_pos()
screen = pygame.display.set_mode((1000,800), 0, 32)
pygame.display.set_caption("Mouse Tracker")
font = pygame.font.Font(None, 25)
text = font.render(coordinate, True, (255,255,255))
while True:
screen.fill((0,0,0))
screen.blit(text, (10, 10))
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
clock.tick(60)
I am trying to make a program that tracks your mouse and shows the coordinates of it on the sreen. I get an error saying:
text = font.render(coordinate, True, (255,255,255))
TypeError: text must be a unicode or bytes.
I am using Python 3.9.1
There are couple of changes to be made:
coordinate = pygame.mouse.get_pos() returns a tuple which the variable coordinate is assigned to. The font.render() method takes a string as an argument not a tuple. So first you need to render the str(coordinate) and not just the coordinate which is actually a tuple. You can read more about rendering the font in pygame here
text = font.render(str(coordinate), True, (255,255,255)) #Rendering the str(coordinate)
Step one alone won't make your code functional, there are still some problems in your code. For blitting the coordinate of the mouse to the screen, you need to get the mouse coordinates at every single frame. For that to function you need to place the line coordinate = pygame.mouse.get_pos() in the while True loop, along with this you also need to place this line text = font.render(str(coordinate), True, (255,255,255)) in the while loop
import pygame,sys
#[...]#part of code
while True:
coordinate = pygame.mouse.get_pos() #Getting the mouse coordinate at every single frame
text = font.render(str(coordinate), True, (255,255,255))
#[...] other part of code
So the final working code should look somewhat like:
import pygame, sys
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode((1000,800), 0, 32)
pygame.display.set_caption("Mouse Tracker")
font = pygame.font.Font(None, 25)
while True:
coordinate = pygame.mouse.get_pos() #Getting the mouse coordinate at every single frame
text = font.render(str(coordinate), True, (255,255,255)) #Rendering the str(coordinate)
screen.fill((0,0,0))
screen.blit(text, (10, 10))
for event in pygame.event.get():
if event.type == pygame.QUIT:#
pygame.quit()
sys.exit()
pygame.display.update()
clock.tick(60)
This question already has answers here:
Pygame mouse clicking detection
(4 answers)
How do I detect if the mouse is hovering over a button? PyGame button class is not displaying the text or changing colour on hover
(1 answer)
Closed 2 years ago.
I'm new to using Pygame and have a question about MOUSEBUTTONDOWN and pygame.draw.circle()
When clicking the 62.5x62.5 square inside of the screen , I want it to show a green circle and when
clicking it again, I want it to disappear. I don't know how to do it and in my code I have to spam click the square for the circle to appear for a split second then it disappears again. I've also tried using a while loop but couldn't make it work.
import pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
running = True
while running:
screen.fill((0, 0, 0))
mouse_pos = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN and mouse_pos[0] >= 150 and mouse_pos[0] <= 212.5 \
and mouse_pos[1] >= 425 and mouse_pos[1] <= 487.5:
pygame.draw.circle(screen, (152, 251, 152), (181.25, 393.75), 20)
pygame.display.update()
You need to draw the circle in the event loop rather than in the event loop. Add a Boolean variable draw_circle and toggle the state of the variable when you click the mouse.
You can't draw with floating point accuracy. Pixels and the mouse position have integral units.
Use a pygame.Rect object and collidepoint() for the click test. The position of the mouse click is stored in the pos attribute of the pygame.event.Event() object:
import pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
draw_circle = False
test_rect = pygame.Rect(0, 0, 40, 40)
test_rect.center = (181, 393)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
if test_rect.collidepoint(event.pos):
draw_circle = not draw_circle
screen.fill((0, 0, 0))
pygame.draw.rect(screen, (127, 127, 127), test_rect, 1) # for debugging
if draw_circle:
pygame.draw.circle(screen, (152, 251, 152), (181, 393), 20)
pygame.display.update()
This question already has answers here:
Why is my pygame application loop not working properly?
(1 answer)
Pygame window not responding after a few seconds
(3 answers)
Closed 2 years ago.
I've look for the docs, many video on YouTube, questions on stackoverflow but I still can't fix it.This is my code:
import pygame
pygame.init()
run = True
#color def
BK = (0, 0, 0)
WT = (255, 255, 255)
GY = (127, 127, 127)
#create screen
screen = pygame.display.set_mode((800,600))
#title, icon
pygame.display.set_caption("Clock")
icon = pygame.image.load("clock.png")
pygame.display.set_icon(icon)
#loop
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.draw.circle(screen, WT, (0, 0), 175, width=1)
You missed to update the display by by either pygame.display.update() or pygame.display.flip(). And you need to draw circle in the application loop. Care about the Indentation:
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
#-->| INDENTATION
# clear display
screen.fill(0)
# draw scene (circle)
pygame.draw.circle(screen, WT, (0, 0), 175, width=1)
# update display
pygame.display.flip()