![]() |
A 4x4 Field in Pygame - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Game Development (https://python-forum.io/forum-11.html) +--- Thread: A 4x4 Field in Pygame (/thread-20447.html) |
RE: A 4x4 Field in Pygame - Piethon - Aug-19-2019 (Aug-14-2019, 12:24 PM)Windspar Wrote: Make sure you install all sdl one libraries. You need mixer, ttf. Eh...mixer is an audio library. Why should I need that, for the game? And ttf impairs the power of my computer. It makes my computer slower. What do I need it for? RE: A 4x4 Field in Pygame - metulburr - Aug-19-2019 sdl-mixer does anything audio related. sdl-ttf is for fonts. pygame.init() initializes everything, including mixer and ttf. So if you want to do that you need to install the dependencies. RE: A 4x4 Field in Pygame - Windspar - Aug-19-2019 Quote:Eh...mixer is an audio library. Why should I need that, for the game?How many games have you played without sound. Quote:And ttf impairs the power of my computer. It makes my computer slower.It for fonts. Making text in pygame. Like to know how this makes your computer runs slower ? Only time code would be run. If you creating a font or rendering to pygame surface. How would you run other examples? That uses these. RE: A 4x4 Field in Pygame - Piethon - Aug-19-2019 (Aug-19-2019, 11:38 AM)Windspar Wrote:Quote:And ttf impairs the power of my computer. It makes my computer slower.It for fonts. Making text in pygame. But I can also use sys for making fonts in pygame. Here's a little example of how I did that once: import pygame import random import sys go = True pygame.init() screen = pygame.display.set_mode([500,500]) BackGround = pygame.image.load("background.jpg") screen.blit(BackGround, (0,0)) pygame.display.set_caption('Pygame test') pygame.display.flip() font = pygame.font.SysFont(None, 60) def writetext(outputtext,colour): text = font.render(outputtext, True, colour) size_rect = text.get_rect(center = (500/2, 54)) screen.blit(text,size_rect) pygame.display.flip() while go: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == pygame.MOUSEBUTTONDOWN: x = random.randint(1,2) screen.blit(BackGround, (0,0)) if x == 1: writetext("Yes!", (255,255,255)) elif x == 2: writetext("No!", (255,255,255))I think, I can do it with sys too. What do you think? RE: A 4x4 Field in Pygame - metulburr - Aug-19-2019 This is using all modules as you have initialized them here on line 7 pygame.init()So in your code you are already initializing mixer and ttf You can uses system fonts, but from my experience its not a good idea when packaging a game. For example some people may have certain fonts on their system while others not, causing an error for them. Its much more solid if you just send a font along with your program. PS you dont need all those flips everywhere. import pygame import random import sys go = True pygame.init() screen = pygame.display.set_mode([500,500]) pygame.display.set_caption('Pygame test') font = pygame.font.SysFont(None, 60) def writetext(outputtext,colour): text = font.render(outputtext, True, colour) size_rect = text.get_rect(center = (500/2, 54)) screen.blit(text,size_rect) while go: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == pygame.MOUSEBUTTONDOWN: x = random.randint(1,2) if x == 1: writetext("Yes!", (255,255,255)) elif x == 2: writetext("No!", (255,255,255)) pygame.display.update() RE: A 4x4 Field in Pygame - Windspar - Aug-19-2019 Reason for one update. 1. It can cause flickering. 2. It will slow down main loop. Another thing. Always render text before main loop. Otherwise you are using resources for no reasons. This also will slow down main loop. You want to get into structuring your code. It will help you learn. Here a start. Also creating text before main loop and how to blit text in main loop. import pygame import random import sys def create_text(font, output_text, colour, position): text = font.render(output_text, True, colour) rect = text.get_rect(center=position) return text, rect def draw_text(surface, data): surface.blit(data[0], data[1]) def main_loop(surface, images): text_image = None while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == pygame.MOUSEBUTTONDOWN: x = random.randint(1,2) if x == 1: text_image = images['yes'] elif x == 2: text_image = images['no'] # clear main surface surface.fill(pygame.Color('black')) if text_image: draw_text(surface, text_image) # render main surface to screen pygame.display.update() def main(): pygame.init() surface = pygame.display.set_mode([500,500]) pygame.display.set_caption('Pygame test') position = surface.get_width() / 2, 54 font = pygame.font.SysFont(None, 60) images = { 'yes': create_text(font, 'Yes', pygame.Color('white'), position), 'no': create_text(font, 'No', pygame.Color('white'), position) } main_loop(surface, images) main() RE: A 4x4 Field in Pygame - Piethon - Aug-19-2019 So know, I wanted to create the script, I've asked for in this tread. Till know, nothing is happening, when I run the code, but I don't get an error. import pygame import sys pygame.init() screen = pygame.display.set_mode([500,500]) BackGround = pygame.image.load("white.png") screen.blit(BackGround, (0,0)) pygame.display.set_caption("Pusqumbers") pygame.display.flip() mapDict = {} for x in range(1, 17): #repeat 16 times mapDict.update({x : None}) #None means it isn't occupied def can_move(x, y): if 0 <= x < 4 and 0 <= y < 4: return True return FalseThanks for your advices, with the example code, I showed above! :-) RE: A 4x4 Field in Pygame - Windspar - Aug-19-2019 Tell us what you want the code to do next time. Why would there be an error. You don't have a loop. You do stuff after flip. If you want to see what in mapDict. Then print it. RE: A 4x4 Field in Pygame - metulburr - Aug-19-2019 (Aug-19-2019, 06:49 PM)Piethon Wrote: Till know, nothing is happening, when I run the code, but I don't get an error.Nothing is happening because you dont have a main game loop to keep it open. It just runs to the end of the script and closes within a millisecond. RE: A 4x4 Field in Pygame - Piethon - Aug-20-2019 Sorry forgot the loop! import pygame import sys pygame.init() screen = pygame.display.set_mode([500,500]) BackGround = pygame.image.load("white.png") screen.blit(BackGround, (0,0)) pygame.display.set_caption("Pusqumbers") go = True while go: mapDict = {print("Hello")} for x in range(1, 17): #repeat 16 times mapDict.update({x : None}) #None means it isn't occupied def can_move(x, y): if 0 <= x < 4 and 0 <= y < 4: return True return False pygame.display.flip()My only problem is know, that I just have an empty window called "pygame window" and it is just loading endlessly and nothing shows up inside the window. |