Rörlig grafik blinkar

Programmeringsfrågor som ej kan kopplas till artiklar

Rörlig grafik blinkar

Inläggav theo0019 » 3 september 2017, 13:06

Hej, har gjort ett spel som liknar PONG. Nyss fungerade det hur bra som helst men nu blinkar bollen hela tiden...

Just nu är programmet lång och hyfsat osorterat,
Kod: Markera allt
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PONG
{
    public partial class Form1 : Form
    {
        public Point GetBallVelocity()
        {
            ball.Location = screenMid;
            int a = rnd.Next(3, 14);
            int b = rnd.Next(3, 14);
            if (rnd.Next(0, 2) == 0) { b = -b; }
            if (rnd.Next(0, 2) == 0) { a = -a; }
            Point speed = new Point(a, b);
            return speed;
        }

        public Point BallPhysics(Panel x, bool recursion, Point velocity)
        {
            x.Location = new Point(x.Location.X + velocity.X, x.Location.Y + velocity.Y);  //Move ball

            if (x.Bounds.IntersectsWith(racket1.Bounds) || x.Bounds.IntersectsWith(racket2.Bounds))
            { velocity = new Point(-velocity.X, velocity.Y); }

            if (x.Top <= 0 || x.Bottom >= screenHeight)
            { velocity = new Point(velocity.X, -velocity.Y); }

            //if (x.Location.X <= racket1.Location.X + racket1.Bounds.Width || x.Location.X >= racket2.Location.X - racket1.Bounds.Width)
            //{
            //    velocity = new Point(-velocity.X, velocity.Y);
            //}
            if (x.Location.X < 0)
            { GetBallVelocity(); score_2++; score2.Text = score_2.ToString(); }

            if (x.Location.X > screenWidth)
            { GetBallVelocity(); score_1++; score1.Text = score_1.ToString(); }

            return velocity;

        }



        public int BotCursion(Panel racket, Point ball_1, Point speed2)
        {
            if (ball_1.X >= racket.Location.X) { return ball_1.Y; }
            if (ball_1.X <= racket1.Location.X) { return screenMid.Y - racket.Bounds.Height / 2; }

            if (ball_1.X <= racket1.Location.X + racket1.Width) { speed2 = new Point(-speed2.X, speed2.Y); }
            if (ball_1.Y <= 0 || ball_1.Y >= screenHeight) { speed2 = new Point(speed2.X, -speed2.Y); }
            ball_1 = new Point(ball_1.X + speed2.X,ball_1.Y + speed2.Y);

            return BotCursion(racket, ball_1, speed2);
        }

        public void BotMove()
        {
            //if (ball.Location.Y > racket2.Location.Y + racket2.Height / 2 && ball.Location.X + 300 >= racket2.Location.X)
            //{ racket2.Location = new Point(racket2.Location.X, racket2.Location.Y + bot_speed*2); }

            //if (ball.Location.Y < racket2.Location.Y + racket2.Height / 2 && ball.Location.X + 300 >= racket2.Location.X)
            //{ racket2.Location = new Point(racket2.Location.X, racket2.Location.Y - bot_speed*2); }

            //if (ball.Location.Y > racket1.Location.Y + racket1.Height / 2)
            //{ racket1.Location = new Point(racket1.Location.X, racket1.Location.Y + bot_speed); }

            //if (ball.Location.Y < racket1.Location.Y + racket1.Height / 2)
            //{ racket1.Location = new Point(racket1.Location.X, racket1.Location.Y - bot_speed); }

            if (ball.Location.Y > racket2.Location.Y + racket2.Height / 2)
            { racket2.Location = new Point(racket2.Location.X, racket2.Location.Y + bot_speed); }

            if (ball.Location.Y < racket2.Location.Y + racket2.Height / 2)
            { racket2.Location = new Point(racket2.Location.X, racket2.Location.Y - bot_speed); }
        }

        public static Random rnd = new Random();

        public static int screenHeight = Screen.PrimaryScreen.Bounds.Height;
        public static int screenWidth = Screen.PrimaryScreen.Bounds.Width;
        public static Point screenMid = new Point(screenWidth / 2, screenHeight / 2);
        public static Point speed = new Point(rnd.Next(-8, 8), rnd.Next(-8, 8));

        //Player
        public static int racket1_speed = 15;
        public static bool upIsActive = false;
        public static bool downIsActive = false;
        public static int score_1 = 0;
        public static int score_2 = 0;
        //BOT
        public static int bot_speed = 5;
        public static int easy = 15;
        public static int medium = 10;
        public static int hard = 5;
        public static int impossible = 1;

        //Recursion
        public static bool moveBot = false;
        public static int moveBot_cord;

        //Menu
        public static bool inGame = true;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            timer2.Enabled = true;
            Cursor.Hide();
            this.FormBorderStyle = FormBorderStyle.None;    //Borderless
            this.Bounds = Screen.PrimaryScreen.Bounds;      //Fullskärm


            ////Objects////
            #region GameObjects
            //Mitten streck
            panel1.Location = new Point(screenMid.X, 0);
            panel1.Size = new Size(5, screenHeight);

            //Score1
            score1.Location = new Point((screenWidth / 10) * 4 - 100, screenHeight / 30);

            //Score2
            score2.Location = new Point((screenWidth / 10) * 6, screenHeight / 30);

            //Racket1
            racket1.Location = new Point((screenWidth / 10), screenMid.Y - racket1.Size.Height / 2);

            //Racket2
            racket2.Location = new Point((screenWidth) - screenWidth/10, screenMid.Y - racket2.Size.Height / 2);
            #endregion

            ////////////

            #region MenuObjects

            menuPanel.Location = new Point(0, 0);
            menuPanel.Height = screenHeight;
            menuPanel.Width = screenWidth;
            menuPanel.Visible = false;

            //For the player to not be able to play/pause background video
            mediaOverlay.Parent = MediaPlayer1;
            mediaOverlay.Height = screenHeight;
            mediaOverlay.Width = screenWidth;
            mediaOverlay.BringToFront();
            mediaOverlay.Location = new Point(0, 0);

            //Background video
            MediaPlayer1.Location = new Point(0, -43);
            MediaPlayer1.Height = screenHeight + 150;
            MediaPlayer1.Width = screenWidth;
            MediaPlayer1.URL = @"C:\Users\theod\source\repos\PONG\PONG\Resources\Smoke3.mp4";

            MediaPlayer1.Ctlcontrols.pause();
            MediaPlayer1.Ctlcontrols.currentPosition = 0;

            #endregion
            ///////////////

            speed = GetBallVelocity();

        }

        #region KeyPresses
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape) { Application.Exit(); }
            if (e.KeyCode == Keys.Up) { upIsActive = true; }
            if (e.KeyCode == Keys.Down) { downIsActive = true; }
        }

        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Up) { upIsActive = false; }
            if (e.KeyCode == Keys.Down) { downIsActive = false; }
        }
        #endregion


        //Game update (Ball, rackets)
        private void timer1_Tick(object sender, EventArgs e)
        {

            speed = BallPhysics(ball, false, speed);

            if (upIsActive && racket1.Top > 0)
            { racket1.Location = new Point(racket1.Location.X, racket1.Location.Y - racket1_speed); }

            if (downIsActive && racket1.Bottom < screenHeight)
            { racket1.Location = new Point(racket1.Location.X, racket1.Location.Y + racket1_speed); }

            if (moveBot)
            {
                if (moveBot_cord - 15 > racket2.Location.Y) { racket2.Location = new Point(racket2.Location.X, racket2.Location.Y + bot_speed); }
                else if (moveBot_cord + 15 < racket2.Location.Y) { racket2.Location = new Point(racket2.Location.X, racket2.Location.Y - bot_speed); }
                else if (moveBot_cord - 15 < racket2.Location.Y && moveBot_cord + 15 > racket2.Location.Y) { moveBot = false; }
                }
            }

        //recursion update
        private void timer2_Tick(object sender, EventArgs e)
        {
            if (ball.Location.X > racket1.Location.X + racket1.Bounds.Width * 2 && inGame)
            {
                moveBot_cord = (BotCursion(racket2, ball.Location, speed) - racket2.Bounds.Height / 2);
                moveBot = true;
            }

            if (!inGame && MediaPlayer1.Ctlcontrols.currentPosition >= 44 ) { MediaPlayer1.Ctlcontrols.currentPosition = 0; }

        }

        private void menu_play_Click(object sender, EventArgs e)
        {
            MediaPlayer1.Ctlcontrols.pause();
            menuPanel.Visible = false;
            menuPanel.SendToBack();
            timer1.Enabled = true;
            inGame = true;
        }
    }
}


