c语言,贪吃蛇创建随机食物,这个分程序中我画线的句子是不是出错,应该是≠NULL才对

c语言,贪吃蛇创建随机食物,这个分程序中我画线的句子是不是出错,应该是≠NULL才对,第1张

虽然你没有把snake还有变量head的定义发出来,但是我认为这一句的确错了。如果是==的话,这个while循环就执行不下去,里面的判断也没有存在的意义。应该是while(q!=NULL)或者是while(q->next!=NULL),是哪一个要看你head的定义和初始化了。

import java.awt.Color

import java.awt.Graphics

import java.awt.Graphics2D

import java.awt.Rectangle

import java.awt.event.KeyAdapter

import java.awt.event.KeyEvent

import java.awt.image.BufferedImage

import java.util.ArrayList

import java.util.List

import javax.swing.JFrame

public class InterFace extends JFrame {

/**

* WIDTH:宽

* HEIGHT:高

* SLEEPTIME:可以看作蛇运动的速度

* L = 1,R = 2, U = 3, D = 4 左右上下代码

*/

public static final int WIDTH = 800, HEIGHT = 600, SLEEPTIME = 200, L = 1,R = 2, U = 3, D = 4

BufferedImage offersetImage= new BufferedImage(WIDTH, HEIGHT,BufferedImage.TYPE_3BYTE_BGR)

Rectangle rect = new Rectangle(20, 40, 15 * 50, 15 * 35)

Snake snake

Node node

public InterFace() {

//创建"蛇"对象

snake = new Snake(this)

//创建"食物"对象

createNode()

this.setBounds(100, 100, WIDTH, HEIGHT)

//添加键盘监听器

this.addKeyListener(new KeyAdapter() {

public void keyPressed(KeyEvent arg0) {

System.out.println(arg0.getKeyCode())

switch (arg0.getKeyCode()) {

//映射上下左右4个键位

case KeyEvent.VK_LEFT:

snake.dir = L

break

case KeyEvent.VK_RIGHT:

snake.dir = R

break

case KeyEvent.VK_UP:

snake.dir = U

break

case KeyEvent.VK_DOWN:

snake.dir = D

}

}

})

this.setTitle("贪吃蛇 0.1 By : Easy")

this.setDefaultCloseOperation(EXIT_ON_CLOSE)

this.setVisible(true)

//启动线程,开始执行

new Thread(new ThreadUpadte()).start()

}

public void paint(Graphics g) {

Graphics2D g2d = (Graphics2D) offersetImage.getGraphics()

g2d.setColor(Color.white)

g2d.fillRect(0, 0, WIDTH, HEIGHT)

g2d.setColor(Color.black)

g2d.drawRect(rect.x, rect.y, rect.width, rect.height)

//如果蛇碰撞(吃)到食物,则创建新食物

if (snake.hit(node)) {

createNode()

}

snake.draw(g2d)

node.draw(g2d)

g.drawImage(offersetImage, 0, 0, null)

}

class ThreadUpadte implements Runnable {

public void run() {

//无限重绘画面

while (true) {

try {

Thread.sleep(SLEEPTIME)

repaint()//

} catch (InterruptedException e) {

e.printStackTrace()

}

}

}

}

/**

* 创建食物

*/

public void createNode() {

//随机食物的出现位置

int x = (int) (Math.random() * 650) + 50,y = (int) (Math.random() * 500) + 50

Color color = Color.blue

node = new Node(x, y, color)

}

public static void main(String args[]) {

new InterFace()

}

}

/**

* 节点类(包括食物和蛇的身躯组成节点)

*/

class Node {

int x, y, width = 15, height = 15

Color color

public Node(int x, int y, Color color) {

this(x, y)

this.color = color

}

public Node(int x, int y) {

this.x = x

this.y = y

this.color = color.black

}

public void draw(Graphics2D g2d) {

g2d.setColor(color)

g2d.drawRect(x, y, width, height)

}

public Rectangle getRect() {

return new Rectangle(x, y, width, height)

}

}

/**

* 蛇

*/

