Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- FORM1----
- 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;
- using VILab8.Properties;
- namespace VILab8
- {
- public partial class Form1 : Form
- {
- Timer timer;
- Pacman pacman;
- static readonly int TIMER_INTERVAL = 250;
- static readonly int WORLD_WIDTH = 15;
- static readonly int WORLD_HEIGHT = 10;
- Image foodImage;
- bool[][] foodWorld;
- public Form1()
- {
- InitializeComponent();
- foodImage = Resources.orange_icon;
- DoubleBuffered = true;
- newGame();
- }
- public void newGame()
- {
- pacman = new Pacman();
- this.Width = pacman.radius * 2 * (WORLD_WIDTH + 1);
- this.Height = pacman.radius * 2 * (WORLD_HEIGHT + 1);
- foodWorld = new bool[10][];
- for (int i = 0; i < foodWorld.Length; i++)
- {
- foodWorld[i] = new bool[15];
- for (int j = 0; j < foodWorld[i].Length; j++)
- {
- foodWorld[i][j] = true;
- }
- }
- timer1.Interval = TIMER_INTERVAL;
- timer1.Enabled = true;
- }
- private void Form1_Paint(object sender, PaintEventArgs e)
- {
- Graphics g = e.Graphics;
- g.Clear(Color.Black);
- for (int i = 0; i < foodWorld.Length; i++)
- {
- for (int j = 0; j < foodWorld[i].Length; j++)
- {
- if (foodWorld[i][j])
- {
- g.DrawImageUnscaled(foodImage, j * pacman.radius * 2 + (pacman.radius * 2 - foodImage.Height) / 2, i * pacman.radius * 2 + (pacman.radius * 2 - foodImage.Width) / 2);
- }
- }
- }
- pacman.Draw(g);
- }
- private void Form1_KeyUp(object sender, KeyEventArgs e)
- {
- if (e.KeyCode == Keys.Up) {
- pacman.ChangeDirection(DIRECTION.Up);
- }
- else if (e.KeyCode == Keys.Down)
- {
- pacman.ChangeDirection(DIRECTION.Down);
- }
- else if (e.KeyCode == Keys.Left)
- {
- pacman.ChangeDirection(DIRECTION.Left);
- }
- else if (e.KeyCode == Keys.Right)
- {
- pacman.ChangeDirection(DIRECTION.Right);
- }
- Invalidate();
- }
- private void timer1_Tick(object sender, EventArgs e)
- {
- pacman.Move();
- if (foodWorld[pacman.positionY][pacman.positionX]) {
- foodWorld[pacman.positionY][pacman.positionX] = false;
- }
- Invalidate();
- }
- }
- }
- PACMAN--
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace VILab8
- {
- enum DIRECTION
- {
- Up,
- Down,
- Left,
- Right
- }
- class Pacman
- {
- public int positionX;
- public int positionY;
- public DIRECTION direction;
- public int radius = 20;
- public int speed;
- public bool mouthOpen;
- public SolidBrush color;
- public Pacman()
- {
- this.speed = this.radius;
- this.positionX = 7;
- this.positionY = 5;
- this.direction = DIRECTION.Right;
- color = new SolidBrush(Color.Yellow);
- mouthOpen = true;
- }
- public void ChangeDirection(DIRECTION direction1)
- {
- this.direction = direction1;
- }
- public void Move()
- {
- if (mouthOpen) mouthOpen = false;
- else mouthOpen = true;
- if (direction == DIRECTION.Left) {
- if (positionX > 0)
- {
- positionX--;
- }
- else {
- positionX = 14;
- }
- }
- else if (direction == DIRECTION.Right)
- {
- if (positionX < 14)
- {
- positionX ++;
- }
- else
- {
- positionX = 0;
- }
- }
- else if (direction == DIRECTION.Up)
- {
- if (positionY > 0)
- {
- positionY --;
- }
- else
- {
- positionY = 9;
- }
- }
- else {
- if (positionY < 9)
- {
- positionY ++;
- }
- else
- {
- positionY = 0;
- }
- }
- }
- public void Draw(Graphics g)
- {
- if (direction == DIRECTION.Right) {
- if(mouthOpen)
- g.FillPie(color, positionX * radius * 2 + 15, positionY * radius * 2+12, radius, radius, 30, 310);
- else
- g.FillPie(color, positionX * radius * 2 + 15, positionY * radius * 2+12, radius, radius, 0, 360);
- }
- else if (direction == DIRECTION.Left)
- {
- if (mouthOpen)
- g.FillPie(color, positionX * radius * 2 + 15, positionY * radius * 2 + 12, radius, radius, 210, 310);
- else
- g.FillPie(color, positionX * radius * 2 + 15, positionY * radius * 2 + 12, radius, radius, 0, 360);
- }
- else if (direction == DIRECTION.Up)
- {
- if (mouthOpen)
- g.FillPie(color, positionX * radius * 2 + 15, positionY * radius * 2 + 12, radius, radius, 300, 310);
- else
- g.FillPie(color, positionX * radius * 2 + 15, positionY * radius * 2 + 12, radius, radius, 0, 360);
- }
- else if (direction == DIRECTION.Down)
- {
- if (mouthOpen)
- g.FillPie(color, positionX * radius * 2 + 15, positionY * radius * 2 + 12, radius, radius, 120, 310);
- else
- g.FillPie(color, positionX * radius * 2 + 15, positionY * radius * 2 + 12, radius, radius, 0, 360);
- }
- }
- }
- }
- RESOURCES--
- http://icons.iconarchive.com/icons/gcds/chinese-new-year/512/orange-icon.png
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement