pygame 对象不会移动
pygame object wont move
我有一个非常简单的程序。我想要的是 class 中的物品自行移动。
import pygame
import time
import random
import threading
#initilasies it
pygame.init()
#variables for height and width
global display_width
display_width= 800
global display_height
display_height= 600
#declares colours uses RGB as reference
white= (255,255,255)
black = (0,0,0)
#sets the dispaly (must be inside a tuple ())
gameDisplay = pygame.display.set_mode((display_width,display_height))
#changes the name of the window
pygame.display.set_caption("Robot Quest")
#times stuff (is gonna be used for FPS)
clock = pygame.time.Clock()
#loads up an image (not shown) must be in same directory
tankImg = pygame.image.load("tank.png")
blockImg = pygame.image.load("block.png")
class things:
def __init__(self,width,height,speed):
self.width = width
self.height = height
#if display.width doesn't work just pass the screen dimensions
self.X = display_width - self.width
self.Y= display_height - self.height
self.speed = speed
def move(self):
self.X -= self.speed
pos = self.X
return pos
def drawImage(self,imageName,x,y):
gameDisplay.blit(imageName,(x,y))
def game_loop():
#game exit value is set
game_exit = False
#when true you exit the loop, logic goes here
while not game_exit:
for event in pygame.event.get():
#method below on what to do if they press x in the corner
if event.type == pygame.QUIT:
#exit the loop
pygame.quit()
quit()
#fills the background
gameDisplay.fill(white)
block = things(100,100,4)
block.drawImage(blockImg,block.X,block.Y)
block.move()
pygame.display.update()
clock.tick(30)
game_loop()
pygame.quit()
quit()
在程序中,block.move() 只执行一次,仅此而已,所以对象停留在同一个位置,只移动了一次位置。我试图将 block.move() 函数放在 for 和 while 循环中,但如果我这样做,程序不会 运行。任何人都可以告诉我如何修复我的代码以使对象连续移动,以便它从一端移动到屏幕到另一端吗?
问题是您在 while 循环中重新初始化您的块,因此在每次迭代中您都将其重置到其原始位置然后移动它。尝试将初始化移到 while 循环之外:
def game_loop():
#game exit value is set
game_exit = False
block = things(100,100,4)
#when true you exit the loop, logic goes here
while not game_exit:
for event in pygame.event.get():
#method below on what to do if they press x in the corner
if event.type == pygame.QUIT:
#exit the loop
pygame.quit()
quit()
#fills the background
gameDisplay.fill(white)
block.drawImage(blockImg,block.X,block.Y)
block.move()
pygame.display.update()
clock.tick(30)
您似乎在每个循环中都初始化了您的块。尝试将 block = things(100,100,4)
移动到 while 循环之前。
我有一个非常简单的程序。我想要的是 class 中的物品自行移动。
import pygame
import time
import random
import threading
#initilasies it
pygame.init()
#variables for height and width
global display_width
display_width= 800
global display_height
display_height= 600
#declares colours uses RGB as reference
white= (255,255,255)
black = (0,0,0)
#sets the dispaly (must be inside a tuple ())
gameDisplay = pygame.display.set_mode((display_width,display_height))
#changes the name of the window
pygame.display.set_caption("Robot Quest")
#times stuff (is gonna be used for FPS)
clock = pygame.time.Clock()
#loads up an image (not shown) must be in same directory
tankImg = pygame.image.load("tank.png")
blockImg = pygame.image.load("block.png")
class things:
def __init__(self,width,height,speed):
self.width = width
self.height = height
#if display.width doesn't work just pass the screen dimensions
self.X = display_width - self.width
self.Y= display_height - self.height
self.speed = speed
def move(self):
self.X -= self.speed
pos = self.X
return pos
def drawImage(self,imageName,x,y):
gameDisplay.blit(imageName,(x,y))
def game_loop():
#game exit value is set
game_exit = False
#when true you exit the loop, logic goes here
while not game_exit:
for event in pygame.event.get():
#method below on what to do if they press x in the corner
if event.type == pygame.QUIT:
#exit the loop
pygame.quit()
quit()
#fills the background
gameDisplay.fill(white)
block = things(100,100,4)
block.drawImage(blockImg,block.X,block.Y)
block.move()
pygame.display.update()
clock.tick(30)
game_loop()
pygame.quit()
quit()
在程序中,block.move() 只执行一次,仅此而已,所以对象停留在同一个位置,只移动了一次位置。我试图将 block.move() 函数放在 for 和 while 循环中,但如果我这样做,程序不会 运行。任何人都可以告诉我如何修复我的代码以使对象连续移动,以便它从一端移动到屏幕到另一端吗?
问题是您在 while 循环中重新初始化您的块,因此在每次迭代中您都将其重置到其原始位置然后移动它。尝试将初始化移到 while 循环之外:
def game_loop():
#game exit value is set
game_exit = False
block = things(100,100,4)
#when true you exit the loop, logic goes here
while not game_exit:
for event in pygame.event.get():
#method below on what to do if they press x in the corner
if event.type == pygame.QUIT:
#exit the loop
pygame.quit()
quit()
#fills the background
gameDisplay.fill(white)
block.drawImage(blockImg,block.X,block.Y)
block.move()
pygame.display.update()
clock.tick(30)
您似乎在每个循环中都初始化了您的块。尝试将 block = things(100,100,4)
移动到 while 循环之前。