pygame

mac系统下安装pygame

戏子无情 提交于 2020-01-29 19:58:20
安装homebrew并更新到最新 安装pip并更新到最新 在terminal中输入: sudo xcode - select - - install 下载完X-code的命令行工具,再输入: 安装Pygame所依赖的库: brew install sdl smpeg sdl_image sdl_mixer sdl_ttf portmidi hg 如果你还想启用较高级的功能,如在游戏中包含声音,可安装下面两个额外的库: brew install sdl_mixer portmidi 最后输入: sudo pip install pygame 如果是python3,则输入: sudo pip3 install pygame 完成上述步骤以后: 输入: python3 运行后输入: import pygame 当出现 pygame 1.9.6 Hello from the pygame community. https://www.pygame.org/contribute.html pygame就安装成功了。 来源: CSDN 作者: weixin_lyy 链接: https://blog.csdn.net/weixin_45237889/article/details/104108604

飞机大战1

怎甘沉沦 提交于 2020-01-28 02:31:41
准备工作:导入pygame: 点击pip —>输入pygame 点击确定即可导入。 一:初始化 1.1游戏的初始化和退出: pygame.init() :初始化方法 pygame.quit():退出方法 import pygame pygame . init ( ) print ( "游戏的代码..." ) pygame . quit ( ) 1.2 游戏的坐标系: import pygame hero_rect = pygame . Rect ( 100 , 500 , 120 , 125 ) print ( "英雄的原点 %d %d " % ( hero_rect . x , hero_rect . y ) ) print ( "英雄的尺寸 %d %d" % ( hero_rect . width , hero_rect . height ) ) print ( "%d %d" % hero_rect . size ) 运行结果: pygame 1.9 .6 Hello from the pygame community . https : // www . pygame . org / contribute . html 英雄的原点 100 500 英雄的尺寸 120 125 120 125 1.3 创建游戏的主窗口: import pygame pygame . init

PyGame快速入门

蓝咒 提交于 2020-01-27 13:44:19
pygame 快速入门 目标 项目准备 使用 pygame 创建图形窗口 理解 图像 并实现图像绘制 理解 游戏循环 和 游戏时钟 理解 精灵 和 精灵组 项目准备 新建 飞机大战 项目 新建一个 hm_01_pygame入门.py 导入 游戏素材图片 游戏的第一印象 把一些 静止的图像 绘制到 游戏窗口 中 根据 用户的交互 或其他情况, 移动 这些图像,产生动画效果 根据 图像之间 是否发生重叠,判断 敌机是否被摧毁 等其他情况 01. 使用 pygame 创建图形窗口 小节目标 游戏的初始化和退出 理解游戏中的坐标系 创建游戏主窗口 简单的游戏循环 可以将图片素材 绘制 到 游戏的窗口 上,开发游戏之前需要先知道 如何建立游戏窗口 ! 1.1 游戏的初始化和退出 要使用 pygame 提供的所有功能之前,需要调用 init 方法 在游戏结束前需要调用一下 quit 方法 方法 说明 pygame.init() 导入并初始化所有 pygame 模块,使用其他模块之前,必须先调用 init 方法 pygame.quit() 卸载所有 pygame 模块,在游戏结束之前调用! import pygame pygame.init() # 游戏代码... pygame.quit() 1.2 理解游戏中的坐标系 坐标系 原点 在 左上角 (0, 0) x 轴 水平方向向 右 ,逐渐增加

Pygame: image not appearing on moving background

落爺英雄遲暮 提交于 2020-01-26 03:55:06
问题 my code was working fine when i didnt have a moving background. But now, i started making my character move from left to right and it stopped showing up. Ive been trying to figure out what the issue is for days, please help!! Thanks!!! the following is the line of code i used for the background and character: # create class for character (object) class player(object): def __init__(self, x, y, width, height): # initialize attributes self.x = x self.y = y self.width = width self.height = height

Pygame: Character not appearing on screen

杀马特。学长 韩版系。学妹 提交于 2020-01-26 03:54:07
问题 I have been trying to get this code to work for weeks and cannot figure out the issue. I have a list of images, that I am using to animate my character. THe game worked fine at first, but ever since I made the background animate (start moving left to right), my character stopped appearing. Do I have to change the location of some of my code? Really confused. Thank you!! # create lists with images of character walking left and right rightDirection = [pygame.image.load('R1.png'), pygame.image

pygame-How to replace an image with an other one

心不动则不痛 提交于 2020-01-25 20:53:06
问题 I have this sort of code: width = 100 height = 50 gameDisplay.blit(button, (width, height)) pygame.display.update() while True: for event in pygame.event.get(): if event.type == pygame.MOUSEBUTTONUP and event.button == 1: # replace the button's image with another Is there any function or something that would let me replace the image with another? 回答1: You cannot 'replace' anything you've drawn. What you do is that you draw new images over the existing ones. Usually, you clear the screen and

how do i connect a page to a button - pygame?

僤鯓⒐⒋嵵緔 提交于 2020-01-25 10:12:18
问题 I have a menu with 3 buttons. I want one of the buttons to connect to my game page which I have but am not sure how to make this happen. So I want the "game" button to lead to the screen which has the actual game (like the screen where you play the game). i am trying to figure out how to connect a page to a button in this pygame. Thanks # import images background = pygame.image.load('background.png') backgroundX = 0 backgroundX2 = background.get_width() homeScreen = pygame.image.load('home

How to draw a line in pygame that moves up the screen

情到浓时终转凉″ 提交于 2020-01-25 06:45:28
问题 I have made a python game, and when you press space it shoots a bullet (a line). It is only able to shoot once though. I was wondering how I could make it shoot multiple times? shotStartX = x + 30 shotStartY = y + 20 shotEndX = x + 30 shotEndY = y shoot = False while True: if event.type == pygame.KEYDOWN: elif event.key == pygame.K_SPACE: shoot = True gameDisplay.fill(blue) if shoot: pygame.draw.line(gameDisplay,red,(shotStartX,shotStartY),(shotEndX,shotEndY),5) shotStartY -= 10 shotEndY -=

How to draw a line in pygame that moves up the screen

偶尔善良 提交于 2020-01-25 06:45:25
问题 I have made a python game, and when you press space it shoots a bullet (a line). It is only able to shoot once though. I was wondering how I could make it shoot multiple times? shotStartX = x + 30 shotStartY = y + 20 shotEndX = x + 30 shotEndY = y shoot = False while True: if event.type == pygame.KEYDOWN: elif event.key == pygame.K_SPACE: shoot = True gameDisplay.fill(blue) if shoot: pygame.draw.line(gameDisplay,red,(shotStartX,shotStartY),(shotEndX,shotEndY),5) shotStartY -= 10 shotEndY -=

How to detect collision using PyBox2d and use that information

梦想的初衷 提交于 2020-01-25 00:20:49
问题 I am trying to filter collisions occurring in my Box2D world by reproducing this example: https://github.com/pybox2d/pybox2d/blob/master/examples/collision_filtering.py I have four classes in my world, Car, Wheel, Building, and Pedestrian, I want to filter which instance collided with which and one of the possible outputs is (pseudo-code) if contact.FixtureA.isinstance(Pedestrian) and contact.FixtureB.isinstance(Car): print("You have caused a traffic accident") I have this set of categories