Denna kanske också kan vara bra att ha.

Kod: Markera allt
namespace PONG
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
            this.score1 = new System.Windows.Forms.Label();
            this.score2 = new System.Windows.Forms.Label();
            this.panel1 = new System.Windows.Forms.Panel();
            this.racket1 = new System.Windows.Forms.Panel();
            this.racket2 = new System.Windows.Forms.Panel();
            this.ball = new System.Windows.Forms.Panel();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.timer2 = new System.Windows.Forms.Timer(this.components);
            this.menu_play = new System.Windows.Forms.Label();
            this.menu_1player = new System.Windows.Forms.Label();
            this.menu_2player = new System.Windows.Forms.Label();
            this.menu_header = new System.Windows.Forms.Label();
            this.menuPanel = new System.Windows.Forms.Panel();
            this.mediaOverlay = new System.Windows.Forms.Panel();
            this.MediaPlayer1 = new AxWMPLib.AxWindowsMediaPlayer();
            this.menuPanel.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.MediaPlayer1)).BeginInit();
            this.SuspendLayout();
            //
            // score1
            //
            this.score1.Font = new System.Drawing.Font("standard 07_57", 72F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.score1.Location = new System.Drawing.Point(13, 9);
            this.score1.Name = "score1";
            this.score1.Size = new System.Drawing.Size(200, 150);
            this.score1.TabIndex = 0;
            this.score1.Text = "0";
            //
            // score2
            //
            this.score2.Font = new System.Drawing.Font("standard 07_57", 72F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.score2.Location = new System.Drawing.Point(219, 9);
            this.score2.Name = "score2";
            this.score2.Size = new System.Drawing.Size(200, 150);
            this.score2.TabIndex = 1;
            this.score2.Text = "0";
            //
            // panel1
            //
            this.panel1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(70)))), ((int)(((byte)(70)))), ((int)(((byte)(70)))));
            this.panel1.Location = new System.Drawing.Point(232, 162);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(5, 1080);
            this.panel1.TabIndex = 2;
            //
            // racket1
            //
            this.racket1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(140)))), ((int)(((byte)(140)))), ((int)(((byte)(140)))));
            this.racket1.Location = new System.Drawing.Point(12, 173);
            this.racket1.Name = "racket1";
            this.racket1.Size = new System.Drawing.Size(15, 150);
            this.racket1.TabIndex = 3;
            //
            // racket2
            //
            this.racket2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(140)))), ((int)(((byte)(140)))), ((int)(((byte)(140)))));
            this.racket2.Location = new System.Drawing.Point(464, 127);
            this.racket2.Name = "racket2";
            this.racket2.Size = new System.Drawing.Size(15, 150);
            this.racket2.TabIndex = 4;
            //
            // ball
            //
            this.ball.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(230)))), ((int)(((byte)(230)))), ((int)(((byte)(230)))));
            this.ball.Location = new System.Drawing.Point(322, 173);
            this.ball.Name = "ball";
            this.ball.Size = new System.Drawing.Size(10, 10);
            this.ball.TabIndex = 5;
            //
            // timer1
            //
            this.timer1.Interval = 10;
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
            //
            // timer2
            //
            this.timer2.Interval = 100;
            this.timer2.Tick += new System.EventHandler(this.timer2_Tick);
            //
            // menu_play
            //
            this.menu_play.Anchor = System.Windows.Forms.AnchorStyles.Top;
            this.menu_play.AutoSize = true;
            this.menu_play.BackColor = System.Drawing.Color.Transparent;
            this.menu_play.Font = new System.Drawing.Font("Showcard Gothic", 60F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.menu_play.Location = new System.Drawing.Point(213, 206);
            this.menu_play.Name = "menu_play";
            this.menu_play.Size = new System.Drawing.Size(243, 98);
            this.menu_play.TabIndex = 1;
            this.menu_play.Text = "PLAY";
            this.menu_play.Click += new System.EventHandler(this.menu_play_Click);
            //
            // menu_1player
            //
            this.menu_1player.Anchor = System.Windows.Forms.AnchorStyles.Top;
            this.menu_1player.AutoSize = true;
            this.menu_1player.BackColor = System.Drawing.Color.Transparent;
            this.menu_1player.Font = new System.Drawing.Font("Showcard Gothic", 40F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.menu_1player.Location = new System.Drawing.Point(218, 328);
            this.menu_1player.Name = "menu_1player";
            this.menu_1player.Size = new System.Drawing.Size(265, 67);
            this.menu_1player.TabIndex = 2;
            this.menu_1player.Text = "1 PLAYER";
            //
            // menu_2player
            //
            this.menu_2player.Anchor = System.Windows.Forms.AnchorStyles.Top;
            this.menu_2player.AutoSize = true;
            this.menu_2player.BackColor = System.Drawing.Color.Transparent;
            this.menu_2player.Font = new System.Drawing.Font("Showcard Gothic", 40F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.menu_2player.Location = new System.Drawing.Point(218, 406);
            this.menu_2player.Name = "menu_2player";
            this.menu_2player.Size = new System.Drawing.Size(268, 67);
            this.menu_2player.TabIndex = 3;
            this.menu_2player.Text = "2 PLAYER";
            //
            // menu_header
            //
            this.menu_header.Anchor = System.Windows.Forms.AnchorStyles.Top;
            this.menu_header.AutoSize = true;
            this.menu_header.Font = new System.Drawing.Font("Showcard Gothic", 100F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.menu_header.Location = new System.Drawing.Point(153, 16);
            this.menu_header.Name = "menu_header";
            this.menu_header.Size = new System.Drawing.Size(429, 167);
            this.menu_header.TabIndex = 0;
            this.menu_header.Text = "PONG";
            //
            // menuPanel
            //
            this.menuPanel.Controls.Add(this.menu_2player);
            this.menuPanel.Controls.Add(this.menu_1player);
            this.menuPanel.Controls.Add(this.menu_play);
            this.menuPanel.Controls.Add(this.menu_header);
            this.menuPanel.Controls.Add(this.mediaOverlay);
            this.menuPanel.Controls.Add(this.MediaPlayer1);
            this.menuPanel.Location = new System.Drawing.Point(518, 28);
            this.menuPanel.Name = "menuPanel";
            this.menuPanel.Size = new System.Drawing.Size(654, 549);
            this.menuPanel.TabIndex = 6;
            this.menuPanel.Visible = false;
            //
            // mediaOverlay
            //
            this.mediaOverlay.Location = new System.Drawing.Point(21, 23);
            this.mediaOverlay.Name = "mediaOverlay";
            this.mediaOverlay.Size = new System.Drawing.Size(200, 100);
            this.mediaOverlay.TabIndex = 5;
            //
            // MediaPlayer1
            //
            this.MediaPlayer1.Enabled = true;
            this.MediaPlayer1.Location = new System.Drawing.Point(88, 206);
            this.MediaPlayer1.Name = "MediaPlayer1";
            this.MediaPlayer1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("MediaPlayer1.OcxState")));
            this.MediaPlayer1.Size = new System.Drawing.Size(265, 295);
            this.MediaPlayer1.TabIndex = 6;
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(40)))), ((int)(((byte)(40)))), ((int)(((byte)(40)))));
            this.ClientSize = new System.Drawing.Size(1300, 619);
            this.Controls.Add(this.menuPanel);
            this.Controls.Add(this.ball);
            this.Controls.Add(this.racket2);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.racket1);
            this.Controls.Add(this.score1);
            this.Controls.Add(this.score2);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
            this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyUp);
            this.menuPanel.ResumeLayout(false);
            this.menuPanel.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.MediaPlayer1)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Label score1;
        private System.Windows.Forms.Label score2;
        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.Panel racket1;
        private System.Windows.Forms.Panel racket2;
        private System.Windows.Forms.Panel ball;
        private System.Windows.Forms.Timer timer1;
        private System.Windows.Forms.Timer timer2;
        private System.Windows.Forms.Label menu_play;
        private System.Windows.Forms.Label menu_2player;
        private System.Windows.Forms.Label menu_1player;
        private System.Windows.Forms.Label menu_header;
        private System.Windows.Forms.Panel menuPanel;
        private System.Windows.Forms.Panel mediaOverlay;
        private AxWMPLib.AxWindowsMediaPlayer MediaPlayer1;
    }
}




Om någon skulle kolla igenom det så skulle det vara toppen :)

EDIT: I alt+tab ser bollen bra ut och blinkar inte, det är alltså bara i själva spelet som det ser dåligt ut.
theo0019
 
Inlägg: 2
Blev medlem: 3 september 2017, 12:58

Re: Rörlig grafik blinkar

Inläggav theo0019 » 3 september 2017, 20:51

¨tydligen var det bara min dator som inte fungerade som den skulle och helt plötsligt fungerade det som normalt igen
theo0019
 
Inlägg: 2
Blev medlem: 3 september 2017, 12:58


Återgå till Frågor

Vilka är online

Användare som besöker denna kategori: Inga registrerade användare och 2 gäster

cron