class Snake {

public List<Node>nodes = new ArrayList<Node>()

InterFace interFace

int dir=InterFace.R

public Snake(InterFace interFace) {

this.interFace = interFace

nodes.add(new Node(20 + 150, 40 + 150))

addNode()

}

/**

* 是否碰撞到食物

* @return true 是 false 否

*/

public boolean hit(Node node) {

//遍历整个蛇体是否与食物碰撞

for (int i = 0i <nodes.size()i++) {

if (nodes.get(i).getRect().intersects(node.getRect())) {

addNode()

return true

}

}

return false

}

public void draw(Graphics2D g2d) {

for (int i = 0i <nodes.size()i++) {

nodes.get(i).draw(g2d)

}

move()

}

public void move() {

nodes.remove((nodes.size() - 1))

addNode()

}

public synchronized void addNode() {

Node nodeTempNode = nodes.get(0)

//如果方向

switch (dir) {

case InterFace.L:

//判断是否会撞墙

if (nodeTempNode.x <= 20) {

nodeTempNode = new Node(20 + 15 * 50, nodeTempNode.y)

}

nodes.add(0, new Node(nodeTempNode.x - nodeTempNode.width,

nodeTempNode.y))

break

case InterFace.R:

//判断是否会撞墙

if (nodeTempNode.x >= 20 + 15 * 50 - nodeTempNode.width) {

nodeTempNode = new Node(20 - nodeTempNode.width, nodeTempNode.y)

}

nodes.add(0, new Node(nodeTempNode.x + nodeTempNode.width,

nodeTempNode.y))

break

case InterFace.U:

//判断是否会撞墙

if (nodeTempNode.y <= 40) {

nodeTempNode = new Node(nodeTempNode.x, 40 + 15 * 35)

}

nodes.add(0, new Node(nodeTempNode.x, nodeTempNode.y - nodeTempNode.height))

break

case InterFace.D:

//判断是否会撞墙

if (nodeTempNode.y >= 40 + 15 * 35 - nodeTempNode.height) {

nodeTempNode = new Node(nodeTempNode.x,40 - nodeTempNode.height)

}

nodes.add(0, new Node(nodeTempNode.x, nodeTempNode.y + nodeTempNode.height))

break

}

}

}

#include<stdio.h>

#include<stdlib.h>

#include<time.h>

#include<conio.h>

typedef struct snake

{

int a

int b

struct snake *u

struct snake *n

}snake,*snake1

typedef struct food

{

int a

int b

}food

int main()

