gpt4 book ai didi

python pygame 检查碰撞然后使项目返回到原始站点

转载 作者:行者123 更新时间:2023-12-01 06:42:01 26 4
gpt4 key购买 nike

12/20更新

我为船舶设置了不同的值,但最终站点未达到预期

def position(self,setting,ship,aliens):
self.ck_edge(setting,aliens)
aliens.update()
collide=pygame.sprite.spritecollideany(ship,aliens)
if collide:
aliens.empty()

self.create(setting,aliens)

ship.rect.center = ship.screen_rect.center
ship.rect.y = ship.screen_rect.bottom

ship.y =ship.rect.centery # when collide, the ship disappear
ship.x = ship.rect.centerx

#

def position(self,setting,ship,aliens):
self.ck_edge(setting,aliens)
aliens.update()
collide=pygame.sprite.spritecollideany(ship,aliens)
if collide:
aliens.empty()

self.create(setting,aliens)

ship.rect.center = ship.screen_rect.center
ship.rect.y = ship.screen_rect.bottom

ship.y =ship.rect.y #when collide, the x is not at the center and the y half of the ship under the screen
ship.x = ship.rect.x

#

def position(self,setting,ship,aliens):
self.ck_edge(setting,aliens)
aliens.update()
collide=pygame.sprite.spritecollideany(ship,aliens)
if collide:
aliens.empty()

self.create(setting,aliens)

ship.rect.center = ship.screen_rect.center
ship.rect.y = ship.screen_rect.bottom

ship.rect.centerx =ship.x #notching change when collide, the ship not move
ship.rect.centery = ship.y

================================================== =========================

我正在尝试实现一个功能,当飞船与外星人相撞时,将飞船返回到屏幕底部,重新启动游戏。

所以我在代码中使用pygame.sprite.spritecollideany(sprite,group)

首先我检查两个站点,如果返回 True 则清空外星人组并将飞船放到其起始站点

但是当我运行脚本时,效果不是我想要的,当飞船与外星人相撞时,飞船返回原点,整个屏幕停止了,没有东西可以移动,外星人和飞船都停止了

我修改了很多次,结果都没有改变,是什么原因导致这个错误?整个屏幕都停止了。

这是我的这个函数的代码:

def position(self,setting,ship,aliens):
self.ck_edge(setting,aliens)
aliens.update()
collide=pygame.sprite.spritecollideany(ship,aliens)
if collide:
aliens.empty()

self.create(setting,aliens)

ship.rect.center=ship.screen_rect.center
ship.rect.bottom=ship.screen_rect.bottom

这是全部代码:

#!/usr/bin/python
import sys,os
import pygame



class Setting():
def __init__(self,width,height):
self.w=width
self.h=height
self.flag=pygame.RESIZABLE
self.color=(255,255,255)
self.speed=1.5
self.ship=3
self.screen=pygame.display.set_mode((self.w,self.h),self.flag)
pygame.display.set_caption("Muhaha")
self.bullet_s=3
self.bullet_w=5
self.bullet_h=20
self.bullet_n=10
self.bullet_c=(0,0,0)

