编写程序,实现使用键盘上的上下左右箭头控制界面上图片的移动。


//MyFrame.java

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class MyFrame extends JFrame implements KeyListener {


private Icon icon,icon2;
private JLabel lab;


public MyFrame(String s) {
super(s);
setSize(700, 500);
setLocationRelativeTo(null);


JPanel contentPane = new JPanel(null);
// contentPane.setLayout();
setContentPane(contentPane);


icon = new ImageIcon("D:\\图片\\c.gif");
lab = new JLabel(icon);
lab.setBounds(0, 0, 100, 100);
addKeyListener(this);


contentPane.add(lab);


setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}


@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub


}


@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
icon2 = new ImageIcon("D:\\图片\\d.gif");
lab.setIcon(icon2);
}


@Override
public void keyReleased(KeyEvent e) {

lab.setIcon(icon);
// TODO Auto-generated method stub
int x = lab.getX();
int y = lab.getY();
if (x >= 700) {
lab.setBounds(0, y, 100, 100);
} else if (x < 0) {
lab.setBounds(700, y, 100, 100);
} else if (y < 0) {
lab.setBounds(x, 500, 100, 100);
} else if (y > 500) {
lab.setBounds(x, 0, 100, 100);
} else {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
lab.setBounds(x - 50, y, 100, 100);
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
lab.setBounds(x + 50, y, 100, 100);
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
lab.setBounds(x, y - 50, 100, 100);
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
lab.setBounds(x, y + 50, 100, 100);
}
}
}

}


//MyFrameDemo.java

public class MyFrameDemo {
public static void main(String[] args) {
MyFrame mf = new MyFrame("键盘事件演示");
}
}

  • 8
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 以下是一个简单的 Python 代码示例,用于实现大鱼吃小鱼的游戏: ```python import random import turtle # 设置窗口大小和标题 window = turtle.Screen() window.title("大鱼吃小鱼") window.setup(600, 600) # 创建大鱼 big_fish = turtle.Turtle() big_fish.shape("turtle") big_fish.color("blue") big_fish.penup() big_fish.speed(0) # 创建小鱼 small_fish = turtle.Turtle() small_fish.shape("circle") small_fish.color("red") small_fish.penup() small_fish.speed(0) small_fish.goto(random.randint(-250, 250), random.randint(-250, 250)) # 创建食物 food = turtle.Turtle() food.shape("triangle") food.color("green") food.penup() food.speed(0) food.goto(random.randint(-250, 250), random.randint(-250, 250)) # 设置键盘监听 def move_up(): big_fish.setheading(90) big_fish.forward(10) def move_down(): big_fish.setheading(270) big_fish.forward(10) def move_left(): big_fish.setheading(180) big_fish.forward(10) def move_right(): big_fish.setheading(0) big_fish.forward(10) window.listen() window.onkeypress(move_up, "Up") window.onkeypress(move_down, "Down") window.onkeypress(move_left, "Left") window.onkeypress(move_right, "Right") # 游戏循环 while True: # 小鱼和食物的随机移动 small_fish.forward(random.randint(1, 5)) small_fish.left(random.randint(-30, 30)) food.forward(random.randint(1, 5)) food.left(random.randint(-30, 30)) # 大鱼吃小鱼 if big_fish.distance(small_fish) < 20: small_fish.goto(random.randint(-250, 250), random.randint(-250, 250)) # 大鱼吃食物 if big_fish.distance(food) < 20: food.goto(random.randint(-250, 250), random.randint(-250, 250)) # 边界检测 if big_fish.xcor() > 300 or big_fish.xcor() < -300: big_fish.right(180) if big_fish.ycor() > 300 or big_fish.ycor() < -300: big_fish.right(180) ``` 运行该代码,即可开始玩大鱼吃小鱼的游戏。通过键盘上下左右箭头控制大鱼的移动,尽可能地吃掉小鱼和食物,避免碰到边界。 ### 回答2: Python是一种强大的编程语言,可以用来编写各种各样的应用程序,包括小游戏。大鱼吃小鱼是一个简单而有趣的游戏,我们可以使用Python来编写它。 首先,我们需要创建一个游戏窗口。我们可以使用Python的pygame库来实现这一点。通过设置窗口的大小和标题,我们可以创建一个适合游戏的界面。 接下来,我们需要定义游戏角色——大鱼和小鱼。我们可以使用Python的类来表示它们。大鱼可以根据玩家的输入来移动,而小鱼则会随机游动。 我们还需要定义一些游戏规则。例如,当大鱼吃掉小鱼时,玩家得分增加。另外,如果大鱼和小鱼相撞,则游戏结束。 游戏的主要逻辑可以放在一个主循环中。在每个循环中,我们可以处理玩家的输入,更新游戏角色的位置,检查是否发生碰撞,并更新游戏分数。 最后,我们可以添加一些图形和音效来增强游戏体验。使用Python的pygame库,我们可以加载图像和音频文件,并在适当的时候播放它们。 通过以上步骤,我们就可以完成一个简单的大鱼吃小鱼游戏。当玩家操作大鱼吃掉尽可能多的小鱼时,游戏会变得越来越有趣和具有挑战性。Python的简洁性和易用性使得编写这样的小游戏变得轻而易举。 ### 回答3: Python编写大鱼吃小鱼的游戏,可以使用pygame库来实现游戏界面的绘制和交互操作。以下是一个简单的示例代码: ```python import pygame import random # 初始化pygame pygame.init() # 设置游戏窗口的大小 screen_width = 800 screen_height = 600 screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption("大鱼吃小鱼") # 加载游戏资源 background = pygame.image.load("background.png") fish1 = pygame.image.load("fish1.png") fish2 = pygame.image.load("fish2.png") fish3 = pygame.image.load("fish3.png") # 设置鱼的初始位置和速度 fish_x = 400 fish_y = 300 fish_speed = 5 # 生成小鱼的初始位置和速度 small_fish_x = random.randint(0, screen_width) small_fish_y = random.randint(0, screen_height) small_fish_speed = 3 # 游戏循环 running = True while running: screen.blit(background, (0, 0)) # 绘制背景 for event in pygame.event.get(): if event.type == pygame.QUIT: running = False keys = pygame.key.get_pressed() if keys[pygame.K_UP]: fish_y -= fish_speed if keys[pygame.K_DOWN]: fish_y += fish_speed if keys[pygame.K_LEFT]: fish_x -= fish_speed if keys[pygame.K_RIGHT]: fish_x += fish_speed # 判断大鱼是否吃到小鱼 if abs(fish_x - small_fish_x) < 50 and abs(fish_y - small_fish_y) < 50: small_fish_x = random.randint(0, screen_width) small_fish_y = random.randint(0, screen_height) screen.blit(fish1, (fish_x, fish_y)) # 绘制大鱼 screen.blit(fish2, (small_fish_x, small_fish_y)) # 绘制小鱼 pygame.display.flip() # 更新显示 # 退出游戏 pygame.quit() ``` 在游戏中,使用上下左右方向键来控制大鱼的移动。当大鱼和小鱼的位置重叠时,说明大鱼吃到了小鱼,随机生成一个新的小鱼的位置。游戏循环不断地更新画面并响应玩家的操作,直到玩家退出游戏。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值