为什么我的一些圈子消失了,而有些则没有?

第 94 章

游戏的重点是让所有圆圈在碰撞时消失,但由于某种原因,有些圆圈没有消失?- 提前谢谢你的帮助!

import turtle
import random
import math
import time

# Setting up the Screen
ms = turtle.Screen()
ms.bgcolor("red")
ms.title("Space Rocket Minigame @Rafa94")

# Using class functions/Methods

# subclass
class Border(turtle.Turtle):
    def __init__(self):  # class constrcutor
        turtle.Turtle.__init__(self)  # adding our Objects attributes all starting with "self"
        self.penup()
        self.hideturtle()
        self.speed(0)
        self.color("silver")
        self.pensize(5)

    def draw_border(self):
        self.penup()# getting our pen to start drawing
        self.goto(-300, -300)
        self.pendown()
        self.goto(-300, 300)
        self.goto(300, 300)
        self.goto(300, -300)
        self.goto(-300, -300)

class Player(turtle.Turtle):  # since it got inherited this class becomes a Superclass

    def __init__(self):  # self is our only argument here but it will have multiple attributes
        turtle.Turtle.__init__(self)  # since we are using the Turtle module, we are able to use it's built in functions
        self.penup()# our attributes
        self.speed(0)
        self.shape("triangle")
        self.color("black")
        self.velocity = 0.1

    def move(self):
        self.forward(self.velocity)

        # Border Checking
        if self.xcor() > 290 or self.xcor() < -290:  # Left side is -290 Right side is 290 we also want the coordinates x and y to be below 300 to not go over our border
            self.left(60)
        if self.ycor() > 290 or self.ycor() < -290:
            self.left(60)

    def turnleft(self):
        self.left(30)

    def turnright(self):
        self.right(30)

    def increasespeed(self):
        self.velocity += 1

class Goal(turtle.Turtle):  # Sub Class

    def __init__(self):
        # since we are using the Turtle module we are able to use it's built in functions
        turtle.Turtle.__init__(self)
        self.penup()  # our attributes
        self.speed(0)
        self.shape("circle")
        self.color("green")
        self.velocity = 3  #xcor                    #ycor
        self.goto(random.randint(-250, 250), random.randint(-250, 250))  # we are making our turtle "go to" X & Y coordinates by -250 and 250 only randomly. We also have our random module here aswell
        self.setheading(random.randint(0, 360))  # setting the heading to see in which direction i want it to go

    def jump(self):  # Jump = Collidee
        self.goto(random.randint(-250, 250), random.randint(-250, 250))  # "jump" stands for Collidee so if the circle "jumps" with player it will move to random postion by 250 and -25
        self.setheading(random.randint(0, 360))  # from where it collidee's it goes 360 moves location 360 Right

    def move(self): # we copyed the same method cause it will be doing the same movements as the player we want it to go "forward" with our set "speed" & also check for our borders we set
        self.forward(self.velocity)

        # Border Checking
        if self.xcor() > 290 or self.xcor() < -290:  # Left side is -290 Right side is 290 we also want the coordinates x and y to be below 300 to not go over our border
            self.left(60)
        if self.ycor() > 290 or self.ycor() < -290:
            self.left(60)

# Collision checking function/Method
# Uses the Pythgorean Theorem to measure the distance between two objects

def isCollision(t1, t2):  # t1 = turtle1 t2 = turtle also when a function starts with  "is" isCollision most likely it will be a Boolean of True or False
    a = t1.xcor()-t2.xcor()  # xcor = Right -xcor = Left/ when they collide the xcor is 0
    b = t1.ycor()-t2.ycor()  # ycor = Right -ycor = Left/ when they collide the ycor is 0
    distance = math.sqrt((a ** 2) + (b ** 2))

    if distance < 30:
        return True
    else:
        return False

# Create class instances
player = Player()  # after creating a class must make instances to call it in other words make an Object of the class
border = Border()  # sub class
#goal = Goal()  #sub class
# Draw our border
border.draw_border()

#create multiple goals
goals = []  # Creating a list of goals
for count in range(6):  # We are making the code repeat 6 times
    goals.append(Goal()) # each time the code runs it puts a goal the end 6 times

# Set keyboard bindings
ms.listen()
ms.onkey(player.turnleft, "Left")
ms.onkey(player.turnright, "Right")
ms.onkey(player.increasespeed, "Up")

# speed game up
ms.tracer(0.1)

# main loop
while True:
    ms.update()
    player.move()  # these two are class methods
    #goal.move()  # the reason we copyed like we said is cause it's gunna have the exact same movements as our player!
    # we want the goal to be True to in our while loop in order for the code to be excuted
    for goal in goals:
        # Basically saying If there is a collision between the player and goal we the want the goal to "jump" / Function in our while True loop
        goal.move()
        if isCollision(player, goal):
            goal.jump()  # baiscally saying if the goal collide's move or "jump" to other location

    time.sleep(0.005)
cdlane

据我所知,你的圈子(目标)消失并重新出现在你设计的其他地方。一种可能性是,由于您将圆圈移动到一个随机位置,该位置可能靠近它们消失的位置,从而看起来什么也没发生。