class Alien(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
pic=pygame.image.load("/home/finals/python/alien/image/ship.jpg").convert_alpha()
self.image=pygame.transform.smoothscale(pic,(100,100))
self.rect=self.image.get_rect()
self.x = float(self.rect.x)
self.rect.x=(self.rect.width)
self.speed=1
self.direction = 1

def create(self,setting,aliens):
spacex=setting.w-(self.rect.x)*2
spacey=(setting.h)/2-self.rect.y
alien_number=int(spacex/(2*(self.rect.width)))
alien_row=int(spacey/(2*(self.rect.height)))
for row in range(alien_row):
for number in range(alien_number):
alien=Alien()
alien.x=alien.rect.x+2*alien.rect.width*number
alien.rect.x=alien.x
alien.rect.y=alien.rect.y+2*alien.rect.height*row
aliens.add(alien)

def ck_edge(self,setting,aliens):
for alien in aliens.sprites():
if alien.x > setting.w or alien.x < 0:
alien.change_direction(aliens)
break
def change_direction(self,aliens):
for alien in aliens.sprites():
alien.rect.y +=50
alien.direction *= -1

def update(self):
self.x += (self.speed*self.direction)
self.rect.x =self.x


def position(self,setting,ship,aliens):
self.ck_edge(setting,aliens)
aliens.update()
collide=pygame.sprite.spritecollideany(ship,aliens)
if collide:
aliens.empty()

self.create(setting,aliens)

ship.rect.center=ship.screen_rect.center
ship.rect.bottom=ship.screen_rect.bottom

def blit(setting,aliens):
aliens.draw(setting.screen)

class Ship():
def __init__(self,setting):
bk=pygame.image.load("/home/finals/python/alien/image/muha.png").convert()
self.bkg=pygame.transform.smoothscale(bk,(setting.w,setting.h))
temp=pygame.image.load("/home/finals/python/alien/image/title.jpg").convert_alpha()
self.ship=pygame.transform.smoothscale(temp,(200,200))
self.screen=setting.screen
self.screen_rect=self.screen.get_rect()
self.rect=self.ship.get_rect()



self.rect.center=self.screen_rect.center
self.rect.bottom=self.screen_rect.bottom

self.x=float(self.rect.centerx)
self.y=float(self.rect.centery)


def blit_ship(self):
self.screen.blit(self.ship,self.rect)
def blit_screen(self):
self.screen.blit(self.bkg,(0,0))



class Function():
def __init__(self):
self.moving_up = False
self.moving_down = False
self.moving_left = False
self.moving_right = False
def event(self,setting,ship):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
self.moving_up=True
elif event.key == pygame.K_DOWN:
self.moving_down=True
elif event.key == pygame.K_LEFT:
self.moving_left=True
elif event.key == pygame.K_RIGHT:
self.moving_right=True
elif event.key == pygame.K_ESCAPE:
sys.exit()

elif event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
self.moving_up=False
elif event.key == pygame.K_DOWN:
self.moving_down=False
elif event.key == pygame.K_LEFT:
self.moving_left=False
elif event.key == pygame.K_RIGHT:
self.moving_right=False

def move(self,setting,ship):

if self.moving_up == True and ship.rect.top >= 0:
ship.y -= setting.speed
if self.moving_down == True and ship.rect.bottom <= setting.h:
ship.y += setting.speed
if self.moving_left == True and ship.rect.left >= 0:
ship.x -= setting.speed
if self.moving_right == True and ship.rect.right <= setting.w:
ship.x += setting.speed
ship.rect.centerx=ship.x
ship.rect.centery=ship.y



def game():
pygame.init()
setting=Setting(1200,800)
function=Function()
ship=Ship(setting)
alien=Alien()
aliens=pygame.sprite.Group()
alien.create(setting,aliens)

while True:
ship.blit_screen()
function.event(setting,ship)

Alien.blit(setting,aliens)
ship.blit_ship()
function.move(setting,ship)
alien.position(setting,ship,aliens)

pygame.display.flip()
game()

最佳答案

船舶的实际位置并不存储在属性ship.rect中,而是存储在(self.x, self.y)。当然,船舶是在 ship.rect 中存储的位置绘制的,但该位置是从 (self.x, self.y >).

您还必须重置(self.xself.y):

class Alien(pygame.sprite.Sprite):

# [...]

def position(self,setting,ship,aliens):
# [...]

collide=pygame.sprite.spritecollideany(ship,aliens)
if collide:
# [...]

ship.rect.center = ship.screen_rect.center
ship.rect.bottom = ship.screen_rect.bottom

# INSERT THE FOLLOWING 2 LINES
ship.x = float(ship.rect.centerx)
ship.y = float(ship.rect.centery)

关于python pygame 检查碰撞然后使项目返回到原始站点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59410461/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com