[英]Difference between rect.move() and rect.move_ip in pygame
[英]Pygame rect.move_ip does not work
因此,我嘗試在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行,在x中,y = player.getPosition()TypeError:必須使用Spieler實例作為第一個參數調用未綁定方法getPosition()。
每次啟動循環時,您都在創建一個rPlayer矩形。 稍后,您將其移動,但矩形將永遠不會保存-您在下一次迭代時使用舊值創建一個新矩形。
而不是在Spiele類中具有x,y和rect值,您應該選擇一個並堅持使用。 我認為最好有一個rect,並創建一個函數,如果需要的話可以返回坐標。
def getPosition(self):
return self.rplayer.x, self.rplayer.y
從Spiele
類中刪除self.x
和self.y
為了更加清楚,我們可以為Spieler
創建一個移動函數,如下所示:
def move(x,y):
self.rplayer.move_ip(x,y)
最后,您想更改下面的移動代碼以調用move(),而不是修改player.x
和player.y
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.