Python “游戏精灵”;“碰撞”;在他们接触之前
使用python和pygame编写和创建游戏的新手。游戏类似于飞鸟,但在太空和管道是流星 在精灵碰撞时创建事件,但是pygame.sprite.spritecollide函数似乎工作不正常。该功能在精灵实际触摸前一英寸左右(屏幕上)触发。如果玩家精灵跌入流星精灵,则事件不会触发Python “游戏精灵”;“碰撞”;在他们接触之前,python,pygame,Python,Pygame,使用python和pygame编写和创建游戏的新手。游戏类似于飞鸟,但在太空和管道是流星 在精灵碰撞时创建事件,但是pygame.sprite.spritecollide函数似乎工作不正常。该功能在精灵实际触摸前一英寸左右(屏幕上)触发。如果玩家精灵跌入流星精灵,则事件不会触发 import pygame import random # Initialize the game engine pygame.init() # Define colors WHITE = (255, 255, 2
import pygame
import random
# Initialize the game engine
pygame.init()
# Define colors
WHITE = (255, 255, 255)
# Define done
done = False
def create_meteor():
meteor = Meteor(WHITE, width, height)
meteor_sprites_list.add(meteor)
class Player(pygame.sprite.Sprite):
# This class will be the sprite controlled by player
# -- Methods
def __init__(self, filename, color, HW, HH):
# Constructor function
# Call parent'c constructor
super().__init__()
# Set height, width
self.image = pygame.image.load("player.png").convert_alpha()
# Set background color to transparent
self.image.set_colorkey(color)
# Make top-left corner the passed in locatin
self.rect = pygame.rect.Rect((HW, HH), self.image.get_size())
# How much to add to current player position
self.dy = 0
def ignite(self):
self.dy = -400
def update(self, dt, screen):
#apply gravity
self.dy = min(400, self.dy + 40)
self.rect.y += self.dy * dt
self.rect.topleft = (self.rect.x, self.rect.y)
# Blit image to screen
screen.blit(self.image, (320, self.rect.y))
pygame.display.flip()
# Define new clas for meteor
class Meteor(pygame.sprite.Sprite):
def __init__(self, color, width, height):
# Takes in parameters for color, width (x position) , and height (y postion)
# Call the parent class
super().__init__()
# Make list of image file location
self.meteor_list = ["meteors/meteor1.png"]
# Randomly select meteor from above list
self.new_meteor = random.choice(self.meteor_list)
# Load graphic that is in file folder
self.image = pygame.image.load(self.new_meteor).convert_alpha()
# Set background to transparent
self.image.set_colorkey(color)
# Fetch the rectangle object that has the dimensions of the image
self.rect = self.image.get_rect()
# Random starting location
self.rect.x = random.randrange(width, (width + 100))
self.rect.y = random.randrange(0, height)
# Random movement to the left
self.change_x = random.randrange(-10,-5)
self.change_y = random.randrange(-4,3)
# ---- Attributes
# What meteor does each cycle through
def update(self):
# Move bad block down 3 at a time
self.rect.x += self.change_x
self.rect.y += self.change_y
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
background_size = pygame.image.load("background.png")
# Get dimensions of background
width = background_size.get_width()
height = background_size.get_height()
HW, HH = width/2, height/2
size = (width, height)
screen = pygame.display.set_mode(size)
# Load image for star background
background = pygame.image.load("background.png").convert_alpha()
# Seperate becuase error when placed before screen
# Creates a list of sprites. Each object in program is added to list. Managed by a class called "group"
meteor_sprites_list = pygame.sprite.Group()
# Create spaceship
player = Player("player.png", WHITE, HW, HH)
# Create meteor sprites on the screen
create_meteor()
#-----Main Program Loop
while not done:
dt = clock.tick(30)
# Main event Loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.MOUSEBUTTONDOWN:
player.ignite()
#-----Game Logic
# Draw background
screen.blit(background, (0,0))
# Update Sprites
# Update meteor sprite
meteor_sprites_list.update()
# Update player sprite
player.update(dt/1000. , screen)
# Draw meteors
meteor_sprites_list.draw(screen)
# Check to see if player has collided with meteor
meteor_hit_list = pygame.sprite.spritecollide(player, meteor_sprites_list, True, pygame.sprite.collide_circle)
# -- Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# Make sure to quit
pygame.quit()
如有任何建议,我们将不胜感激和欢迎。谢谢你看 如果碰撞检测出现问题,通常可以绘制(或打印)相关精灵的矩形。 您没有在矩形的x坐标处闪烁图像,因此矩形实际上将更向右
screen.blit(self.image, (320, self.rect.y)) # self.rect.x is equal to `HW` not 320.
只需在rect处闪烁图像即可:
screen.blit(self.image, self.rect)
重现您的问题并不容易,因为需要20多个资产。你能提供一个重现问题的例子吗?谢谢你的建议。为预编辑的长度道歉。代码中有一些部分可以改进。我建议你把你的游戏发布在上面,以获得一些提示,但它只允许在那里发布完整的、有效的示例。谢谢你的推荐。一旦我的比赛结束,我就去那里!我想你已经可以发布当前形式的程序了,你只需要修复rect问题,这样它就没有bug了。我只有几个建议。需要立即删除的一件事是玩家的
update
方法中的pygame.display.flip()
调用。你应该每帧只调用一次,否则图形会闪烁,这对性能也不好。谢谢你的回答。我更改了代码,一切都正常工作。