Я программирую что-то вроде PONG в IOS Playgrounds.
Все это работает, но через несколько секунд происходит сбой.
Я действительно не знаю почему. Может кто-нибудь, пожалуйста, помогите мне?
Положение мяча меняется, и сенсорное событие работает, но оно вылетает при розыгрыше. Функция UIView
Я пытался очистить контекст и все, кромеэто не сработало
import UIKit;
import PlaygroundSupport;
import CoreGraphics;
import Dispatch;
func delay (delay:Double)
{
let t = CACurrentMediaTime()
var b = true
while b {
if(CACurrentMediaTime()>t+delay){
b=false;
return
}
}
}
let page = PlaygroundPage.current
var sheight:Double = 700 //Double(UIScreen.main.bounds.height);
var swidth:Double = 900 //Double(UIScreen.main.bounds.width);
let v = UIView(frame: CGRect(x: -100, y: -100, width: swidth, height: sheight))
swidth = Double(v.bounds.width)
sheight = Double(v.bounds.height)
let moveStab = sheight / 300;
var dirX = 1.0
var dirY = 1.0
let bsp:Double = 3.0
var Point1 = 0;
var Point2 = 0;
let psp:Double = 2.0
var up = [false,false]
var down = [false,false]
var ballx = swidth / 2;
var bally = sheight / 2;
let ballRad = 30.0
var p1x = 20.0
var p1y = sheight / 2
var p2x = swidth - 40.0
var p2y = sheight / 2
let pw = 20.0
let ph = 75.0
class Draw: UIView {
func PointInRad (point: CGPoint, point2 : CGPoint, rad : CGFloat) -> Bool{
let x1 = point.x;
let y1 = point.y
let x2 = point2.x
let y2 = point2.y
var dx = x1-x2
var dy = y1 - y2
if (dx < 0){dx=dx * -1}
if (dy < 0){dy=dy * -1}
let e = sqrt(dx*dx + dy*dy)
if(e <= rad){
return true
}
return false
}
func PointInRad (point: CGPoint, x2 : CGFloat, y2: CGFloat, rad: CGFloat) -> Bool {
let x1 = point.x;
let y1 = point.y
var dx = x1-x2
var dy = y1 - y2
if (dx < 0){dx=dx * -1}
if (dy < 0){dy=dy * -1}
let e = sqrt(dx*dx + dy*dy)
if(e <= rad){
return true
}
return false
}
func PointInRad ( x1 : CGFloat, y1 : CGFloat, x2 : CGFloat, y2 : CGFloat, rad: CGFloat) -> Bool{
var dx = x1-x2
var dy = y1 - y2
if (dx < 0){dx=dx * -1}
if (dy < 0){dy=dy * -1}
let e = sqrt(dx*dx + dy*dy)
if(e <= rad){
return true
}
return false
}
func update(){
self.isMultipleTouchEnabled = true;
self.clearsContextBeforeDrawing = true;
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
if(PointInRad(point: touch.location(in: view), x2: 700, y2:400 , rad: 50)){
up[1] = true
}
if(PointInRad(point: touch.location(in: view), x2: 100, y2:400 , rad: 50)){
up[0] = true
}
if(PointInRad(point: touch.location(in: view), x2: 700, y2:500 , rad: 50)){
down[1] = true
}
if(PointInRad(point: touch.location(in: view), x2: 100, y2:500 , rad: 50)){
down[0] = true
}
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
if(!PointInRad(point: touch.location(in: view), x2: 700, y2:400 , rad: 50)){
up[1] = false
}
if(!PointInRad(point: touch.location(in: view), x2: 100, y2:400 , rad: 50)){
up[0] = false
}
if(!PointInRad(point: touch.location(in: view), x2: 700, y2:500 , rad: 50)){
down[1] = false
}
if(!PointInRad(point: touch.location(in: view), x2: 100, y2:500 , rad: 50)){
down[0] = false
}}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
if(PointInRad(point: touch.location(in: view), x2: 700, y2:400 , rad: 50)){
up[1] = false
}
if(PointInRad(point: touch.location(in: view), x2: 100, y2:400 , rad: 50)){
up[0] = false
}
if(PointInRad(point: touch.location(in: view), x2: 700, y2:500 , rad: 50)){
down[1] = false
}
if(PointInRad(point: touch.location(in: view), x2: 100, y2:500 , rad: 50)){
down[0] = false
}}
}
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
touchesEnded(touches, with: event)
self.setNeedsDisplay()
}
var n = false;
func drawCircle (color : UIColor, x:CGFloat, y:CGFloat, rad: CGFloat){
// Circle Drawing
let newx = x - rad / 2
let newy = y - rad / 2
let circlePath = UIBezierPath(ovalIn: CGRect(x: newx, y: newy, width: rad, height: rad))
color.setFill()
circlePath.fill()
}
func drawRect ( color : UIColor, x:CGFloat, y:CGFloat, width: CGFloat, height: CGFloat ) {
let circlePath = UIBezierPath(rect: CGRect(x: x, y: y, width: width, height: height))
color.setFill()
circlePath.fill()
}
override func draw(_: CGRect) {
var Path = UIBezierPath(ovalIn: CGRect(x: -100, y: -100, width: 900, height: 700))
let con = UIGraphicsGetCurrentContext();
con?.clear(CGRect(x: -100, y: -100, width: 1000, height: 1000))
// Color Declarations
var color = UIColor.white;
let ButtonN = UIColor(white: 0.5, alpha: 0.5)
let ButtonT = UIColor(white: 0.8, alpha: 0.8)
var p1 = NSString(string: String(Point1))
p1.draw(in: CGRect(x: 325, y: 100, width: 75, height: 100), withAttributes: [NSAttributedString.Key.foregroundColor: UIColor.white,NSAttributedString.Key.font: UIFont(name: "Arial", size: 80) ])
p1 = NSString(string: String(Point2))
p1.draw(in: CGRect(x: 430, y: 100, width: 75, height: 100), withAttributes: [NSAttributedString.Key.foregroundColor: UIColor.white,NSAttributedString.Key.font: UIFont(name: "Arial", size: 80) ])
p1 = NSString(string: String("Pong by Jakob"))
p1.draw(in: CGRect(x: 700, y: 25, width: 100, height: 100), withAttributes: [NSAttributedString.Key.foregroundColor: UIColor.white,NSAttributedString.Key.font: UIFont(name: "Arial", size: 20) ])
var rad = CGFloat(50);
var newx:CGFloat = 100.0
var newy:CGFloat = 400.0
var circlePath = UIBezierPath(ovalIn: CGRect(x: newx, y: newy, width: rad, height: rad))
if up[0] {
ButtonT.setFill();
} else {
ButtonN.setFill();
}
circlePath.fill()
circlePath = UIBezierPath(ovalIn: CGRect(x: 700, y: newy, width: rad, height: rad))
if up[1] {
ButtonT.setFill();
} else {
ButtonN.setFill();
}
circlePath.fill()
circlePath = UIBezierPath(ovalIn: CGRect(x: newx, y: 500, width: rad, height: rad))
if down[0] {
ButtonT.setFill();
} else {
ButtonN.setFill();
}
circlePath.fill()
circlePath = UIBezierPath(ovalIn: CGRect(x: 700, y: 500, width: rad, height: rad))
if down[1] {
ButtonT.setFill();
} else {
ButtonN.setFill();
}
circlePath.fill()
for i in 0 ... 30 {
color = UIColor.white
circlePath = UIBezierPath(rect: CGRect(x: CGFloat(393), y:CGFloat(Double(i)*50), width: CGFloat(14), height: CGFloat(30)))
color.setFill()
circlePath.fill()
}
circlePath = UIBezierPath(rect: CGRect(x: p1x, y: p1y, width: pw, height: ph))
color.setFill()
circlePath.fill()
circlePath = UIBezierPath(rect: CGRect(x: p2x, y: p2y, width: pw, height: ph))
color.setFill()
circlePath.fill()
rad = CGFloat(ballRad);
color = UIColor.white;
newx = (CGFloat(ballx) - rad / 2)
newy = CGFloat(bally) - rad / 2
circlePath = UIBezierPath(ovalIn: CGRect(x: newx, y: newy, width: rad, height: rad))
color.setFill()
circlePath.fill()
}
}
let view = Draw(frame: CGRect(x: 0, y: 0, width: swidth, height: sheight))
view.setNeedsDisplay()
view.update()
let T = Thread(block: {
while true {
if(Int(ballx)<Int(p1x + pw) && bally+ballRad <= p1y + ph && bally - ballRad >= p1y){
dirX *= -1
}
if(ballx<p2x && bally+ballRad <= p2y + ph && bally - ballRad >= p2y){
dirX *= -1
}
if(bally < 0 + ballRad || bally > 600 - ballRad){
dirY *= -1;
}
if (ballx - ballRad < 0){
ballx = 800/2
dirX = 1;
Point2+=1
}
if (ballx + ballRad > 800){
ballx = swidth/2
dirX = -1;
Point1+=1
}
if up[0] {
p1y -= psp
}
if up[1]{
p2y -= psp
}
if down[0] {
p1y += psp
}
if down[1]{
p2y += psp
}
ballx += bsp * dirX
bally += bsp * dirY
view.setNeedsDisplay()
view.updateFocusIfNeeded()
for i in 1 ... 10000 {
print(i)
}
}
})
T.start()
let t = Thread(block: {
while true {
DispatchQueue.main.sync {
view .setNeedsDisplay()
}}
})
t.start()
page.liveView = view;
page.needsIndefiniteExecution = true
page.wantsFullScreenLiveView = true
Because i am on my IPad i dont have any Error message