[英]How can I move a sprite in pygame?
我想用pygame制作一個高爾夫游戲。 現在我正在測試只是移動精靈。 每當我修改player.position
時,精靈都會被復制並切成兩半。 我真的很困惑。
到目前為止,這是我的代碼:
import pygame
import player
import os
WIDTH, HEIGHT = 750, 750
WINDOW = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("GOLF")
#Setting up the player
player = player.PlayerClass()
def draw_window ():
#drawing the player
WINDOW.blit(player.texture, player.position)
pygame.display.update()
def LeftClick ():
#difference = player.position - pygame.mouse.get_pos()
player.position.x += 10
def main ():
run = True
FPS = 60
clock = pygame.time.Clock()
while run:
clock.tick(FPS)
draw_window()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
LeftClick()
main()
玩家等級:
import pygame
import os
class PlayerClass:
def __init__(self):
self.texture = pygame.image.load(os.path.join((os.path.join("assets", "imgs")), "ball.png"))
self.position = pygame.Vector2(300, 500)
您必須在每一幀中重新繪制場景。 重繪的意思:
清除顯示
繪制對象
更新顯示
def draw_window ():
# clear display
WINDOW.fill((0, 0, 0))
# drawing the player
WINDOW.blit(player.texture, player.position)
# update display
pygame.display.update()
在顯示表面上繪圖意味着永久改變表面中像素的顏色。 因此,需要在應用程序循環中的每一幀開始時清除顯示。 典型的 PyGame 應用程序循環必須:
pygame.time.Clock.tick
限制每秒幀數以限制 CPU 使用率pygame.event.pump()
或pygame.event.get()
來處理事件。blit
所有對象)pygame.display.update()
或pygame.display.flip()
更新顯示
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.