使用 tkinter Canvas.move() 的像素轨迹/后像

问题描述 投票:0回答:1

我正在尝试使用 tkinter 在 Python 中制作游戏《外星人入侵者》。当试图让外星人在屏幕上移动时,我发现他们留下了像素痕迹/后图像,如下所示。我将 canvas.move 用于其他对象,例如播放器及其激光器,它们没有遇到相同的问题。有修复吗? (外星人移动的说明不完整,我想先解决这个问题,让他们向右移动,没有像素效果) 抱歉,我对编码比较陌生,并且对 tkinter/任何类型的游戏创建都缺乏经验。非常感谢任何帮助。

我的代码(仅适用于外星人,不适用于玩家或激光)是

from tkinter import * # Graphical user interface (GUI) module as part of standard library
import random
import time 

GAME_WIDTH = 1000
GAME_HEIGHT = 500
ALIEN_SPEED = 100
ALIEN_SIZE = 50
BACKGROUND_COLOUR = "black"

score = 0 #initial score
direction = "right" #initial direction
aliens = [] #global variable to create a row of aliens 

#initial set up 
window = Tk() #from tkinter to create the window
window.title("Alien Game")
label = Label(window, text = f"Score: {score}", font = ('consolas', 40))
label.pack()

canvas = Canvas(window, background= BACKGROUND_COLOUR, height= GAME_HEIGHT, width= GAME_WIDTH)
canvas.pack()



class Alien: #create individual alien body
    def __init__(self, x, y):
        self.x, self.y = x, y
        self.body = []
        self.face = canvas.create_rectangle(self.x, self.y, self.x+ALIEN_SIZE, self.y+ALIEN_SIZE, fill = 'yellow', outline='', width=0)
        self.eye1 = canvas.create_rectangle(self.x+0.2*ALIEN_SIZE, self.y+0.2*ALIEN_SIZE, self.x+0.4*ALIEN_SIZE, self.y+0.4*ALIEN_SIZE, fill = 'black', outline='', width=0)
        self.eye2 = canvas.create_rectangle(self.x+0.6*ALIEN_SIZE, self.y+0.2*ALIEN_SIZE, self.x+0.8*ALIEN_SIZE, self.y+0.4*ALIEN_SIZE, fill = 'black', outline='', width=0)
        self.mouth = canvas.create_rectangle(self.x+0.2*ALIEN_SIZE, self.y+0.8*ALIEN_SIZE, self.x+0.8*ALIEN_SIZE, self.y+ALIEN_SIZE, fill = 'black', outline='', width=0)
        self.body.extend((self.face, self.eye1, self.eye2, self.mouth))


def create_aliens(): # create multiple aliens in a row
    for i in range (int((GAME_WIDTH/(2*ALIEN_SIZE))*0.8)):
        aliens.append(Alien(x = i*2*ALIEN_SIZE, y = ALIEN_SIZE/2))
    return aliens

def move_aliens(): #move row of alien across screen
    global direction
    aliens = create_aliens()
    if direction == 'right':
        if aliens[-1].x <= (GAME_WIDTH-ALIEN_SIZE): # test if reached screen edge 
            for alien in aliens:
                for body_part in alien.body:
                    canvas.move(body_part, 10, 0)
                
        else:
            direction = 'left'
    elif direction == 'left':
        if aliens[0].x >= (0):
            for alien in aliens:
                canvas.move(alien.body, -5, 0)
                alien.x -=5
        else:
            direction = 'right'
    print (direction)
    window.after(ALIEN_SPEED, move_aliens)

#to centre the window on the screen (top left is position 0,0)
window.update()
x= int((window.winfo_screenwidth() - window.winfo_width())/2) #centre x position
y = int((window.winfo_screenheight() - window.winfo_height())/2) #centre y position
window.geometry(f'{window.winfo_width()}x{window.winfo_height()}+{x}+{y-30}') #format of (window width) x (window height) at + (position width) + (position height)

move_aliens()

window.mainloop()

how aliens start after they start moving to the right 10 pixels at a time

tkinter pixel game-development move tkinter-canvas
1个回答
0
投票

每当调用

move_aliens()
时,你就会创建一组新的外星人。一旦在函数之外创建一组外星人:

def move_aliens(): #move row of alien across screen
    global direction
    ### don't create set of aliens here
    #aliens = create_aliens()
    if direction == 'right':
        if aliens[-1].x <= (GAME_WIDTH-ALIEN_SIZE): # test if reached screen edge
            for alien in aliens:
                for body_part in alien.body:
                    canvas.move(body_part, 10, 0)

        else:
            direction = 'left'
    elif direction == 'left':
        if aliens[0].x >= (0):
            for alien in aliens:
                canvas.move(alien.body, -5, 0)
                alien.x -=5
        else:
            direction = 'right'
    print (direction)
    window.after(ALIEN_SPEED, move_aliens)

...

aliens = create_aliens()
move_aliens()

...
© www.soinside.com 2019 - 2024. All rights reserved.