pygame实现弹球游戏

本文实例为大家分享了pygame实现弹球游戏的具体代码,供大家参考,具体内容如下

pygame弹球游戏

写的很简陋
pip install pygame 安装pygame模块

代码,复制运行即可

import pygame
import random

pygame.init()

win = pygame.display.set_mode((600, 600)) # 画布窗口的大小
pygame.display.set_caption("弹球游戏") # 窗口标题

x, y = 300, 0 # 方块的起点
width, height = 10, 10 # 方块的宽,高
speed = 1 # 速度

def _randomOK():
  return random.randint(0, 1)

stop = False
_random = _randomOK()

str1 = "暂停中"
baffle = 250
status = 0

count = 0
top = 0
while True:
  # 刷新频率, 小球移动速度
  pygame.time.Clock().tick(1000)

  for event in pygame.event.get():
    # 窗口x事件
    if event.type == pygame.QUIT:
      exit(0)
    elif event.type == pygame.KEYDOWN:
      # 回车事件
      if event.key == 13:
        str1 = "暂停中"
        stop = not stop
        if status == 1:
          x, y = 300, 0

  keys = pygame.key.get_pressed()
  if stop:
    pygame.display.set_caption(str1) # 窗口标题
    continue
  if y >= 590:
    status = 1
    stop = not stop
    str1 = "游戏结束,回车重新开始,反弹次数" + str(count)
    count = 0

  pygame.display.set_caption("弹球游戏") # 窗口标题
  if y == 0:
    top = 0
  if top == 0:
    if _random == 0: # 向下左弹
      x -= speed
      y += speed
    elif _random == 1:
      x += speed
      y += speed
  else:
    if _random == 0: # 向上左弹
      x -= speed
      y -= speed
    elif _random == 1: # 向上右弹
      x += speed
      y -= speed
  # 方向箭头响应
  if keys[pygame.K_LEFT]:
    baffle -= speed
    if baffle < 0:
      baffle = 0

  if keys[pygame.K_RIGHT]:
    baffle += speed
    if baffle > 500:
      baffle = 500
  # 碰撞逻辑
  if 500 <= y <= 520:
    print(x, y)
    print(baffle)
    # y 高度坐标 200 x 宽度坐标 200
    # x坐标加300 大于 宽度初始坐标, 小于 宽度+300
    if baffle <= x <= baffle + 100:
      count += 1
      top = 1

  # 防止跑出边界
  if x > win.get_size()[0] - width:
    _random = _randomOK()
    x = win.get_size()[0] - width

  if x < 0:
    _random = _randomOK()
    x = 0

  if y > win.get_size()[1] - height:
    _random = _randomOK()
    y = win.get_size()[1] - height

  if y < 0:
    _random = _randomOK()
    y = 0

  # 将每一帧的底色先填充成黑色
  win.fill((64, 158, 255))
  # 画方块
  pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
  # 挡板设置,
  pygame.draw.rect(win, (255, 255, 255), (baffle, 500, 100, 20))
  # 更新画布
  pygame.display.update()