{

char c,c0 = 'd'

int i,j,k,n=1,t,at

snake p,q

snake *dd,*dd0,*dd1,*dd2

food f

srand(time(NULL))

p.u = NULL

p.n = &q

p.a = 5p.b = 6q.a = 5q.b = 5

q.u = &pq.n = NULL

dd=dd2= &q

f.a=(rand()%15+1)

f.b=(rand()%15+1)

while(1)

{

srand(time(NULL))

system("cls")

for(i = 0i <17i ++)

{

for(j = 0j <17j++)

{

if(i == 0 )

printf("▁")

else if(i == 16)

printf("▔")

else if(j == 0)

printf("▕")

else if(j == 16)

printf("▏")

else if(i == p.a &&j == p.b)

printf("◆")

else if(i == f.a &&j == f.b)

printf("☆")

else

{

t = 0

dd = dd2

for(k = 0k <n k++)

{

if(i == dd->a &&j == dd->b)

{

printf("¤")

t = 1

break

}

dd = dd->u

}

if(t == 0)

printf(" ")

}

}printf("\n")

}

at = 0

dd =dd2

for(i=0i<ni++)

{

if(p.a == dd->a &&p.b == dd->b)

{

printf("game over!!\n")

for (int iii=0iii<1000iii++) {}

exit(0)

}

dd = dd->u

}

if(p.a == f.a &&p.b == f.b)

{

dd = dd2

at =1

f.a = (rand()%15+1)

f.b = (rand()%15+1)

for(i=0i<ni++)

{

if(f.a == dd->a &&f.b == dd->b)

{

f.a = dd2->a

f.b = dd2->b

break

}

}

n++

}

if(kbhit())

{

c = getch()

dd = dd2

if(c == 'w' &&c0 != 's')

{

if(at == 1)

{

dd0 =(snake1)malloc(sizeof(snake))

dd0->a = dd2->add0->b = dd2->b

dd0->n = NULLdd0->u = dd2

dd2=dd0

}

dd = dd2

for(i = 0i<n i++)

{

dd1 = dd->u

dd->b = dd1->b

dd->a = dd1->a

dd = dd->u

}

if(p.a == 1)

p.a = 15

else

p.a = (p.a-1)%15

}

else if(c == 's' &&c0 != 'w')

{

if(at == 1)

{

dd0 =(snake1)malloc(sizeof(snake))

dd0->a = dd2->add0->b = dd2->b

dd0->n = NULLdd0->u = dd2

dd2=dd0

}

dd = dd2

for(i = 0i<n i++)

{

dd1 = dd->u

dd->b = dd1->b

dd->a = dd1->a

dd = dd->u

}

p.a = (p.a%15)+1

}

else if(c == 'a' &&c0 != 'd')

{

if(at == 1)

{

dd0 =(snake1)malloc(sizeof(snake))

dd0->a = dd2->add0->b = dd2->b

dd0->n = NULLdd0->u = dd2

dd2=dd0

}

dd = dd2

for(i = 0i<n i++)

{

dd1 = dd->u

dd->b = dd1->b

dd->a = dd1->a

dd = dd->u

}

if(p.b == 1)

p.b = 15

else

p.b = (p.b-1)%15

}

else if(c == 'd' &&c0 != 'a')

{

if(at == 1)

{

dd0 =(snake1)malloc(sizeof(snake))

dd0->a = dd2->add0->b = dd2->b

dd0->n = NULLdd0->u = dd2

dd2=dd0

}

dd = dd2

for(i = 0i<n i++)

{

dd1 = dd->u

dd->b = dd1->b

dd->a = dd1->a

dd = dd->u

}

p.b = (p.b%15)+1

}

else

{

goto qq

}

c0 = c

}

else

{

qq: if(c0 == 'w')

{

if(at == 1)

{

dd0 =(snake1)malloc(sizeof(snake))

dd0->a = dd2->add0->b = dd2->b

dd0->n = NULLdd0->u = dd2

dd2=dd0

}

dd = dd2

for(i = 0i<n i++)

{

dd1 = dd->u

dd->b = dd1->b

dd->a = dd1->a

dd = dd->u

}

if(p.a == 1)

p.a = 15

else

p.a=(p.a-1)%15

}

else if(c0 == 's')

{

if(at == 1)

{

dd0 =(snake1)malloc(sizeof(snake))

dd0->a = dd2->add0->b = dd2->b

dd0->n = NULLdd0->u = dd2

dd2=dd0

}

dd = dd2

for(i = 0i<n i++)

{

dd1 = dd->u

dd->b = dd1->b

dd->a = dd1->a

dd = dd->u

}

p.a=(p.a%15)+1

}

else if(c0 == 'a')

{

if(at == 1)

{

dd0 =(snake1)malloc(sizeof(snake))

dd0->a = dd2->add0->b = dd2->b

dd0->n = NULLdd0->u = dd2

dd2=dd0

}

dd = dd2

for(i = 0i<n i++)

{

dd1 = dd->u

dd->b = dd1->b

dd->a = dd1->a

dd = dd->u

}

if(p.b == 1)

p.b = 15

else

p.b=(p.b-1)%15

}

else if(c0 == 'd')

{

if(at == 1)

{

dd0 =(snake1)malloc(sizeof(snake))

dd0->a = dd2->add0->b = dd2->b

dd0->n = NULLdd0->u = dd2

dd2=dd0

}

dd = dd2

for(i = 0i<n i++)

{

dd1 = dd->u

dd->b = dd1->b

dd->a = dd1->a

dd = dd->u

}

p.b=(p.b%15)+1

}

}

fflush(stdin)

dd = &q

_sleep(0)

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11301920.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-15
下一篇 2023-05-15

发表评论

登录后才能评论

评论列表(0条)

保存