pygame

Sprite mask collision problems in pygame

泄露秘密 提交于 2019-12-20 03:52:35
问题 I am attempting to create a racing game in pygame. I want it such that when the car goes off the track, it slows down. I have tried to do this by having another sprite that is an outline of the track and when the car touches that sprite, it slows down. This does not work and I don't know why. Is there a better way to do this? Img is the car image Back is the racetrack BackHit is the outline I receive this error code: Traceback (most recent call last): File "C:\Users\Daniella\Desktop\Python

Trying to display a png file in pygame using pygame.display.update, and it shows for less than a second then disappears.

左心房为你撑大大i 提交于 2019-12-20 03:52:06
问题 The image is a playing card. We are using pygame 4.5 community edition and pycharm 2.6.9 because 2.7 does not support pygame (this is a school). Here is the code: import pygame pygame.init() picture=pygame.image.load("cards/S01.png") pygame.display.set_mode(picture.get_size()) main_surface = pygame.display.get_surface() main_surface.blit(picture, (0,0)) pygame.display.update() Why does the window disappear? 回答1: Try this: import pygame pygame.init() picture=pygame.image.load("cards/S01.png")

How to make a circle semi-transparent in pygame?

↘锁芯ラ 提交于 2019-12-20 03:40:48
问题 I'm somewhat new to pygame and trying to figure out how to make a circle semi-transparent. The trick however is that the background for the circle also has to be transparent. Here is the code I'm talking about: size = 10 surface = pygame.Surface(size, size), pygame.SRCALPHA, 32) pygame.draw.circle( surface, pygame.Color("black"), (int(size/2), int(size/2)), int(size/2), 2) I tried using surface.set_alpha(127) but that didn't work. I'm assuming because the surface is already transparent. Any

How can I stop a character in pygame from leaving the edge of the screen?

[亡魂溺海] 提交于 2019-12-20 03:23:26
问题 I have created a small program in pygame where the player controls a blue square moving around the screen, but I want to stop the player from moving past the edge of the screen. Here is the code I have so far, how can I do this? import pygame pygame.init() screen = pygame.display.set_mode((800, 600)) done = False x = 30 y = 30 clock = pygame.time.Clock() while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True if event.type == pygame.KEYDOWN and event.key ==

Pygame waiting the user to keypress a key

…衆ロ難τιáo~ 提交于 2019-12-20 03:16:38
问题 I am searching a method, where the program stops and waiting for a spesific key to be pressed by the user. May I can implement this one with a while loop? I need the best algorithm, if there exist a build-in function of waiting, to avoid the loop. I found several information on the official website of pygame, but nothing help. Here is a testing algorithms but won't work: key = "f" while key != "K_f": key = pygame.key.get_pressed() if key[Keys.K_f]: do something... 回答1: You could do it with a

Rotation based on end points

随声附和 提交于 2019-12-20 02:57:17
问题 I'm using pygame to draw a line between two arbitrary points. I also want to append arrows at the end of the lines that face outward in the directions the line is traveling. It's simple enough to stick an arrow image at the end, but I have no clue how the calculate the degrees of rotation to keep the arrows pointing in the right direction. 回答1: Here is the complete code to do it. Note that when using pygame, the y co-ordinate is measured from the top, and so we take the negative when using

Pygame collision detect with object and rect

ε祈祈猫儿з 提交于 2019-12-20 02:52:33
问题 Yep, I'm asking another question about this program :D Anyway, I currently a program that creates two lines on the screen with a gap in between them that can scroll. From here, I obviously need to see if the two objects are colliding. Since I only have one sprite and one rectangle I thought it was slightly pointless and overkill to make two classes for them. However, I can only find tutorials relating to classes which I obviously don't need. So, my question really is: Is it possible to test

Pygame screen freezes when I close it

我的梦境 提交于 2019-12-20 02:42:47
问题 The code loads up a pygame screen window, but when I click the X to close it, it becomes unresponsive. I'm running on a 64-bit system, using a 32-bit python and 32-bit pygame. from livewires import games, color games.init(screen_width = 640, screen_height = 480, fps = 50) games.screen.mainloop() 回答1: Mach1723's answer is correct, but I would like to suggest another variant of a main loop: while 1: for event in pygame.event.get(): if event.type == QUIT: ## defined in pygame.locals pygame.quit(

Font module error when using pygame2exe

只愿长相守 提交于 2019-12-20 02:39:20
问题 I used Pygame2exe from the Pygame website and that got rid of the problem with the .dlls but there's still an error that remains. The only font I'm using is the standard pygame font ( freesansbold.ttf ) and I've included a copy of it in my game directory. Also, I'm using the Font class and not the SysFonts class which I thought would fix my problem. Any thoughts? C:\Python26\apple_orchard\dist\appleorchard.exe:27: RuntimeWarning: use font: MemoryLoadLibrary failed loading pygame\font.pyd

Pygame only detects mouse events, does not detect KEYDOWN events

柔情痞子 提交于 2019-12-20 01:57:07
问题 I have written the following simple program which should print out all events detected by pygame.event.get() . import pygame, sys from pygame.locals import * display = pygame.display.set_mode((300, 300)) pygame.init() while True: for event in pygame.event.get(): print(event) if event.type == QUIT: pygame.quit() sys.exit() But when I run this I only have mouse events, and a KEYDOWN and KEYUP event when I hit caps-lock twice, being printed in terminal. When I use any other keys they only print