通常,您的代码是一团糟。下面是我对它的重写,以给它带来一些结构和风格。以及更多地利用海龟提供的功能:

from turtle import Screen, Turtle
from random import randint

WIDTH, HEIGHT = 600, 600
CURSOR_SIZE = 20

class Border(Turtle):

    def __init__(self):  # class initializer
        super().__init__(visible=False)
        self.penup()
        self.color("silver")
        self.pensize(5)

    def draw_border(self):
        self.goto(-WIDTH/2, -HEIGHT/2)
        self.pendown()

        for _ in range(2):
            self.forward(WIDTH)
            self.left(90)
            self.forward(HEIGHT)
            self.left(90)

class Player(Turtle):

    def __init__(self):
        super().__init__(shape="triangle")
        self.color("black")
        self.penup()

        self.velocity = 0.1

    def move(self):
        self.forward(self.velocity)

        if not (CURSOR_SIZE - WIDTH/2 < self.xcor() < WIDTH/2 - CURSOR_SIZE and CURSOR_SIZE - HEIGHT/2 < self.ycor() < HEIGHT/2 - CURSOR_SIZE):
            self.undo()  # undo forward motion
            self.left(60)
            self.forward(self.velocity)  # redo forward motion in new heading

    def turn_left(self):
        self.left(30)

    def turn_right(self):
        self.right(30)

    def increase_speed(self):
        self.velocity += 1

class Goal(Turtle):

    def __init__(self):
        super().__init__(shape="circle")
        self.color("green")
        self.penup()

        self.velocity = 3

        self.jump()

    def jump(self):
        while self.distance(player) < CURSOR_SIZE * 10:  # make sure we move far away from player
            self.goto(randint(CURSOR_SIZE - WIDTH/2, WIDTH/2 - CURSOR_SIZE), randint(CURSOR_SIZE - HEIGHT/2, HEIGHT/2 - CURSOR_SIZE))

        self.setheading(randint(0, 360))

    def move(self):
        self.forward(self.velocity)

        if not (CURSOR_SIZE - WIDTH/2 < self.xcor() < WIDTH/2 - CURSOR_SIZE and CURSOR_SIZE - HEIGHT/2 < self.ycor() < HEIGHT/2 - CURSOR_SIZE):
            self.undo()  # undo forward motion
            self.left(60)
            self.forward(self.velocity)  # redo forward motion in new heading

def isCollision(t1, t2):
    return t1.distance(t2) < CURSOR_SIZE

def move():
    player.move()

    for goal in goals:
        goal.move()

        if isCollision(player, goal):
            goal.jump()

    screen.update()

    screen.ontimer(move, 10)

# Setting up the Screen
screen = Screen()
screen.bgcolor("red")
screen.title("Space Rocket Minigame @cdlane")
screen.tracer(False)

# Create class instances
Border().draw_border()

player = Player()

# Create multiple goals
goals = [Goal() for _ in range(6)]

# Set keyboard bindings
screen.onkey(player.turn_left, "Left")
screen.onkey(player.turn_right, "Right")
screen.onkey(player.increase_speed, "Up")
screen.listen()

move()

screen.mainloop()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么有些原语有字节码,而有些则没有?

简介:一些游戏物体在移动,而有些则没有

为什么有些信号被束缚而另一些则没有束缚?

为什么有些算术指令有有符号/无符号变体,而有些则没有

为什么有些文本在WPF中会消失,而有些则不会

为什么有些UWP项目有project.json而有些却没有?

为什么有些Java setter方法自动成为Kotlin属性,而有些却没有?

为什么有些对象没有在这个 .splice() 中被移除而有些是?

为什么有些电子邮件ID被接受而有些却没有PHP

为什么有些Micro-USB插头的一侧有这两个狭缝/弹簧,而有些却没有?

为什么有些 URL 被“禁止”而有些不在我的 Web 应用程序中

为什么有些 Angular 模块会导入而有些却不会?

为什么有些库需要嵌入而有些则不需要?

为什么有些函数参数存储在堆栈上而有些存储在堆上?

为什么有些快照很快,而有些快照那么慢?

为什么有些行可以插入而有些行不能?

为什么有些程序以未定义的行为执行而另一些却没有呢?

为什么有些球显示它们发生了碰撞,而另一些却没有?

为什么有些字符串包含“&nbsp;” 还有一些“”,当我的输入相同时(“”)?

XPage的beforePageLoad事件-一些代码有效,而有些则无效

PHP的说,一些结果不是未定义的,而有些则是

Blazor一些Javascript可以运行,而有些则不能

为什么有些Jupyter Notebook输出单元格带有输出提示(即Out [4] :),而有些却没有?

为什么有些终端从终端运行时使用“&”关闭了某些程序,而有些终端却没有呢?

为什么有些简短的条件语句在python中起作用而有些却没有呢?

为什么/为什么有些类需要每个可能的参数,而有些则不需要?

Angular UI Router-在路由中传递某些参数状态,而有些则没有?

为什么有些内存地址报告为常数,而另一些则更改?

TRegistry-为什么有些键可读,而另一些则不可读?