所以我尝试在Pygame中编写我的第一个真实游戏(不使用精灵),但是使用pygame.rect进行的碰撞检测不起作用,所以我试图找到问题,然后把它传出了rect.mouve ip没用。 下面是源代码:
import pygame
from splayer import *
pygame.init()
window = pygame.display.set_mode((800,600))
pygame.display.set_caption('Mein erstes Spiel')
white = (255,255,255)
clock = pygame.time.Clock()
gravity = 1
player = Spieler(10,100)
g1 = pygame.image.load('gunter1.png')
g2 = pygame.image.load('gunter.png')
b1 = pygame.image.load('block.png')
gameLoop=True
y2 = 400
while gameLoop:
for event in pygame.event.get():
if (event.type == pygame.QUIT):
gameLoop=False
window.fill(white)
window.blit(b1, (10,y2))
block1 = pygame.Rect(10, y2, 100, 100)
window.blit(b1, (100,y2))
block2 = pygame.Rect(100, y2, 100, 100)
window.blit(b1, (200,y2))
block3 = pygame.Rect(200, y2, 100, 100)
window.blit(b1, (300,y2))
block4 = pygame.Rect(300, y2, 100, 100)
window.blit(b1, (400,y2))
block5 = pygame.Rect(400, y2, 100, 100)
rplayer = pygame.Rect(player.x, player.y, 100, 100)
rplayer.move_ip(player.x, player.y)
if rplayer.colliderect(block1) == True:
gravity = 0
player.update(gravit)
player.render(window, g1)
key = pygame.key.get_pressed()
if key[pygame.K_SPACE]:
window.blit(g2, (player.x,player.y))
if key[pygame.K_d]:
player.x += 20
if key[pygame.K_a]:
player.x -= 20
if key[pygame.K_w]:
player.y -= 20
pygame.display.flip()
clock.tick(10)
pygame.quit()
以下是我自己写过的玩家类“splayer.py”
class Spieler:
def __init__(self, x, y):
self.x = x
self.y = y
self.v = 0
self.rplayer = pygame.Rect(self.x, self.y, 100, 100)
def update(self, gravity):
self.v+=gravity
self.y+=self.v
def render(self, window, g1):
window.blit(g1, (self.x,self.y))
但是出现问题的部分显然是这部分:
rplayer = pygame.Rect(player.x, player.y, 100, 100)
rplayer.move_ip(player.x, player.y)
if rplayer.colliderect(block1) == True:
gravity = 0
我希望你能找到问题
我在实现代码方面仍有一些问题:
import pygame
from splayer3 import *
pygame.init()
window = pygame.display.set_mode((800,600))
pygame.display.set_caption('Mein erstes Spiel')
white = (255,255,255)
clock = pygame.time.Clock()
gravity = 1
player = Spieler
g1 = pygame.image.load('gunter1.png')
g2 = pygame.image.load('gunter.png')
b1 = pygame.image.load('block.png')
gameLoop=True
y2 = 400
while gameLoop:
for event in pygame.event.get():
if (event.type == pygame.QUIT):
gameLoop=False
window.fill(white)
window.blit(b1, (10,y2))
block1 = pygame.Rect(10, y2, 100, 100)
window.blit(b1, (100,y2))
block2 = pygame.Rect(100, y2, 100, 100)
window.blit(b1, (200,y2))
block3 = pygame.Rect(200, y2, 100, 100)
window.blit(b1, (300,y2))
block4 = pygame.Rect(300, y2, 100, 100)
window.blit(b1, (400,y2))
block5 = pygame.Rect(400, y2, 100, 100)
x, y = player.getPosition()
player.move(x, y)
#player.rplayer.move_ip(player.x, player.y)
if player.rplayer.colliderect(block1) == True:
gravity = 0
player.update(gravity,)
player.render(window, g1)
key = pygame.key.get_pressed()
if key[pygame.K_SPACE]:
window.blit(g2, (player.x,player.y))
if key[pygame.K_d]:
player.x += 20
if key[pygame.K_a]:
player.x -= 20
if key[pygame.K_w]:
player.y -= 20
pygame.display.flip()
clock.tick(10)
pygame.quit()
又是Spieler班:
import pygame
class Spieler:
def __init__(self):
self.v = 0
self.rplayer = pygame.Rect(10, 100, 100, 100)
def getPosition(self):
return self.rplayer.x, self.rplayer.y
def update(self, gravity):
self.v+=gravity
self.y+=self.v
def render(self, window, g1):
window.blit(g1, (self.x,self.y))
def move(self, x, y):
self.rplayer.move_ip(x, y)
我收到错误:
文件“spiel3.py”,第32行,in x,y = player.getPosition() TypeError:必须使用Spieler实例作为第一个参数调用未绑定方法getPosition()(没有取而代之)
答案 0 :(得分:1)
每次启动循环时都会创建一个rPlayer矩形。稍后你移动它,但永远不会保存矩形 - 你在下一次迭代时创建一个带有旧值的新矩形。
不要在Spiele类中使用x,y和rect值,而应该决定使用它并坚持使用它。我认为最好有一个rect,并创建一个函数,如果我们需要它们将返回坐标。
def getPosition(self):
return self.rplayer.x, self.rplayer.y
从self.x
班级中删除self.y
和Spiele
。
为了更清楚,我们可以为Spieler
创建一个移动函数,如下所示:
def move(x,y):
self.rplayer.move_ip(x,y)
最后,您要更改下面的移动代码以调用move()而不是修改player.x
和player.y