pygame

Collision detection against player and blocks in the map

扶醉桌前 提交于 2021-02-10 12:13:25
问题 I made a game map like this class Game_map: def __init__(self): self.land = pygame.image.load(r"C:\Users\name\OneDrive\Documents\A level python codes\final game\land.png").convert() self.height = 200 self.width = 200 self.land = pygame.transform.scale(self.land, (420, 250)) self.land.set_colorkey((0, 0, 0)) self.map_width = 6000 self.map_height = 2000 # 0 = emepty # 1 = land self.layout = [[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0 ], [ 0, 0

How to check collision of mouse with sprites in pygame?

我们两清 提交于 2021-02-10 12:12:32
问题 I replaced my mouse with a crosshair and I have some targets on the screen. When you shoot a target, it's supposed to disappear from the screen. How can I check if the CENTER of my crosshair (My actual mouse) is in collision with one of the sprites? The following code doesn't precisely check if the center of my crosshair is on one of the targets, so even if you shoot with the empty area in the crosshair it would count, and I don't want that. import pygame from random import randrange class

Pygame sound keeps repeating

狂风中的少年 提交于 2021-02-10 06:44:20
问题 I am trying to play a sound at the end of a game when there is a lose. Previously this code below worked with Python 3.5 but it would abort after it played the sound. I upgraded to python 3.6 and now it just keeps on repeating. How can I play the sound until the end? import pygame def sound(): pygame.mixer.init() sound1 = pygame.mixer.Sound('womp.wav') while True: sound1.play(0) return 回答1: while True is an endless loop: while True: sound1.play(0) The sound will be played continuously. Use

How do I make my monster move randomly in my game

淺唱寂寞╮ 提交于 2021-02-10 06:38:08
问题 I'm fairly new to programming and via a tutorial created a maze game which I've added too. It's all working well but I'd now like to add a monster or 2 to make it more of a challenge! I just want to keep it simple and have the monster randomly moving around the maze. I have created a monster but despite trying different things it won't move. I suspect I need to add something to the main loop but I don't know what. The code is below. Any help is appreciated! import os import random import

How to show shields level by changing color of the progress bar

ⅰ亾dé卋堺 提交于 2021-02-10 06:22:31
问题 I have a progress bar in the code which only shows a GREEN color but i want to modify it with the percentage of a player, if player is between 50 to 100% then its color is GREEN, if it is less than 50 and equal to 25 its color is ORANGE and under 25% is RED color. I have attached a python code I would be very thankful if anybody help def draw_shield_bar(surf, x, y, pct): I have a progress bar in the code which only shows a GREEN color but i want to modify it with the percentage of a player,

How to show shields level by changing color of the progress bar

别等时光非礼了梦想. 提交于 2021-02-10 06:22:03
问题 I have a progress bar in the code which only shows a GREEN color but i want to modify it with the percentage of a player, if player is between 50 to 100% then its color is GREEN, if it is less than 50 and equal to 25 its color is ORANGE and under 25% is RED color. I have attached a python code I would be very thankful if anybody help def draw_shield_bar(surf, x, y, pct): I have a progress bar in the code which only shows a GREEN color but i want to modify it with the percentage of a player,

Use collidelist in class

点点圈 提交于 2021-02-10 06:13:00
问题 I have created a class to create rectangles and put them in a list . I don't want them to collide so I use collidelist but it isn't working.rectangles are still colliding . I also want rectangles to move down and change x position when hit a specific point , I can do that but I am not sure if it is preventing collidelist from working Look the code below for more clarification. import pygame import random from pygame.locals import * import time pygame.init() a = 255,255,255 b = 0,0,0 c = 80

why collision between two moving objects on pygame dont work?

耗尽温柔 提交于 2021-02-10 05:13:46
问题 I am doing a snake game(there is two snakes on the game) with pygame and i want to detect when the snake head collides with the another snake body, do that for both and a special case when the both heads collides, im doing currently the collision between the snake head and the other snake body, it works fine if one of the snakes is frozen and the other is moving, but if both is moving the collision just dont work heres the code that moves the snakes: new_pos = None if direction == 'DOWN': new

How can I rotate my hitbox with my rotating and moving car in pygame?

ε祈祈猫儿з 提交于 2021-02-10 04:54:21
问题 I am trying to collide my car sprite with another rect, right now I am using rect collisions and my car collision video, but each time I rotate the hitbox it moves somewhere else. Is there a way I could collide my car sprite with that hitbox instead of using a car hitbox? My car class: class Car: def __init__(self, x, y, height, width, color): self.x = x - width / 2 self.y = y - height / 2 self.height = height self.width = width self.color = color self.rect = pygame.Rect(x, y, height, width)

How to draw a border around a sprite or image in pygame?

巧了我就是萌 提交于 2021-02-10 03:32:22
问题 I'd like to draw a red border around an image, but can't figure out how. I've tried to fill the image of the Hero sprite and set the colorkey to red self.image.set_colorkey(red) , but that makes the image invisible. Drawing a red rect onto the image, just filled it completely: pygame.draw.rect(self.image, red, [0, 0, width, height]) . I just want a red border that will help with collision detection in the future. The code in main.py : import pygame from pygame import * import sprites from