pygame

pygame detecting mouse cursor over object

℡╲_俬逩灬. 提交于 2021-01-29 04:26:04
问题 I want to print a statement when the mouse cursor hovers over an image I loaded onto the screen, but it only prints when the mouse cursor hovers over the top left portion of the screen even if the image is in the center or bottom right. import pygame, sys from pygame import * def main(): pygame.init() FPS = 30 fpsClock = pygame.time.Clock() screen = pygame.display.set_mode((600, 400)) cat = pygame.image.load('cat.png') while True: if cat.get_rect().collidepoint(pygame.mouse.get_pos()): print

How does the collidepoint function in pygame work? [duplicate]

╄→尐↘猪︶ㄣ 提交于 2021-01-29 04:13:53
问题 This question already has answers here : How do I detect collision in pygame? (4 answers) Pygame mouse clicking detection (4 answers) Closed 3 hours ago . So I am doing a lab regarding pong and I was lost on the concept of collidepoint a function in pygame, which checks whether the center of the circle is inside the paddle. Upon looking at documentation I was still confused since I am very new to programming and I am really having a hard time settling in and getting a grasp of the new skills

blitting pygame.Surface() onto pygame.OPENGL display

时光毁灭记忆、已成空白 提交于 2021-01-29 03:52:51
问题 How can I blit a pygame.Surface() object onto a pygame.OPENGL display and flip the display? import pygame pygame.init() RES = (640, 480) display = pygame.display.set_mode(RES, pygame.FULLSCREEN | pygame.OPENGL) bg_img = pygame.Surface(RES) bg_img.fill((255, 255, 255)) display.blit(bg_img, (0, 0)) pygame.quit() sys.exit() gives me Traceback (most recent call last): File "C:/Game Dev/TESTS/Clock Comparison/opengl_test.py", line 12, in <module> display.blit(bg_img, (0, 0)) error: Cannot blit to

How do I make objects never overlap?

依然范特西╮ 提交于 2021-01-28 21:45:01
问题 I am trying to make a car game in pygame, but whenever I try to spawn the cars, some of them get on top of each other, how do I fix it? Here is the code for spawning the car: if len(enemiesL) == 0: if len(enemiesR) == 0: wavelengthL += 3 for _ in range(wavelengthL): enemy = EnemyL(random.randrange(160, Width/2 - 50), random.randrange(-1500, -100), random.choice(["black", "blue", "brown", "darkblue", "orange", "pale", "purple", "red", "white", "yellow"])) enemiesL.append(enemy) if len(enemiesR

tkinter and pygame do not want to work in one window [duplicate]

只愿长相守 提交于 2021-01-28 21:12:03
问题 This question already has answers here : Unable to install pygame on Python via pip (Windows 10) (6 answers) Closed 8 days ago . I want to create an application and want to use tkinter as GUI and one of game libraries as Pyglet and Pygame. I didn't find any info about embedding pyglet into tkinter but found some code with tkinter and pygame: Embedding a Pygame window into a Tkinter or WxPython frame. I'm using python 3.7 and here is the code that I use: import pygame import tkinter as tk from

How do I make objects never overlap?

≡放荡痞女 提交于 2021-01-28 21:02:32
问题 I am trying to make a car game in pygame, but whenever I try to spawn the cars, some of them get on top of each other, how do I fix it? Here is the code for spawning the car: if len(enemiesL) == 0: if len(enemiesR) == 0: wavelengthL += 3 for _ in range(wavelengthL): enemy = EnemyL(random.randrange(160, Width/2 - 50), random.randrange(-1500, -100), random.choice(["black", "blue", "brown", "darkblue", "orange", "pale", "purple", "red", "white", "yellow"])) enemiesL.append(enemy) if len(enemiesR

Make the background of an image transparent in Pygame with convert_alpha

隐身守侯 提交于 2021-01-28 20:18:01
问题 I am trying to make the background of an image transparent in a Pygame script. Now the background in my game is black, instead of transparent. I read somewhere else that I could use convert_alpha , but it doesn't seem to work. Here is (the relevant part of) my code: import PIL gameDisplay = pygame.display.set_mode((display_width, display_height)) img = pygame.image.load('snakehead1.bmp').convert_alpha(gameDisplay) What am I doing wrong? Thanks in advance! 回答1: To make an image transparent you

trying to get sprite to disappear from screen after certain amount of time

橙三吉。 提交于 2021-01-28 19:51:59
问题 i am trying to get my explosion image to disappear after a bullet collision with an alien. i am able to get the explosion to appear when the collision happens but can't seem to get it to disappear afterwards. i would like the explosion image to disappear after maybe 1 second or so. i understand that it would possibly involve a game clock and a kill method? thank you. import sys import pygame from settings import Settings from ship import Ship from bullet import Bullet from alien import Alien

Multiple Bullets pygame

这一生的挚爱 提交于 2021-01-28 16:34:01
问题 I know there have been several topics on this but I still can't figure out how to make my robot shot multiple bullets... I want press my Space Button and shoot multiple bullets but i still can shoot only one bullet. I know that my code is a mess so I'm sorry. Thanks for help! https://github.com/20nicolas/Game.git Here is my code: import pygame as py import os py.init () screen = py.display.set_mode ((800,600)) bg = py.image.load('game-assets-game-background-sidescroller.png') clock = py.time

why when the x and y size are not the same the grid is not showing properly

限于喜欢 提交于 2021-01-28 12:06:01
问题 I have this code that is used to display a grid but when size_x and size_y are not the same the grid is not shown properly. For exemple, if size_x = 16 and size_y =8 , the grid that is displayed is this: If size_x and size_y are the same everything works fine, if size_x and size_y are the same in this case there value is 16: import pygame # Define some colors BLACK = (0, 0, 0) WHITE = (255, 255, 255) GREEN = (0, 255, 0) RED = (255, 0, 0) # This sets the WIDTH and HEIGHT of each grid location