pygame.quit()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • 使用python和pygame制作挡板弹球游戏

    python是个很有趣的语言,可以在cmd命令窗口运行,还有很多的功能强大的模块. 学了一天pygame,用python和pygame写一个简单的挡板弹球游戏. 2018年6月21日 00:15:21 GitHub: EasyBaffleBallGame # -*- coding:utf-8 -*- from sys import exit import pygame from pygame.locals import * pygame.init() # 创建窗口 ScreenWidth = 5

  • python运用pygame库实现双人弹球小游戏

    使用python pygame库实现一个双人弹球小游戏,两人分别控制一个左右移动的挡板用来拦截小球,小球会在两板间不停弹跳,拦截失败的一方输掉游戏,规则类似于简化版的乒乓球. 因为是第一次用pygame写python小游戏并且只用了两三个小时,所以有些粗糙,部分方面有些bug,比如板子可以移动出屏幕外,游戏结束后的提示显示不全. 但是关键部分如小球的移动和基本功能等,还算比较完善. 代码如下: 运行环境为python 3.7,需要安装pygame库 import pygame,sys,time,

  • python pygame实现挡板弹球游戏

    学了一天pygame,用python和pygame写一个简单的挡板弹球游戏 GitHub: EasyBaffleBallGame # -*- coding:utf-8 -*- from sys import exit import pygame from pygame.locals import * pygame.init() # 创建窗口 ScreenWidth = 500 ScreenHright = 720 ScreenSize = (ScreenWidth, ScreenHright)

  • pygame库实现移动底座弹球小游戏

    本文实例为大家分享了pygame实现移动底座弹球的具体代码,供大家参考,具体内容如下 输出结果: 实现代码: # -*- coding: utf-8 -*- #Py之pygame:有趣好玩--利用pygame库实现一个移动底座弹球的小游戏 import pygame as pg from pygame.locals import * #将pygame所有常量导入,如后面的QUIT from time import sleep import sys #设置基本屏幕参数 pg.init() #初始化

  • pygame实现弹球游戏

    本文实例为大家分享了pygame实现弹球游戏的具体代码,供大家参考,具体内容如下 pygame弹球游戏 写的很简陋 pip install pygame 安装pygame模块 代码,复制运行即可 import pygame import random pygame.init() win = pygame.display.set_mode((600, 600)) # 画布窗口的大小 pygame.display.set_caption("弹球游戏") # 窗口标题 x, y = 300,

  • Java基于swing实现的弹球游戏代码

    本文实例讲述了Java基于swing实现的弹球游戏代码.分享给大家供大家参考. 主要功能代码如下: 复制代码 代码如下: package Game; import java.awt.Graphics; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.Random;

  • python基于pygame实现响应游戏中事件的方法(附源码)

    本文实例讲述了python基于pygame实现响应游戏中事件的方法.分享给大家供大家参考,具体如下: 先看一下我做的demo效果: 当玩家按下键盘上的:上,下,左,右键的时候,后台会打印出玩家所按键的数字值,而图形会随之移动 这是客观上面存在的现象. 那么啥是事件呢? 你叫我做出定义,我不知道,我只能举个例子说明,例如接下来的代码中,列出来一些关于游戏中的事件 ''' 事件 产生途径 参数 QUIT 用户按下关闭按钮 none ATIVEEVENT Pygame被激活或者隐藏 gain, sta

  • 非html5实现js版弹球游戏示例代码

    开始前的html页面  开始后的html游戏界面  html页面布局,即index.html文件源码如下: 复制代码 代码如下: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" co

  • java实现简单的弹球游戏

    弹球游戏实现原理: 隔一定时间(小于1秒)重新绘制图像,因为Graphics类是一个抽象类,创建子类的时候需要把所有涉及的方法都得重写,所以这里使用的是创建Canvas的子类,只需要重写它的paint()方法来实现.这里我们用了键盘监听事件.Timer类等. 游戏说明: 该弹球游戏中的小球会随着时间增加速度且速度最多是横向速度和垂直速度为10.当小球的y坐标(垂直坐标)大于球拍的y坐标(垂直坐标)即判断游戏结束.控制台显示的是小球的x方向的速度和y方向的速度. import java.awt.*

  • python编写弹球游戏的实现代码

    弹球游戏: from tkinter import * import time import random tk=Tk() #创建一个界面 tk.title("弹球游戏") canvas=Canvas(tk,width=800,height=600,bg="skyblue",bd=0,highlightthickness = 0) tk.resizable(0,0) #表示边框不能被拉伸 canvas.pack() #使部件放在主窗口中 tk.update() #刷

  • pygame实现雷电游戏雏形开发

    本文实例为大家分享了pygame实现雷电游戏开发代码,供大家参考,具体内容如下 源代码: stars.py #-*- coding=utf-8 -*- #!/usr/bin/python import pygame from pygame.locals import * from random import randint import math class Star(object): def __init__(self, x, y, speed, color=(255,255,255)): s

  • pygame实现俄罗斯方块游戏(AI篇1)

    上次更新到pygame实现俄罗斯方块游戏(基础篇3) 现在继续 一.定义玩家类 定义玩家类是为了便于进行手动和机器模式或各种不同机器人模式的混合使用,增加代码扩展性. 可以先定义一个玩家基类 class Player(object): auto_mode=False # 是否是自动模式,自动模式应当不响应键盘操作 def __init__(self): pass def run(self): # 进行操作 pass 手动类和机器类继承自Player类 class HumanPlayer(Play

随机推荐