pygame

Getting Pygame keyboard input and check if it's a number

家住魔仙堡 提交于 2020-12-19 04:20:45
问题 I am trying to get keyboard input using Pygame using this command: if event.type == pg.KEYDOWN: # ADD KEYBOARD EVENTS keys = pg.key.get_pressed() I want to check if the button pressed represents a number, I already know how to check if a string represent a number using try/except command, but, in my code keys is not a string, it is a huge tuple - and I don't know how to get it in an efficient way because every time I look in the internet on how to get keyboard input, they need to equate keys

python library pygame: centering text

梦想与她 提交于 2020-12-19 03:24:47
问题 I have a code: # draw text font = pygame.font.Font(None, 25) text = font.render("You win!", True, BLACK) screen.blit(text, [SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2]) How can I get text width and height methods like in java, so I can center text like: screen.blit(text, [SCREEN_WIDTH / 2 - text_w / 2, SCREEN_HEIGHT / 2 - text_h / 2]) If this is not possible, what is another way ? I've found this example, but I didn't really understand it. 回答1: You can get the dimensions of the rendered text image

python library pygame: centering text

久未见 提交于 2020-12-19 03:17:40
问题 I have a code: # draw text font = pygame.font.Font(None, 25) text = font.render("You win!", True, BLACK) screen.blit(text, [SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2]) How can I get text width and height methods like in java, so I can center text like: screen.blit(text, [SCREEN_WIDTH / 2 - text_w / 2, SCREEN_HEIGHT / 2 - text_h / 2]) If this is not possible, what is another way ? I've found this example, but I didn't really understand it. 回答1: You can get the dimensions of the rendered text image

python library pygame: centering text

自古美人都是妖i 提交于 2020-12-19 03:17:05
问题 I have a code: # draw text font = pygame.font.Font(None, 25) text = font.render("You win!", True, BLACK) screen.blit(text, [SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2]) How can I get text width and height methods like in java, so I can center text like: screen.blit(text, [SCREEN_WIDTH / 2 - text_w / 2, SCREEN_HEIGHT / 2 - text_h / 2]) If this is not possible, what is another way ? I've found this example, but I didn't really understand it. 回答1: You can get the dimensions of the rendered text image

Framerate affect the speed of the game

自古美人都是妖i 提交于 2020-12-18 05:00:10
问题 I am practicing on pygame and I was wondering how can we do so that the framerate does not affect the speed of execution of the game I would like FPS to not be locked and the game to always run at the same speed . Until now I used the pygame.time.Clock.tick function but the speed of the character was changing depending on the number of FPS, which I don't want. 回答1: pygame.time.Clock.tick returns the number of milliseconds passed since the previous call. If you call it in the application loop,

Framerate affect the speed of the game

 ̄綄美尐妖づ 提交于 2020-12-18 04:50:21
问题 I am practicing on pygame and I was wondering how can we do so that the framerate does not affect the speed of execution of the game I would like FPS to not be locked and the game to always run at the same speed . Until now I used the pygame.time.Clock.tick function but the speed of the character was changing depending on the number of FPS, which I don't want. 回答1: pygame.time.Clock.tick returns the number of milliseconds passed since the previous call. If you call it in the application loop,

Why is the PyGame animation is flickering

痴心易碎 提交于 2020-12-18 02:01:55
问题 So I run the code and it just starts glitching out. I am new to pygame. Here is the code: import pygame pygame.init() # Screen (Pixels by Pixels (X and Y (X = right and left Y = up and down))) screen = pygame.display.set_mode((1000, 1000)) running = True # Title and Icon pygame.display.set_caption("Space Invaders") icon = pygame.image.load('Icon.png') pygame.display.set_icon(icon) # Player Icon/Image playerimg = pygame.image.load('Player.png') playerX = 370 playerY = 480 def player(x, y): #

How do I stop more than 1 bullet firing at once?

我的未来我决定 提交于 2020-12-17 07:12:44
问题 import pygame pygame.init() red = 255,0,0 blue = 0,0,255 black = 0,0,0 screenWidth = 800 screenHeight = 600 gameDisplay = pygame.display.set_mode((screenWidth,screenHeight)) ## screen width and height pygame.display.set_caption('JUST SOME BLOCKS') ## set my title of the window clock = pygame.time.Clock() class player(): ## has all of my attributes for player 1 def __init__(self,x,y,width,height): self.x = x self.y = y self.height = height self.width = width self.vel = 5 self.left = False self

How do I change the color of a button once it is clicked in Pygame?

倖福魔咒の 提交于 2020-12-16 07:00:42
问题 I have been working on this project, and I have created a selection screen with functions and events so that when the mouse hovers over a button it turns orange. But it being a selection screen, once a button is clicked I want it to stay highlighted to show that that item has being selected but I can't seem to figure out how to do this. Here is my code: def button2(msg,x,y,w,h,ic,ac, action=None): mouse = pygame.mouse.get_pos() click = pygame.mouse.get_pressed() if x+w > mouse[0] > x and y+h

How do I change the color of a button once it is clicked in Pygame?

99封情书 提交于 2020-12-16 07:00:41
问题 I have been working on this project, and I have created a selection screen with functions and events so that when the mouse hovers over a button it turns orange. But it being a selection screen, once a button is clicked I want it to stay highlighted to show that that item has being selected but I can't seem to figure out how to do this. Here is my code: def button2(msg,x,y,w,h,ic,ac, action=None): mouse = pygame.mouse.get_pos() click = pygame.mouse.get_pressed() if x+w > mouse[0] > x and y+h