我本质上是想用pygame做一个“实心”的对象。这样做的目的是在玩家接触时击退他们。我目前使用的(但不能正常工作)如下:
keys_pressed = pygame.key.get_pressed()
if 1 in keys_pressed:
if keys_pressed[K_w]:
self.player_l[1] += -2
if self.player_r.colliderect(self.tower_r): self.player_l[1] -= -2
if keys_pressed[K_a]:
self.player
我做了一些游戏,但我仍然是新的制作游戏。我制作了这个RPG游戏,这个代码只是游戏的一部分。游戏对游戏的不同级别执行不同的Pygame文件,如代码的这一部分所示(这是用于随机战斗的):
if (mainCharacterCoordinates.colliderect(battlefield) and (keys_pressed[pygame.K_DOWN] or keys_pressed[pygame.K_UP] or keys_pressed[pygame.K_RIGHT] or keys_pressed[pygame.K_LEFT])):
f = open('x.txt
由于这两个操作使用相同的逻辑,我不明白为什么一个可以工作,而另一个不能。下面是我的代码:
# Make a ship.
ship = Ship(ai_settings, screen)
# Make a group to store bullets
bullets = Group()
# Make the enemy
enemys = Group()
all_sprites = Group()
all_sprites.add(ship)
# Start the main loop for the game.
while True:
check_events(ai_settings
我想用鼠标选择一张卡片(卡片改变为另一张带有橙色边缘的卡片的图像),移动它(或不移动),然后取消再次点击卡片,将它返回到卡片的原始图像(没有橙色边缘)。我做了两个第一步,但我找不到取消选择卡片的方法。
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
sys.exit()
if event.type==pygame.MOUSEBUTTONDOWN:
if event.button==1 and mouse_rect.collide
#Main problem should be around this function
def pick_ups(health_regenerate,damage_buff,speed_increase):
print("pickups")
#Checks if player collides with health_regenerate, then makes health =
100
if player.colliderect(health_regenerate):
print('collidehealth'
我只想把这段代码优化到最大值,我不介意知道优化是否已经在最大值,或者我是否做错了运动和碰撞。我的游戏是一个2d的我的风格的游戏,所以它将有很多的other_rects。希望我的代码容易读懂。
代码:
def move(rect, movement, other_rects): # move one axis at a time, returns moved rect and info on collisions
collisions = [0, 0, 0, 0] # left, right, up, down
if movement.x:
rect.x +=
我有一个Box对象数组,每个对象都有一个rect属性。我正在试着检查我的玩家是否与阵列中的任何一个rects发生碰撞。按照它现在的工作方式,逻辑只在数组的最后一个元素上工作,所以我的玩家只会与一个just和他刚刚走过的所有其他元素发生碰撞。我认为可能有更好的方法来检查与任何rects的碰撞,但下面是我所拥有的:
class Box:
def __init__(self, a, b, c, d):
self.rect = pygame.Rect(a, b, c, d)
..。
def detect_collisions(self, player_obj):
for b in ra
我想不出一种方法来检测玩家是否在Pygame的rect中点击过。我试过了
self.mouserect=(pygame.mouse.get_pos(), 8,8)
然后后来
if self.click: #(this is true if mouse button is down)
if self.mouserect.colliderect(self.a_thing_to_click_on.rect):
do_stuff
但这给了我一个AttributeError:'tuple‘对象没有'colliderect’属性。我做
colliderect()函数不会返回True和False;相反,它返回0和1。当我使用函数类型时,它显示int而不是bool。如果我使用0和1而不是布尔值编写代码,那么代码可以工作,但是为什么会发生这种情况呢?Pygame文档没有提到任何关于它的内容。 def collisions():
tileHitsList = []
for rect in tileList:
print(testcube.colliderect(rect)) #it weirdly prints 0 and 1 instead of False and True
#
我在理解colliderect如何与精灵一起工作时遇到了一些麻烦。我对它有一个很好的想法,但是每当我尝试将它实现到我的游戏中时,我都会收到错误消息"attributeError:'pygame.surface‘object has no attribute 'rect'“
ufo_lvl_1 = pygame.image.load("ufo1.png")
bullet = pygame.image.load("bullet.png")
class Bullet(pygame.sprite.Sprite):
def _
我在pygame上做了一个小项目。这是一个架空的2D游戏,但如果玩家在一个物体后面,我需要玩家在它后面的一层。如果玩家在对象的前面,那么它需要在对象的前面一个层。
for image, imagerect, imageypos in zip(blitimages, blitrects, blitypositions):
if realy < imageypos:
screen.blit(playerimage, playerimagerect)
screen.blit(image, imagerect)
if realy >
我需要一些帮助来重置我做的一个游戏。我已经让主循环开始工作了,碰撞检测也工作了。我正在尝试在游戏中立即重启,一个只是重置分数并重新开始的游戏-我不希望在它再次重启游戏之前有任何用户输入。
MoveAsteroids()只是在屏幕上移动小行星,这是玩家必须避免的。它也是这样的函数,每次小行星被闪避时,分数都会增加1。
def game_loop():
global score
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT
我试图使一个新的矩形出现时,与一个障碍和障碍被删除,但del命令不像我看到的教程中的工作,只有当形状与障碍物碰撞时,物体才会出现。如何使新的rect在碰撞后永久出现,而不仅仅是在碰撞时,以及如何修复del命令无效?
#just the code for the collision
if main.colliderect(obstacle):
del obstacle
pygame.draw.rect(window, color2, pygame.Rect(xr2, yr2, 30, 30))
#all of the code
import pygame
i
这段代码只有在我声明打印(“abc”)时才能工作,否则它就完全不能工作了,没有任何单独的原因,我使用pygame来执行我所做的扫雷项目。
作品:
for Sprite in self.CellsSprites:
if Sprite.rect.colliderect(self.rect):
print("abc")
if time.time() - self.time > 0.1 and self.block == False:
self.block = True
self.time =
我有一个代码,使玩家在二维移动,没有重力(像艾萨克或世界最难的游戏)。问题在于与地图中的瓷砖(celle)的碰撞:
def collisions(self):
player = self.player.sprite
for cell in self.celle.sprites():
if cell.rect.colliderect(player.rect):
if player.direction.x < 0:
player.rect.left = cell.rect.right
我试图让碰撞为在列表中创建的所有区域工作,所以当球触及块[]列表中创建的区域时,它将弹出,并附加(删除)一个块,就像在游戏突破中一样。我试过:
for block in blocks:
if blockRect.colliderect(ball):
speed[1] = -speed[1]
blocks.append(blockRect)
和
if blockRect.colliderect(ball):
speed[1] = -speed[1]
blocks.append(blockRect)
编辑:这是尝试和测试
在此之前,我使用代码来表示“每按一次按钮它就会移动”,但现在我将其更改为“当您按下按钮时,它会向一个方向移动,直到有进一步的指令”,但现在碰撞器无法工作。它以前是有效的,但我仍然是pygame的新手。我发现了一些类似的问题,但我认为我已经做了提出问题的人所做的事情。任何帮助都会被接受。
import pygame, sys, random
from pygame.locals import *
from time import sleep
pygame.init()
def render():
windowSurface.fill(black)
player = pygame
我试图创造一个小的,简单的游戏。但到目前为止我遇到了很多麻烦。(我对电子游戏相当陌生)
这个问题是从这个代码中产生的:
#The blocks' code
for block in blocklist:
#Blocks Collide
if any(block.rect.colliderect(x.rect) for x in blocklist if x is not block):
x=(int(mse[0]) / 32)*32
y=(int(mse[1]) / 32)*32
所以,我在游戏中制作了这个2D游戏,当我试图跳出一个平台时,我的玩家会浮起来,而不是秋天的。我一直试图用真实和错误的陈述来使它发挥作用,但大多数时候我的球员根本不动,我也试过告诉它,如果它不与平台碰撞,这是行不通的。
这是我尝试过的事情之一
for Platform in platforms:
if not playerman.rect.colliderect(Platform.rect):
playerman.y -= playerman.speed
我的游戏演示
import pygame
pygame.init()
# width and he
当我注意到我的文件名不是我想要的那样时,我正在处理代码中另一个不工作的部分(这可以在代码的注释中找到)。我关闭了VS代码,更改了文件名,它开始产生这个错误。我不知道是怎么回事,我在互联网上只能找到“初始化游戏”或“把pygame.quit()放到循环之外”,在这两种情况下我都做了。我甚至试着更新游戏。
import pygame
import random
# starts up all the upper levels stuff like the window and framerate
pygame.init()
win = pygame.display.set_mode((1028,