【发布时间】:2020-06-01 09:22:29
【问题描述】:
我正在尝试实现一个游戏,它允许点击随机生成的圆圈,每次点击它都会给你积分。
import pygame
import time
import random
pygame.init()
window = pygame.display.set_mode((800,600))
class Circle():
def __init__(self, color, x, y, radius, width):
self.color = color
self.x = x
self.y = y
self.radius = radius
self.width = width
def draw(self, win, outline=None):
pygame.draw.circle(win, self.color, (self.x, self.y), self.radius, self.width)
def isOver(self, mouse):
mouse = pygame.mouse.get_pos()
# Pos is the mouse position or a tuple of (x,y) coordinates
if mouse[0] > self.x and mouse[0] < self.x + self.radius:
if mouse[1] > self.y and mouse[1] < self.y + self.width:
return True
return False
circles = []
clock = pygame.time.Clock()
FPS = 60
current_time = 0
next_circle_time = 0
run=True
while run:
delta_ms = clock.tick()
current_time += delta_ms
if current_time > next_circle_time:
next_circle_time = current_time + 1000 # 1000 milliseconds (1 second)
r = 20
new_circle = Circle((255, 255, 255), random.randint(r, 800-r), random.randint(r, 600-r), r, r)
circles.append(new_circle)
print()
window.fill((0, 0, 0))
for c in circles:
c.draw(window)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run=False
pygame.quit()
quit()
if event.type == pygame.MOUSEBUTTONDOWN:
if Circle.isOver(mouse):
print('Clicked the circle')
但是,当我尝试实现最后一个条件时,如果 Circle.isOver(mouse):... 我收到此错误
TypeError: isOver() missing 1 required positional argument: 'mouse'
有谁知道这个问题的解决方案?非常感谢任何帮助!
【问题讨论】:
标签: python oop pygame attributes