【发布时间】:2020-02-20 16:42:39
【问题描述】:
我目前正在为学校课程项目编写一个简单的电脑游戏,但在 pygame 中遇到了问题。在我的游戏中,玩家只需向空中飞行的目标射击,几乎是逆向太空入侵者。我当前的问题是,我不能在竞技场中同时拥有两个形状,而其中一个形状会闪烁和滞后。以下是我的代码,欢迎任何帮助。干杯。
另外,有谁知道我可以如何在每幅画之间添加延迟。正如我在上面暗示的那样,我的目标是让目标在没有重叠的情况下保持相当的空间,但我确实希望同时在屏幕上出现多个。干杯。
import pygame
#Setting window dimensions and caption (Module 1)
pygame.init()
window = pygame.display.set_mode((800, 575))
pygame.display.set_caption("TARGET PRACTICE")
#Colour variables (Module 1)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (200, 0, 0)
GREEN = (0, 200, 0)
BLUE = (0, 0, 200)
#Target coordinates and dimensions (Module 3)
#target_x = 0
#target_y = 80
#target_w = 60
#target_h = 40
#target_v = 1
exec = True
class Target:
def __init__(self, x, y, h, w, v):
self.x = x
self.y = y
self.h = h
self.w = w
self.v = v
target_1 = Target(0, 80, 60, 40, 0.1)
target_2 = Target(0, 100, 60, 40, 0.1)
target_3 = Target(0, 50, 60, 40, 1)
clock = 0
while exec:
pygame.time.delay(1)
for event in pygame.event.get():
if event.type == pygame.QUIT:
exec = False
#Target Movement (Module 4)
window.fill(RED)
#Background and target drawing (Module 2)
target_1.x += target_1.v
pygame.draw.rect(window, BLUE, (target_1.x, target_1.y, target_1.h, target_1.w))
clock += 1
if clock%2 == 0:
target_2.x += target_2.v
pygame.draw.rect(window, BLUE, (target_2.x, target_2.y, target_2.h, target_2.w))
pygame.display.update()
pygame.quit()```
【问题讨论】:
标签: python oop pygame draw shapes